Created
April 3, 2015 18:23
-
-
Save pebreo/b451b74b05654c7599ce to your computer and use it in GitHub Desktop.
braintree-payment-example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<form id="checkout" method="post" action="{% url 'recurpayresult' %}"> | |
{% csrf_token %} | |
<input type="text" name="first_name"> | |
<input type="text" name="last_name"> | |
<div id="dropin"></div> | |
<input type="submit" value="Pay $25"> <!-- braintree will use this $25 payment --> | |
</form> | |
<script src="https://js.braintreegateway.com/v2/braintree.js"></script> | |
<script> | |
braintree.setup( | |
// Variable is inserted when template is rendered by the backend | |
"{{client_token}}", | |
'dropin', { | |
container: 'dropin' | |
}); | |
</script> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.conf.urls import patterns, include, url | |
from django.contrib import admin | |
from payments.views import BraintreePayView, BraintreeRecurPayView, bt_result, recurpayresult | |
urlpatterns = patterns('', | |
url(r'recurpay/', BraintreeRecurPayView.as_view(), name='recurpay'), | |
url(r'recurpayresult/', recurpayresult, name='recurpayresult'), | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.shortcuts import render | |
from django.core.urlresolvers import reverse | |
from django.views.generic import TemplateView | |
from django.views.generic import FormView | |
from django.http import HttpResponseRedirect, HttpResponse | |
from django.shortcuts import render_to_response | |
import urlparse | |
import braintree | |
braintree.Configuration.configure(braintree.Environment.Sandbox, | |
merchant_id="xyz456", | |
public_key="abc123", | |
private_key="abc123") | |
class BraintreeRecurPayView(TemplateView): | |
template_name = 'payments/payform.html' | |
def get_context_data(self, **kwargs): | |
context = super(BraintreeRecurPayView, self).get_context_data(**kwargs) | |
client_token = braintree.ClientToken.generate() | |
context.update({ | |
'client_token': client_token, | |
}) | |
return context | |
SUBSCRIPTION_PLAN = 'abc123' | |
def recurpayresult(request): | |
if request.method == 'POST': | |
# CREATE A CUSTOMER | |
# This is PCI compliant because Braintree is getting the CC on the frontend | |
# and in this view you only get the payment_method_nonce (token) which is PCI compliant. | |
create_result = braintree.Customer.create({ | |
"first_name": request.POST["first_name"], | |
"last_name": request.POST["last_name"], | |
"payment_method_nonce": request.POST["payment_method_nonce"] | |
}) | |
if create_result.is_success: | |
try: | |
# CREATE SUBSCRIPTION | |
# Note that the customer.credit_cards property is PCI compliant | |
customer_id = create_result.customer.id | |
customer = braintree.Customer.find(customer_id) | |
payment_method_token = customer.credit_cards[0].token | |
subscribe_result = braintree.Subscription.create({ | |
"payment_method_token": payment_method_token, | |
"plan_id": SUBSCRIPTION_PLAN, | |
}) | |
if subscribe_result.is_success: | |
return HttpResponse("Subscription Status {0}".format(subscribe_result.subscription.status)) | |
else: | |
return HttpResponse("Error: {0}".format(subscribe_result.message)) | |
except braintree.exceptions.NotFoundError: | |
return HttpResponse("No customer found for id: {0}".format(request.args['id'])) | |
else: | |
return HttpResponse("Error: {0}".format(create_result.message)) | |
return HttpResponseRedirect(reverse('recurpay')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment