问题
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