Skip to content

Reference Transaction Payment

nov matake edited this page Aug 9, 2011 · 2 revisions

Reference Transaction Payment

Official PayPal API document about Reference Transaction is here.
https://cms.paypal.com/cms_content/US/en_US/files/developer/PP_EnterpriseExpressCheckout_AdvancedFeaturesGuide.pdf

To use this feature, you need to make contract with PayPal.
Contact to PayPal for details.

Setup transaction

Call SetExpressCheckout using this code and let user redirect to the given redirect URI.

request = Paypal::Express::Request.new(
  :username   => SET_YOUR_OWN,
  :password   => SET_YOUR_OWN,
  :signature  => SET_YOUR_OWN
)
payment_request = Paypal::Payment::Request.new(
  :billing_type  => :MerchantInitiatedBilling,
  # Or ":billing_type => :MerchantInitiatedBillingSingleAgreement"
  # Read official document for details
  :billing_agreement_description => SET_YOUR_OWN
)
response = request.setup(
  payment_request,
  YOUR_SUCCESS_CALBACK_URL,
  YOUR_CANCEL_CALBACK_URL
)
response.redirect_uri

Assume the end-user approved the payment request on PayPal.com and redirect back to your site.
In the redirect back request, you get token in query string.

Create Reference Transaction Agreement

Call CreateBillingAgreement using this code.

response = request.agree! token, profile
# inspect this attribute for more details
response.reference
response.reference.identifier # => reference_id

Get agreement details

You can get reference transaction agreement details anytime.
Call BillAgreementUpdate using this code.

NOTE: The API method is BillAgreementUpdate, but actually it is just Fetching.

response = request.agreement reference_id
# inspect this attribute for more details
response.reference

Charge

Once you got a billing agreement (represented by reference_id), you can charge money anytime.
Since there are no “Schedule” which Recurring payment has, you need to call DoReferenceTransaction each time you charge.

request.charge! reference_id, 100 # Charge $100
request.charge! reference_id, 100, :currency_code => :JPN # Charge ¥100

Revoke agreement

Call BillAgreementUpdate using this code.

NOTE: Here, it is actually Updating status as Canceled.

request.revoke! reference_id