Created
May 13, 2013 13:50
-
-
Save SeraphimSerapis/5568431 to your computer and use it in GitHub Desktop.
Verifies a payment that is being generated with PayPal's Mobile SDKs.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'paypal-sdk-rest' | |
require 'sinatra' | |
include PayPal::SDK::REST | |
get '/verify/:payment_id/:total/:currency' do | |
payment_id = params[:payment_id] | |
total = params[:total] | |
currency = params[:currency] | |
begin | |
payment = Payment.find(payment_id) | |
if payment.state == 'approved' | |
transaction = payment.transactions.find{ |txn| txn.amount.total == total && txn.amount.currency == currency } | |
if transaction | |
sale = transaction.related_resources.find{ |res| res.sale.state == 'completed' } | |
if sale | |
"OK" | |
else | |
"ERROR - no sale" | |
end | |
else | |
"ERROR - no transaction" | |
end | |
else | |
"ERROR - wrong state" | |
end | |
rescue | |
"ERROR - no payment" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment