Last updated by miky91 2 years ago

PayPal Plugin

Installation

To install this plug-in run:

grails install-plugin paypal

Usage

This plug-in provides 3 new artefacts:

  • A PaypalController - a Grails controller that deals with PayPal requests and integrations with the PayPal Instance Payment Notification (IPN) system
  • A PaypalTagLib - a tag library that provides the ability to create PayPal "buy now" buttons
  • A Payment domain class - a domain class that is used to store information about Payments
To get started you need to configure PayPal server and merchant email address in Config.groovy as well as the grails server URL used for absolute links. For example:

environments {
  production {
     grails.paypal.server = "https://www.paypal.com/cgi-bin/webscr"
     grails.paypal.email = "example@business.com"
     grails.serverURL = "http://www.grails.org"		
  }
  development {
     grails.paypal.server = "https://www.sandbox.paypal.com/cgi-bin/webscr"
     grails.paypal.email = "testpp_1211202427_biz@g2one.com"
     grails.serverURL = "http://812.99.101.131"				
  }
}

With this done the next thing to do is to create a PayPal button. For example:

<paypal:button 
	itemName="iPod Nano"
	itemNumber="IPD0843403"
	transactionId="${payment?.transId}"
	amount="99,00"
        discountAmount="0"
	buyerId="${user.id}"
	/>

The itemName, itemNumber, amount, discountAmount and buyerId are required. The buyerId can be a reference to an existing domain class id such as a User id. The transactionId is used in case you are resuming an existing Payment.

When the button is clicked the plugin will create and save a new instance of the org.grails.paypal.Payment class to track the order. The button will also send the IPN URL that PayPal should use to send notifications back.

In order for IPN to function correctly you must set the grails.serverURL setting in Config.groovy to a web facing domain or IP address (so that PayPal can do a post back)

Payment Tracking with Filters

A typical way to integrate with the PayPal plug-in is through the use of filters. The PayPal plug-in places the payment instance into the request when it has been created so a filter that executes after the PaypalController and integrate with it.

For example:

buyFilter(controller:"paypal", action:"buy") {
 after = {
      new IPodPayment(payment:request.payment, IPod.get(params.id)).save()
 }
}
paymentReceivedFilter(controller:'paypal', action:'(success|notifyPaypal)') {
  after = {
     def payment = request.payment
     if(payment && payment.status == org.grails.paypal.Payment.COMPLETE) {
         def payment = IPodPayment.findByPayment(request.payment)
         payment.shipIt()
     }
  }
}

Testing after plugin modification

As every Grails plugin is also a Grails application, you can run the Paypal plugin as an app and then enter the following URL in your browser:

http://localhost:8080/paypal/paypal/test