簡體   English   中英

如何獲取 Paypal 交易 ID 並在 ruby​​ on rails 中退款

[英]How to get Paypal Transaction id and make refunds in ruby on rails

我正在使用:Ruby 2.4、Rails 5.2.1、沙盒模式下的 Paypal:

 gem 'paypal-sdk-rest', '~> 1.7.3'

貝寶付款為:

  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
                                             }]
                       })

付款創建為:

 if @payment.create
      render json: {success: true, paymentID: @payment.id}
    else
      @payment.error # Error Hash
      render json: {success: false}
    end

付款捕獲:

 if payment.execute(payer_id: params[:payerID])
      render json: {msg: 'payment_completed', result: 'completed', }
    else
      render json: {msg: payment.error, result: 'failed'}
    end

退款:第1步:從paypal找到paypal付款對象:

payment = PayPal::SDK::REST::Payment.find(order.paypal_id)

第 2 步:找到該付款的交易:

transaction = payment.transactions.last

Step2:找到與該付款相關的資源:

related_resource = transaction.related_resources.last

第 3 步:找到銷售對象表單 paypal sdk:

sale = related_resource.sale
sale = Sale.find(sale.id)

現在繼續退款:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM