Skip to content

Instantly share code, notes, and snippets.

@SeraphimSerapis
Created May 13, 2013 13:50
Show Gist options
  • Save SeraphimSerapis/5568431 to your computer and use it in GitHub Desktop.
Save SeraphimSerapis/5568431 to your computer and use it in GitHub Desktop.
Verifies a payment that is being generated with PayPal's Mobile SDKs.
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