Recurring Payment

Simon Hürlimann edited this page Sep 26, 2013 · 8 revisions

Recurring Payment

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(
  :currency_code => :JPY, # if nil, PayPal use USD as default
  :billing_type  => :RecurringPayments,
  :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 Recurring Profile

Call CreateRecurringPaymentsProfile using this code. Ensure the description matches the description in the PaymentRequest.

profile = Paypal::Payment::Recurring.new(
  :start_date => SET_YOUR_OWN,
  :description => SET_YOUR_OWN,
  :billing => {
    :period        => SET_YOUR_OWN, # ie.) :Month, :Week
    :frequency     => SET_YOUR_OWN,
    :amount        => SET_YOUR_OWN,
    :currency_code => :JPY # if nil, PayPal use USD as default
  }
)
response = request.subscribe!(token, profile)
# inspect this attribute for more details
response.recurring
response.recurring.identifier # => profile_id

Get subscription details

You can get recurring profile details anytime.
Call GetRecurringPaymentsProfileDetails using this code.

response = request.subscription(profile_id)
# inspect this attribute for more details
response.recurring

Cancel (unsubscribe) / Suspend / Reactivate profile

Call ManageRecurringPaymentsProfileStatus using this code.

request.renew!(profile_id, :Cancel)
request.renew!(profile_id, :Suspend)
request.renew!(profile_id, :Reactivate)