Accept payments in Django - braintree and stripe

Gaurav Gopal Wagh
3 min readApr 5, 2016

--

This blog gives the idea about basic implementation of payment gateways in Django. I am going to cover two payment gateways here Braintree and Stripe

Braintree:

Braintree provides its own python package and client sdk. Install Server

SDK:

Install braintree package using pip.

pip install braintree

Install Client SDK:

Install it with npm

Install it just like you would any other npm module:

npm install --save braintree-web

Install it with Bower

Install it like you would any other Bower package:

bower install --save braintree-web

Use the direct link

You can also load braintree.js directly from here. Once loaded, braintree will be available on the global namespace.

<script src="https://js.braintreegateway.com/js/braintree-2.22.2.min.js"></script>

Modes:

For development purpose braintree provides the sandbox mode and for live on production it provides production mode.

A) Production:

braintree.Configuration.configure(braintree.Environment.Production,
merchant_id=settings.BRAINTREE_MERCHANT_ID,
public_key=settings.BRAINTREE_PUBLIC_KEY,
private_key=settings.BRAINTREE_PRIVATE_KEY)

B) Sandbox:

import braintree
braintree.Configuration.configure(braintree.Environment.Sandbox,
merchant_id=settings.BRAINTREE_MERCHANT_ID,
public_key=settings.BRAINTREE_PUBLIC_KEY,
private_key=settings.BRAINTREE_PRIVATE_KEY)

Brain-tree credentials settings:

in settings.py file provide following credentials

BRAINTREE_MERCHANT_ID ="3j2************y"
BRAINTREE_PUBLIC_KEY = "64z***********v2"
BRAINTREE_PRIVATE_KEY = "5507************************d78f"

Braintree provides two sdk’s.

A) Client SDK:

The client SDK enables us to collect payment method (e.g. credit card, PayPal) details.

B) Servr SDK:

The server SDKs manage all requests to the Braintree gateway

Braintree basically works on two key concepts

1. Client token:

Client token is basically the signed data generated by the server to authenticate application on client side. For each request new client token gets generated. Client is responsible for obtaining the client token and initializing the client sdk. If this succeeds, the client will generate a payment_method_nonce.

2. Payment method nonce:

Client token is basically the signed data generated by the server to authenticate application on client side. For each request new client token gets generated. Client is responsible for obtaining the client token and initializing the client sdk. If this succeeds, the client will generate a payment_method_nonce.

Payment Method example:

result = braintree.PaymentMethod.create({
“customer_id”: “12345”,
“payment_method_nonce”: nonce_from_the_client
})

Braintree client side dropin UI:

<form id=”checkout” method=”post” action=”/checkout”>
<div id=”payment-form”></div>
<input type=”submit” value=”Pay $10">
</form>
<script src=”https://js.braintreegateway.com/v2/braintree.js"></script>
<script>
// We generated a client token for you so you can test out this code
// immediately. In a production-ready integration, you will need to
// generate a client token on your server (see section below).
var clientToken = “client token’’;
braintree.setup(clientToken, “dropin”, {
container: “payment-form”
});
</script>

Braintree server side methods

  • Generate Client token:
client_token = braintree.ClientToken.generate({
“customer_id”: merchant_customer_id
})
  • Create new customer:
new_customer_result = braintree.Customer.create({
“email”: instance.email
})
  • Create transaction:
payment_method_result = braintree.Transaction.sale({
“customer_id”: merchant_customer_id,
“amount”: am,
“options”: {
“submit_for_settlement”: True
}
})
  • Success or error message after sale:
if not payment_method_result.is_success:
messages.error(request, “An error occured: %s” % (payment_method_result.message))
return redirect(“account_upgrade”)

Stripe:

An easy way to integrate Stripe is via Checkout. Checkout an embedded tool that takes care of building an HTML form, validating input, and securing your customers’ card data.

Using Checkout, sensitive credit card information is sent directly to Stripe, and does not touch on our server.

pip install stripe

Stripe returns a token to our site, representing the card, and this token can then be used in a charge request.

Stripe credentials settings:

in settings.py file provide following credentials

STRIPE_PUBLIC_KEY = os.environ.get("STRIPE_PUBLIC_KEY", "pk_test_e**************0H8")
STRIPE_SECRET_KEY = os.environ.get("STRIPE_SECRET_KEY", "sk_test_Sp**************z3")

Embedding Checkout in your site:

<form action="" method="POST">
<script src="https://checkout.stripe.com/checkout.js" class="stripe-button btn-danger"
data-key="pk_test_eVQIbobjI0DDN6Vk66F1k0H8"
data-amount="2000"
data-name="Demo Site"
data-description="Consultadd"
data-image="/media/img/128x128.png"
data-locale="auto">
</script>
</form>

The most important thing to notice is the data-key attribute added to the SCRIPT tag. This key identifies your account when communicating with Stripe.

With Stripe, sensitive cardholder data does not hit your server, greatly minimizing your PCI compliance burden.

Workflow of Stripe:-

  • The customer arrives at our payment page, loaded over HTTPs, that includes the Checkout code.
  • The customer clicks the payment button (e.g., “Pay with Card”), completes the payment form, and clicks “Pay $20.00” within the Checkout window.
  • Checkout sends the credit card details directly to Stripe from the customer’s browser, assuming the details pass basic validation.
  • Stripe returns a token to Checkout, or an error message if real validation fails.
  • Checkout takes the returned token and stores it in the page’s primary form — the one surrounding the SCRIPT tag above, in a hidden element named stripeToken.
  • Checkout submits the form to your server.
  • Your server uses the posted token to charge the card.

Create charge in Stripe:

import stripecharge = stripe.Charge.create(
amount=2000, # amount in cents, again
currency="usd",
source=token,
description="Example charge"
)

For stripe implementation i used djstripe as a third party module

View demo project on github , View live demo

--

--