PayPal Payment¶ ↑
PayPal API Client for Adaptive Payments, Invoice and Express Checkout.
Installation (Gemfile)¶ ↑
gem 'paypal-payment', :git => 'https://github.com/chardy/paypal-payment.git'
Usage¶ ↑
First, you need to set up your credentials:
require "paypal-payment" PayPal::Api.configure do |config| config.sandbox = true config.username = "chardy_api1.gmail.com" config.password = "8DTSWBZC7GDR3T4X" config.signature = "AG-8mOpuFhFyFOYHlaTUYn3Syf15AWJKRnfHMVsmCtC3DK51-ENEPqLS" end
Adaptive Payments¶ ↑
You can request a new preapproval authorization:
pa = PayPal::AdaptivePayments::Preapproval.new( :client_details => { :ip_address => "234.8.8.18", :device_id => "123456", :application_id => "PP" }, :starting_date => Date.today, :currency_code => 'USD', :max_total_amount_of_all_payments => 2000.0, :cancel_url => 'http://yoursite.com/preapproval/cancel?preapprovalkey=${preapprovalkey}', :return_url => 'http://yoursite.com/preapproval/accepted?preapprovalkey=${preapprovalkey}' ) response = pa.create puts response.preapproval_url if response.valid?
You need to redirect your user to the url returned by response.approval_url
. After the user accepts or rejects your preapproval request, he will be redirected to one of those urls you specified. Note that you need to append “preapprovalkey=${preapprovalkey}” to the urls if you want PayPal to redirect with preapprovalkey You can use the preapprovalkey
parameter to query for the approval details.
If you need to retrieve information about your buyer, like address or e-mail, you can use the details()
method.
pa = PayPal::AdaptivePayments::Preapproval.new(:preapproval_key => "PA-89578182UL033944H") response = pa.details
When you need to request payment, create a new Payment with the preapproval key.
pp = PayPal::AdaptivePayments::Payment.new( :preapproval_key => "PA-89578182UL033944H", :receiver => {:email => "seller@example.com", :amount => 10.0}, :currency_code => 'USD', :cancel_url => 'http://example.com/cancel', :return_url => 'http://example.com/thank_you' ) response = pp.pay response.success? response.completed?
You can manage your preapproval.
pa = PayPal::AdaptivePayments::Preapproval.new(:preapproval_key => "PA-89578182UL033944H") pa.cancel
What information do I need to keep?¶ ↑
You should save two paramaters to your database: TOKEN
and PROFILEID
. TOKEN
is required when user returns to your website after he authorizes (or not) the billing process. You need to save it so you can find him later. You can remove this info after payment and recurring profile are set.
The PROFILEID
allows you to manage the recurring profile, like cancelling billing when an user don’t want to use your service anymore.
NOTE: TOKEN will expire after approximately 3 hours.
Maintainer¶ ↑
-
Chardy Wang
License¶ ↑
(The MIT License)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.