我有以下控制器:
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帐户链接到特定客户的保险库。然而,它不会链接。我检查了文档参考,在我看来它不应该是任何额外的调整。
任何提示?
答案 0 :(得分:0)
我使用python / javascript开发了用于api开发的braintree。您需要为事务创建braintree token
。如果您有customer_id,则可以生成braintree令牌。您可以按照此代码创建令牌:
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