【问题标题】:How to implement Paypal payouts with paypal smart button integration in rails如何在 Rails 中使用 Paypal 智能按钮集成实现 Paypal 支付
【发布时间】:2020-11-11 05:42:54
【问题描述】:

我通过使用SmartButtons 并在server-side 中创建订单,在我的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>

orders_controller.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 可用于它,但我很困惑在哪里安装此代码以及如何将其与前端集成。有任何想法吗?在此先感谢:)

【问题讨论】:

    标签: ruby-on-rails paypal paypal-rest-sdk


    【解决方案1】:

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

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

    Payouts 是一种严格的后端 API 操作,您可以在其中将钱从您的帐户发送到另一个帐户。

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

    【讨论】:

    • 感谢您的回复...在这里我有一个用户可以购买课程的门户网站,我必须将钱转给课程所有者,并向管理员支付 10% 的佣金我应该实施什么在这种情况下?
    • 谁是在其帐户上启用了付款的人?管理员?如果他们向管理员支付 100% 的费用,那么管理员稍后可以向所有者支付 90% 的费用。
    • 管理员是启用付款的人,是的,我已使用结帐将 100% 支付给管理员,管理员将使用付款将 90% 发送给课程所有者。方法对吗?
    猜你喜欢
    • 2021-07-14
    • 2021-01-28
    • 2020-03-23
    • 2020-08-05
    • 2020-08-13
    • 1970-01-01
    • 2020-05-25
    • 2020-10-16
    • 2019-11-16
    相关资源
    最近更新 更多