Paypal gives a vast support for online payment, everything is been documented in nice way but we have to Google for better results for your payment to get in your control(other than standard payments).

There are two API’s provided by pay pal:

  1. Rest(

    Doc’s)

  2. Classic(

    Doc’s)

The classic contains many product listed in the doc. From that list i am explaining the recurring payment.

First we need merchant api. Simply add below codes in your build config of Grails project.

compile ('com.paypal.sdk:merchantsdk:2.5.106'){
excludes "commons-codec"
}

Need a developers account. (Create here)

Details of code can be found at  Dev tools

Codes are below with explanations.

RecurringPaymentsProfileDetailsType profileDetails = new RecurringPaymentsProfileDetailsType(Start-date);

Recurring profile start date is added here. When the date of recurring starts the first payment is made by paypal.

BasicAmountType paymentAmount = new BasicAmountType(CurrencyCodeType.USD, "1.0");
BillingPeriodType period = BillingPeriodType.fromValue("Day");
int frequency = 10;
BillingPeriodDetailsType paymentPeriod = new BillingPeriodDetailsType(period, frequency, paymentAmount);

In billing period details we need to add the time period can be day, month or year, the frequency i.e. the number of occurrence of the time period and last the payment amount.

ScheduleDetailsType scheduleDetails = new ScheduleDetailsType();
scheduleDetails.setDescription("recurring billing");
scheduleDetails.setPaymentPeriod(paymentPeriod);

Simply adding the description for what the payment is about and setting the period.

CreditCardDetailsType creditCard = new CreditCardDetailsType();
creditCard.setCreditCardNumber("4745425765192217");
creditCard.setCVV2("962");
creditCard.setExpMonth(03);
creditCard.setExpYear(2015);
creditCard.setCreditCardType(CreditCardTypeType.fromValue("Visa"));

Adding the valid credentials. Set proper card type, I have set for only visa here .

PayerInfoType payerInfoType=new PayerInfoType()
PersonNameType p=new PersonNameType()
p.setFirstName(cardholderName)
payerInfoType.setPayerName(p)
creditCard.setCardOwner(payerInfoType)

The above codes set the payer name. These are not in the actual dev.tool documents.

CreateRecurringPaymentsProfileRequestDetailsType createRPProfileRequestDetails = new CreateRecurringPaymentsProfileRequestDetailsType(profileDetails, scheduleDetails);
createRPProfileRequestDetails.setCreditCard(creditCard);

From here the profile is created.

CreateRecurringPaymentsProfileRequestType createRPProfileRequest = new CreateRecurringPaymentsProfileRequestType();
createRPProfileRequest.setCreateRecurringPaymentsProfileRequestDetails(createRPProfileRequestDetails);
CreateRecurringPaymentsProfileReq createRPPProfileReq = new CreateRecurringPaymentsProfileReq();
createRPPProfileReq.setCreateRecurringPaymentsProfileRequest(createRPProfileRequest);

Now buyers information is set. We need to set the sellers account credentials. Which van be easily found at developer.paypal.com

Map<String, String> sdkConfig = new HashMap<String, String>();
sdkConfig.put("mode", "sandbox");
sdkConfig.put("acct1.UserName", "jb-us-seller_api1.paypal.com");
sdkConfig.put("acct1.Password", "WX4WTU3S8MY44S7F");
sdkConfig.put("acct1.Signature","AFcWxV21C7fd0v3bYYYRCpSSRl31A7yDhhsPUU2XhtMoZXsWHFxu-RWy");
PayPalAPIInterfaceServiceService service = new PayPalAPIInterfaceServiceService(sdkConfig);
CreateRecurringPaymentsProfileResponseType createRPProfileResponse = service.createRecurringPaymentsProfile(createRPPProfileReq);

These configurations are easy but i am explaning them also, first add the mode(Remember that sandbox mode is only user for testing and development, actual payment is done in live mode).

For username password and signature go to this link and open the facilitator profile and switch tab to API credentials.

Paypal responses as JSON


PROFILEID=I-X5626H5BY8A5 & PROFILESTATUS=ActiveProfile & TIMESTAMP=2015-03-29T16:37:00Z & CORRELATIONID=36742ed03cdb6 & ACK=Success & VERSION=104.0 & BUILD=15840636

Paypal generates a recurring profile id which comes in response with the acknowledgement.

After the code cooking is done start testing them. 😉