ruby 返回确认从贝宝API辛纳屈

qncylg1j  于 2023-05-17  发布在  Ruby
关注(0)|答案(1)|浏览(127)

我们正在创建一个贝宝快速结帐业务使用辛纳屈。我需要能够得到的东西返回形式的API给我确认,订单付款已经通过。我想使用这个方法将一个“PAID”值传递到一个与客户匹配的数据库中。这是设置我的paypal快速结帐的代码(在shopping_cart.erb中):

<script>
  paypal.Button.render({
    env: 'sandbox', // sandbox | production
    // PayPal Client IDs - replace with your own
    // Create a PayPal app: https://developer.paypal.com/developer/applications/create
    client: {
      sandbox: '',
      production: ''
    },
    // Show the buyer a 'Pay Now' button in the checkout flow
    commit: true,
    // payment() is called when the button is clicked
    payment: function(data, actions) {
      // Make a call to the REST api to create the payment
      return actions.payment.create({
        //    curl -v -X POST "https://api.sandbox.paypal.com/v1/payments/payment" \
        //     -H "Content-Type:application/json" \
        //     -H "Authorization: Bearer <Access-Token>" \
        //     -d '{"intent": "sale", "payer": {"payment_method": "paypal"} },
        transactions: [
          {amount: { total: '0.01', currency: 'USD' }}
        ],
        redirect_urls: {
          "return_url": "http://www.paypal.com/return",
          "cancel_url": "http://www.paypal.com/cancel"
        }
      });
    },

    // onAuthorize() is called when the buyer approves the payment
    onAuthorize: function(data, actions) {
      // Make a call to the REST api to execute the payment
      return actions.payment.execute().then(function() {
        window.alert('Payment Complete!');
        window.location.href = "../receipt";
        document.getElementbyId("paypal-button-container").innerHTML = "<input type='hidden' name='clicked' value='paid'>"
      });
    }
  }, '#paypal-button-container');

  function endClicked(){
    document.getElementbyId("paypal-button-container").innerHTML = "<input type='hidden' name='clicked' value='paid'>"
  }
</script>

我如何得到一个确认,然后传递一些东西给数据库?下面是我的`app.rb处理它的部分:

post '/receipt' do
  @title = 'Receipt'
  campaign_name = params[:campaign_name]
  user = session[:user]
  cart = session[:cart]
  puts "this is the receipt"
  db.exec("UPDATE orders SET payment_status = 'paid' WHERE campaign_name = '#{campaign_name}' AND customer_name = '#{user}'")
  erb :receipt, :locals => {
    :cart => session[:cart],
    :campaign_name => session[:campaign_name],
    :user => session[:user],
    :message =>"Thanks for your order, here is a receipt you can print for your records."
  }
end

get '/receipt' do
  @title = 'Receipt'
  campaign_name = params[:campaign_name]
  user = session[:user]
  cart = session[:cart]
  clicked = params[:clicked]
  puts "this is clicked '#{clicked}'"
  # if clicked != ""
  #   redirect '/receipt'
  # end

  erb :receipt,:locals => {
    :cart => session[:cart],
    :campaign_name => session[:campaign_name],
    :clicked => clicked,
    :user => session[:user],
    :message =>"Thanks for your order, here is a receipt you can print for your records."
  }
end
kx5bkwkv

kx5bkwkv1#

调用捕获API后,您应该会收到一个包含付款状态的响应。它会告诉你你的付款是否完成。

相关问题