繁体   English   中英

如何在导轨中集成 paypal 智能按钮来实现 Paypal 支出

[英]How to implement Paypal payouts with paypal smart button integration in rails

我通过使用SmartButtons并在服务器端创建订单,在我的 rails 应用程序中实现了 PayPal 结帐 API。

我使用了payouts-ruby-sdk gem,我的代码如下:-

index.html.erb
<!-- Set up a container element for the button -->
<div id="paypal-button-container"></div>

<!-- Include the PayPal JavaScript SDK -->
<script src="https://www.paypal.com/sdk/js?client-id=xyz&currency=USD"></script>

<script>
    // Render the PayPal button into #paypal-button-container
    paypal.Buttons({

        // Call your server to set up the transaction
        createOrder: function(data, actions) {
            return fetch('/orders', {
                method: 'post'
            }).then(function(res) {
                return res.json();
            }).then(function(orderData) {
                return orderData.orderID;
            });
        },

        // Call your server to finalize the transaction
        onApprove: function(data, actions) {
            return fetch('/orders/' + data.orderID + '/capture', {
                method: 'post'
            }).then(function(res) {
                return res.json();
            }).then(function(orderData) {
                // Three cases to handle:
                //   (1) Recoverable INSTRUMENT_DECLINED -> call actions.restart()
                //   (2) Other non-recoverable errors -> Show a failure message
                //   (3) Successful transaction -> Show a success / thank you message

                // Your server defines the structure of 'orderData', which may differ
                var errorDetail = Array.isArray(orderData.details) && orderData.details[0];

                if (errorDetail && errorDetail.issue === 'INSTRUMENT_DECLINED') {
                    // Recoverable state, see: "Handle Funding Failures"
                    // https://developer.paypal.com/docs/checkout/integration-features/funding-failure/
                    return actions.restart();
                }

                if (errorDetail) {
                    var msg = 'Sorry, your transaction could not be processed.';
                    if (errorDetail.description) msg += '\n\n' + errorDetail.description;
                    if (orderData.debug_id) msg += ' (' + orderData.debug_id + ')';
                    // Show a failure message
                    return alert(msg);
                }

                // Show a success message to the buyer
                alert('Transaction completed');
            });
        }


    }).render('#paypal-button-container');
</script>

订单控制器.rb

class OrdersController < ApplicationController
   skip_before_action :verify_authenticity_token

  def index
  end

  def create
    # Creating Access Token for Sandbox
        client_id =  'xyz'
      client_secret = 'abc'
        # Creating an environment
        environment = PayPal::SandboxEnvironment.new(client_id, client_secret)
        client = PayPal::PayPalHttpClient.new(environment)

        request = PayPalCheckoutSdk::Orders::OrdersCreateRequest::new
        request.request_body({
                                intent: "CAPTURE",
                                purchase_units: [
                                    {
                                        amount: {
                                            currency_code: "USD",
                                            value: "10.00"
                                        }
                                    }
                                ]
                              })

        begin
        # Call API with your client and get a response for your call
        # debugger
        response = client.execute(request)
        puts response.result.id
        render json: {success: true, orderID: response.result.id}
        rescue PayPalHttp::HttpError => ioe
            # Something went wrong server-side
            puts ioe.status_code
            puts ioe.headers["debug_id"]
        end
  end

  def execute_payment
    client_id =  'xyz'
    client_secret = 'abc'
        # Creating an environment
        environment = PayPal::SandboxEnvironment.new(client_id, client_secret)
        client = PayPal::PayPalHttpClient.new(environment)

      request = PayPalCheckoutSdk::Orders::OrdersCaptureRequest::new(session[:orderID])

        begin
            # Call API with your client and get a response for your call
            response = client.execute(request) 
            
            # If call returns body in response, you can get the deserialized version from the result attribute of the response
            order = response.result
            puts order
        rescue PayPalHttp::HttpError => ioe
            # Something went wrong server-side
            puts ioe.status_code
            puts ioe.headers["debug_id"]
        end
  end
end

现在我想实现 Paypal 的 Payouts API 并且我知道paypal-ruby-sdk可用于它,但我很困惑在哪里安装此代码以及如何将其与前端集成。 有任何想法吗? 提前致谢:)

您上面的代码是 Checkout 代码,用于前端 (JavaScript) 和后端 (Ruby)。

付款与 Checkout 无关,无论是前端 Checkout 还是后端 Checkout。

支付是严格的后端 API 操作,您可以将资金从您的帐户发送到另一个帐户。

付款不会连接到任何前端 UI。 如果需要,您可以构建自己的 UI 来触发付款。 大概您知道要从您的帐户向谁汇款,以及应该触发此操作的流程。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM