如何获得Paypal交易ID并在轨道上的ruby中退款

问题描述 投票:1回答:1

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

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

Paypal付款方式为:

  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
ruby-on-rails ruby paypal paypal-sandbox paypal-rest-sdk
1个回答
1
投票

退款:第1步:从paypal中找到paypal付款对象:

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

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

transaction = payment.transactions.last

第2步:找到该付款的相关资源:

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
© www.soinside.com 2019 - 2024. All rights reserved.