Correct way to link PayPal Accounts to vault with Braintree DropUI

喜欢而已 提交于 2019-12-24 13:24:01

问题


I have the following controller:

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

With the above code, I was testing with linking a paypal account to the vault for a specific customer. However It won't link. I checked the documentation reference it seems to me it shouldn't be any extra tweak.

Any hint?


回答1:


I worked on the braintree for api development using python/javascript. You need to create the braintree token for the transaction.If you have the customer_id you can generate the braintree token. You can follow this code to create the token:

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


来源:https://stackoverflow.com/questions/31079792/correct-way-to-link-paypal-accounts-to-vault-with-braintree-dropui

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!