PayPal Integration with Grails
Posted By : Santosh Singh | 30-Mar-2015
To use PayPal first we need to install PayPal plugin by editing conf/BuildConfig.groovy file.
By Adding this line project automaticly install all dependency of PayPal, for latest plugin please go to grails plugin section.
...
plugins {
...
compile ":paypal:0.6.8"
}
Configure Paypal Plugin
Create two account one is for buyer and one is bussiness type so that we can use that account for testing.
An account is associated with an email address. I.e. the information needed for someone to send a payment is just the email address of the merchant(bussiness) account.
Note :- WE WILL USE SENDBOX ENVVIROMENT FOR TESTING PURPOSES. WE CAN TEST APPLICATION WITHOUT TRANSFERRING REAL MONEY.
Now configure url for test in /conf/Config.groovy
development {
grails.paypal.server = "https://www.sandbox.paypal.com/cgi-bin/webscr"
grails.paypal.email = "[email protected]"
grails.serverURL = "http://localhost:8080" // your project url
}
URL for production.
production {
grails.paypal.server = "https://www.paypal.com/cgi-bin/webscr"
grails.paypal.email = "[email protected]"
grails.serverURL = "http://store.grails.asia"
}
Create Domain Class
Here is an example domain class that represents a product:
class MyItem {
String itemName
Buyer buyer
Long itemNumber
BigDecimal itemPrice
BigDecimal itemDiscount
static constraints = {
name(blank:false, nullable:false, unique: true)
price(blank:false, nullable:false)
}
}
Here is a sample domain class that represents a transaction. The completed field is for the status of the transaction. Pending state uses false value while completed uses true value.
// This class is from PayPal plugin
import org.grails.paypal.Payment
class ItemPaymentDetails {
MyItem myItem
Payment payment
boolean isCompleted = false
}
Display Buy Button.
Here in gsp to display the item with Buy button:
<paypal:button
itemName="${myItem.name}"
itemNumber="${myItem.itemNumber}"
discountAmount="${myItem.itemDiscount}"
amount="${myItem.itemPrice}"
buyerId="${buyer.id}"/>
Purchase Filter
Create a Buy/Purchase Filter. For example, create the filter grails-app/conf/myfilters/PurchaseFilters.groovy with this code:
class PurchaseFilters {
def filters = {
all(controller:'paypal', action:'buy') {
before = {
}
after = { Map model ->
def myItem = MyItem.findByName(request.payment.paymentItems.itemNumber)
new ItemPaymentDetails(payment:request.payment, MyItem:myItem).save()
}
afterView = { Exception e ->
}
}
}
}
This filter use to track that user clicked on buy. The payment record is created for the user.
Purchase Completed Filter
Create a Purchase Completed Filter in /conf/myfilters/PurchaseCompletedFilters.groovy.
Class PurchaseCompletedFilters {
def filters = {
all(controller: 'paypal', action: '(success|notifyPaypal)') {
before = {
}
after = { Map model ->
def payment = request.payment
if(payment && payment.status == org.grails.paypal.Payment.COMPLETE) {
def itemPaymentDetails = ItemPaymentDetails.findByPayment(payment)
if ( !itemPaymentDetails.isCompleted ) {
itemPaymentDetails.isCompleted = true
}
}
}
afterView = { Exception e ->
}
}
}
}
When the user paid the item, this filter will be invoked. The payment record is retrieved and can be used in your custom business logic. For example, we can update the isCompleted flag in our own transaction domain model.
For more info click here
THANKS
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Santosh Singh
Santosh is a seasoned software developer working on JAVA, J2EE, Spring, Grails, HTML, JavaScript, jQuery, Ajax, AngularJS, SQL, developed 3+ applications. He loves to play soccer and reading books in free time.