正确的方式来链接的PayPal帐户与布伦特里DropUI至库(Correct way to link

2019-10-23 12:47发布

我有以下控制器:

class PaymentsController < ApplicationController
    before_action :authenticate_user!, only: [:new]

    def new
        gon.client_token = generate_client_token
    end

    def create
        @result = Braintree::Transaction.sale(
                            amount: 60,
                            payment_method_nonce: params[:payment_method_nonce])
        if @result.success?
            puts @result.transaction.payment_instrument_type
            flash[:notice] = 'Yes, transaction completed'
        else
            flash[:alert] = "Something went wrong while processing your transaction. Please try again!"
      gon.client_token = generate_client_token
            render :new
        end
    end

    private

    def generate_client_token
        Braintree::ClientToken.generate(customer_id: current_user.braintree_customer_id)
    end
end

与上面的代码,我用PayPal帐户链接到库为特定客户测试。 但是它不会链接。 我查了一下,在我看来它不应该有任何额外的调整文件的参考。

任何提示?

Answer 1:

我的工作是为布伦特里使用python /的JavaScript API开发。 您需要创建braintree token的transaction.If你可以生成布伦特里令牌CUSTOMER_ID。 你可以按照这个代码来创建令牌:

def checkout(request):
    api_token = request.session['token']
    nonce = request.POST['payment_method_nonce']
    customer = Customer.objects.get(user=request.user)
    # With this the PaymentMethod will be associated with the Customer
    result = braintree.PaymentMethod.create({
        "customer_id": customer.customer_id,
        "payment_method_nonce": nonce
    })
    token = result.payment_method.token
    print token
    # you the BT token to charge the user
    result = braintree.Transaction.sale({
        "amount": 10,
        "payment_method_token": token,
        "options": {'submit_for_settlement': True}
    })
    print result.transaction.id


文章来源: Correct way to link PayPal Accounts to vault with Braintree DropUI