Paypal Adaptive Ruby Gem Released
I have been tinkering with the new Paypal Adaptive Payments API and created a simple ruby gem to interface with it. Still pretty new but I’m using it with little problems so far. Submit bug reports if found. See the code at github
Paypal Adaptive Payments API
The adaptive payments api gives you the opportunity to make preapproved payments, chained payments and parallel payments. The chained/parallel payments are great for commission-based apps or if you are trying to connect a buyer to multiple sellers with a single interface.
How to use with Rails:
Install:
sudo gem install paypal_adaptive
Setup your API info by adding a paypal_adaptive.yml to your config folder:
development:
environment: "sandbox"
username: "sandbox_username"
password: "sandbox_password"
signature: "sandbox_signature"
application_id: "sandbox_app_id"
test:
environment: "sandbox"
username: "sandbox_username"
password: "sandbox_password"
signature: "sandbox_signature"
application_id: "sandbox_app_id"
production:
environment: "production"
username: "my_production_username"
password: "my_production_password"
signature: "my_production_signature"
application_id: "my_production_app_id"
Make the payment request:
pay_request = PaypalAdaptive::Request.new
data = {
"returnUrl" => "http://testserver.com/payments/completed_payment_request",
"requestEnvelope" => {"errorLanguage" => "en_US"},
"currencyCode"=>"USD",
"receiverList"=>{"receiver"=>
[{"email"=>"testpp_1261697850_per@nextsprocket.com", "amount"=>"10.00"}]},
"cancelUrl"=>"http://testserver.com/payments/canceled_payment_request",
"actionType"=>"PAY",
"ipnNotificationUrl"=>"http://testserver.com/payments/ipn_notification"
}
pay_response = pay_request.pay(data)
if pay_response.success?
redirect_to pp_response.approve_paypal_payment_url
else
puts pay_response.errors.first['message']
redirect_to failed_payment_url
end
Once the user goes to pp_response.approve_paypal_payment_url, they will be prompted to login to Paypal for payment.
Upon payment completion page, they will be redirected to http://testserver.com/payments/completed_payment_request.
They can also click cancel to go to http://testserver.com/payments/canceled_payment_request
The actual payment details will be sent to your server via “ipnNotificationUrl” You have to create a listener to receive POST messages from paypal. I added a Rails metal template in the templates folder which handles the callback.
Additionally, you can make calls to Paypal Adaptive’s other APIs:
payment_details, preapproval, preapproval_details,
cancel_preapproval, convert_currency, refund
Input is just a Hash just like the pay method. Refer to the Paypal Adaptive manual for more details.