mark
mark

Reputation: 673

Payment method token is invalid in Braintree

I want to test it on the side of the functionality of the subscription payment gateway Braintree - for my application using Django (python).

Your code I have only one py file. (without front-end). When I want to create a subscription I get an error:

<ErrorResult 'Payment method token is invalid.' at 7f101d301390>

How can I get token payment method?

Here is all my code:

import braintree

braintree.Configuration.configure(braintree.Environment.Sandbox,
                                  merchant_id="myMechrantId",
                                  public_key="myPublicKey",
                                  private_key="myPrivateKey")

client_token = braintree.ClientToken.generate()

client = braintree.Customer.create({
    "first_name": 'Mike',
    "last_name": "Smith",
    "company": "Braintree",
    "email": "[email protected]",
    "phone": "312.555.1234",
    "fax": "614.555.5678",
    "website": "www.example.com"
})

result = braintree.Subscription.create({
    "payment_method_token": "the_token",
    "plan_id": "Here is my plan ID"
})

Upvotes: 7

Views: 6040

Answers (2)

Avin Mathew
Avin Mathew

Reputation: 542

It's late but I got stuck on this problem recently, this is the solution that helped me

customer_create_result = gateway.customer.create({
        "first_name": user.first_name,
        "last_name": user.middle_name + '' + user.last_name,
        "payment_method_nonce": payment_method_nonce
    })
subscription_create_result = gateway.subscription.create({
        "payment_method_token":
        customer_create_result.customer.payment_methods[0].token,
        "plan_id": braintree_plan_id
    })

where payment_method_nonce can be obtained from payload.nonce on payment button click and braintree_plan_id is the id for the plan you create in braintree control panel

here is the braintree reference that helped me https://developer.paypal.com/braintree/docs/guides/customers#create-with-payment-method

Upvotes: 0

agf
agf

Reputation: 176950

I work at Braintree. Please get in touch with our support team if you have more questions.

In general, one of the main benefits of a service like Braintree is that you never have to handle credit card numbers, so you're better off following the Braintree recurring billing guide instead, which will better match a real integration with Braintree.

That said, if you do want to test it without a front-end, you can test it like this:

result = braintree.Customer.create({
    "credit_card": {
        "number": "4111111111111111",
        "expiration_date": "12/16"
    }
})

result = braintree.Subscription.create({
    "payment_method_token": result.customer.credit_cards[0].token,
    "plan_id": "my_plan_id"
})

Upvotes: 7

Related Questions