6

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": "jen@example.com",
    "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"
})
mark
  • 653
  • 1
  • 10
  • 23

2 Answers2

7

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"
})
agf
  • 171,228
  • 44
  • 289
  • 238
  • @AwaisMustafa Yes, create the subscription with the payment method token for the PayPal account. – agf Nov 29 '18 at 04:34
  • @agf Can you please tell me how we can get token frim paypal acc ? – Sachin Shah Oct 11 '19 at 10:30
  • 1
    @SachinShah Please reach out to Braintree support with your question, but it's basically the same except replace the `credit_cards` bit. – agf Oct 12 '19 at 04:01
  • It took me forever to find something which made sense for the value of payment_method_toke. Why can't they just call it payment_method_id – Bogdan M. Nov 19 '19 at 08:43
  • 1
    @BogdanM. In general, the distinction is that `_token`s can be set by the merchant via the API while `_id`s can't be. – agf Nov 20 '19 at 00:31
  • oh, touche. Thanks it does make sense now :) – Bogdan M. Nov 20 '19 at 07:42
0

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

Avin Mathew
  • 336
  • 11
  • 25