Last updated by bherrmann7 7 months ago
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 production
environments {
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 artefacts
New 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 paypal
description
This is description of what the recurring billing agreement is for
initialAmount
This is the amount you want to charge the customer for their first payment
amount
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,REACTIVATE
PayPalPayment
This object represents and instance of an adhoc Paypal transaction.
description
This is description of what the recurring billing agreement is for
amount
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 number
cardEndMonth
This is the expiry month of the card
cardEndYear
This is the expriy year of the card
cardStartMonth
This is the start month of the card
cardStartYear
This is the start year of the card
cardIssueNumber
This is the date the card was issued
cardVerificationValue
This the card CVV number on the back of the card on the signature strip
firstName
This is the frst name of the card holder
lastName
This is the last name of the card holder
email
This is the email of the card holder
street
This is number and street address of the cards billing address
city
This is the city of the cards billing address
state
This is the state of the cards billing address (opitonal)
zip
This is zip or post code of the cards billing address
countryCode;
This is country code of the cards billing address
description
This is a description of this transaction
ipAddress
This is the ip address of the customer
New 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 agreement
cancelMonthlyRecurringPayment
This method takes a PayPalBilling cancels a paypal recurring billing agreement
doSinglePaymentSale
This method takes a PayPalPayment object and a PayPalPaymentDetailsCommand object and creates a paypal transaction
Tag 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 }
....