Paypal Pro
Dependency :
compile ":paypal-pro:0.2"
Summary
Installation
grails install-plugin paypal-pro
Description
Paypal Pro Plugin
Overview
This plugin provides provided seemless integration with papal web payment pro. This allows you to take payment from your customers through your website while accepting payment through the backend with paypal. You can:Take one off payments
Setup recurring payments
Author: Peter Delahunty
blog: http://blog.peterdelahunty.com
twitter: http://twitter.com/peterdelahunty
email: peter.delahunty@solution51.com
website: www.solution51.com
Compatibility
The plugin is currently compatible with version 1.1.x of grails.Installation
Installation is very simple as is all grails plugin installations;grails install-plugin paypal-pro
Configuration
You need to specify your paypal web payments pro account credentials. You setup a sandbox for developement and live for productionenvironments {
development { paypalPro {
username = "sandbox@tester.com";
password = "1242591648";
signature = "kmksmkmskdsmfkmksmdkfskdfk";
environment = "sandbox";
}
} production {
paypalPro {
username = "liveusername@tester.com";
password = "123456";
signature = "kkmkmkmkmkmkm";
environment = "live";
} }}Documentation
The Paypal Pro plugin integrates into your current project as a service. It provides the following artefactsNew Domain objects
PayPalBilling
This object represents and instance of a Paypal recurring billing agreement. These are the properties requried to be filled by you.referenceId
This is used by you to keep any reference data back to your domain. It is stored in paypaldescription
This is description of what the recurring billing agreement is forinitialAmount
This is the amount you want to charge the customer for their first paymentamount
Thi is the on going amount you want to charge the customer.currencyCode;
This is the currency code of the the amounts you want to charge. It must be a paypal supported currency. eg USD for us dollars.PayPalBillingHistory
This object represents and instance of a change to a paypal recurring billing agreement. The changes possible are: START,CANCEL,SUSPEND,REACTIVATEPayPalPayment
This object represents and instance of an adhoc Paypal transaction.description
This is description of what the recurring billing agreement is foramount
Thi is the amount you want to charge the customer.currencyCode;
This is the currency code of the the amounts you want to charge. It must be a paypal supported currency. eg USD for us dollars.New Command Object
PayPalPaymentDetailsCommand
This object represents the information that a customer needs to enter in a form to take payment via a credit card. I has the following attributes:cardType
This can be one of these values: (Visa, MasterCard, Discover, Amex, Maestro, Solo)cardNumber
This is the credit or debit card numbercardEndMonth
This is the expiry month of the cardcardEndYear
This is the expriy year of the cardcardStartMonth
This is the start month of the cardcardStartYear
This is the start year of the cardcardIssueNumber
This is the date the card was issuedcardVerificationValue
This the card CVV number on the back of the card on the signature stripfirstName
This is the frst name of the card holderlastName
This is the last name of the card holderstreet
This is number and street address of the cards billing addresscity
This is the city of the cards billing addressstate
This is the state of the cards billing address (opitonal)zip
This is zip or post code of the cards billing addresscountryCode;
This is country code of the cards billing addressdescription
This is a description of this transactionipAddress
This is the ip address of the customerNew Service
PayPalProService
The PayPalProService has the following method for making payments with Paypal web payments pro.createMonthlyRecurringPayment
This method takes a PayPalBilling object and a PayPalPaymentDetailsCommand object and creates a paypal recurring billing agreementcancelMonthlyRecurringPayment
This method takes a PayPalBilling cancels a paypal recurring billing agreementdoSinglePaymentSale
This method takes a PayPalPayment object and a PayPalPaymentDetailsCommand object and creates a paypal transactionTag libs for payment form
I have also created several tag libs for creating the payment form.cardTypeSelect (select box)
This example uses the cardTypeSelect to show the cards. I only show credit cards due to the cardType parameter<paypalpro:cardTypeSelect name="cardType" value="${paymentDetails?.cardType}" noSelection="['':'-Choose your Card Type-']" tabindex="1" cardTypes="['Visa','MasterCard','Amex','Discover']" />
countryCodeSelect (select box)
This tag renders a list of all countries with the country code as a valid paypal code<paypalpro:countrySelect class="sb" id="countryCode" name="countryCode" value="${paymentDetails?.countryCode}" noSelection="['':'-Choose your Country-']" tabindex="16"/>
monthSelect
Here the tag is used for the cardStartMonth<paypalpro:monthSelect name="cardStartMonth" value="${paymentDetails?.cardStartMonth}" noSelection="['':'Month']" tabindex="3"/>
yearSelect
Here the tag is used for the cardStartYear<paypalpro:startYearSelect name="cardStartYear" value="${paymentDetails?.cardStartYear}" noSelection="['':'Year']" tabindex="4"/>
Examples:
Example controller to take payment
This controller assumes you have submitted a payment for with fields that map to the fields on the PayPalPaymentDetailsCommand object. It is passed to the controller as a closure parameter.def takePayment = {PayPalPaymentDetailsCommand paymentDetailsCommand ->if (paymentDetailsCommand.hasErrors()) {
render(view: 'enterPayment',
model: [paymentDetailsCommand : paymentDetailsCommand])
} else {// call your service to take payment using the PayPalProService}Handling errors in your view
You can ues the renderErrors errors tag on the paymentDetailsCommand to see standard grails constraints error messages. You can also use paypalpro:hasPaymentErrors and paypalpro:renderPaymentErrors tags from the plugin to see the error message that came back from paypal. Such as invalid credit card etc.<g:if test="${flash.message}"> <div class="errors"> <ul> <li>${flash.message}</li> </ul> </div> </g:if><g:hasErrors model="[paymentDetailsCommand: paymentDetailsCommand]"> <div class="errors"> <g:renderErrors model="[paymentDetailsCommand: paymentDetailsCommand]" as="list"/> </div> </g:hasErrors><paypalpro:hasPaymentErrors bean="${paymentDetailsCommand}"> <div class="errors"> <paypalpro:renderPaymentErrors bean="${paymentDetailsCommand}" as="list"/> </div> </paypalpro:hasPaymentErrors>
Setting up a monthly billing via your service
In this example i have subscription object that has a price that i pass to the PayPalBilling object. I also use the user id as the reference id. If the payment is succcessful then the paypal recurring billing profile id will be stored the profile id column of the PayPalBilling table.class MyPaymentService {def payPalProServicedef setupMonthlyRecurringPayment(
PayPalPaymentDetailsCommand paymentDetailsCommand,
User user, Subscription subscription) { PayPalBilling billing = new PayPalBilling()
billing.amount = subscription.price;
billing.initialAmount = subscription.price
billing.currencyCode = "USD"
billing.referenceId = "$user.id"
billing.description = user.email if (payPalProService.createMonthlyRecurringPayment(
paymentDetailsCommand, billing)) { // on success the billing object has been save to the db
user.billing
user.save() }else{
// failed to take payment so return the view to show errors
}Taking up on of payment via your service
You just need to setup a PayPalPayment object and pass it to the PayPalProSerive to take payment. On a successful payment the paypal transaction id is stored in the PayPalPayment table.class MyPaymentService {def payPalProServicedef buyProduct(PayPalPaymentDetailsCommand paymentDetailsCommand,
User user,
Product product) { Double amount = product.price; PayPalPayment payPalPayment = new PayPalPayment()
payPalPayment.amount = amount
payPalPayment.currencyCode = "USD"
payPalPayment.description = user.email + " purchased " + product.id if (payPalProService.doSinglePaymentSale(paymentDetailsCommand, payPalPayment)) { // do something after successful payment }else{ // failed to take payment so return the view to show errors }
....