Reputation: 5901
I am using: Ruby 2.4, Rails 5.2.1, Paypal in sandbox mode:
gem 'paypal-sdk-rest', '~> 1.7.3'
Paypal payment as:
items << {
:name => title,
:price => unit_price,
:currency => "USD",
:quantity => quantity,
:description => short_description
}
amount = {
:total => subtotal,
:currency => "USD",
:details => {"subtotal": subtotal, "shipping": "0", "tax": "0"}
}
invoice_number = "invoice-#{rand(1111..9999)}"
@payment = Payment.new({
:intent => "sale",
:payer => {
:payment_method => "paypal"},
:redirect_urls => {
:return_url => "http://localhost:3000/payments/#{order.id}/make_payment",
:cancel_url => "http://localhost:3000/"},
:transactions => [{
:item_list => {
:items => items,
},
:amount => amount,
:description => "Transaction description.",
:invoice_number => invoice_number
}]
})
Payment create as :
if @payment.create
render json: {success: true, paymentID: @payment.id}
else
@payment.error # Error Hash
render json: {success: false}
end
Payment capture:
if payment.execute(payer_id: params[:payerID])
render json: {msg: 'payment_completed', result: 'completed', }
else
render json: {msg: payment.error, result: 'failed'}
end
Upvotes: 1
Views: 380
Reputation: 5901
Refund : step 1: find paypal payment object from paypal:
payment = PayPal::SDK::REST::Payment.find(order.paypal_id)
step 2: find the transaction to that payment:
transaction = payment.transactions.last
Step2: find related resources to that payment:
related_resource = transaction.related_resources.last
step 3: find the sale object form paypal sdk:
sale = related_resource.sale
sale = Sale.find(sale.id)
Now proceed with refunds:
refund = sale.refund({
:amount => {
:total => "1.31",
:currency => "USD"
}
})
if refund.success?
logger.info "Refund[#{refund.id}] Success"
render json: { result: 'success '}
else
logger.error refund.error.inspect
render json: { result: 'fail'}
end
Upvotes: 1