Permalink
Browse files

added stripe integration and some more methods to samurai

  • Loading branch information...
1 parent f8b3375 commit 08c0e1484189ac2c9bfde5b8b9dd16ade0602b13 @hayyat hayyat committed Dec 28, 2011
View
@@ -12,3 +12,6 @@ example/localsettings.py
docs/_build/*
*_flymake.py*
+#*
+.py#
+
@@ -0,0 +1,11 @@
+from django import forms
+from django.conf import settings
+import stripe
+
+class StripeForm(forms.Form):
+ credit_card__number = forms.CharField()
+ credit_card__cvc = forms.CharField(max_length=4)
+ credit_card__expiration_month = forms.CharField(max_length=7)
+ credit_card__expiration_year = forms.CharField(max_length=7)
+
+
@@ -44,10 +44,25 @@ def authorize(self, money, credit_card, options = None):
return {'status': 'FAILURE', 'response': error}
return {'status': 'SUCCESS', 'response': response}
- def capture(self, money, authorization, options = None):
+ def capture(self, money, identification, options = None):
+ from samurai.transaction import Transaction
+ trans = Transaction.find(identification)
+ if not trans.errors:
+ new_trans = trans.capture(money)
+ return{'status': "SUCCESS", "response": new_trans}
+ else:
+ return{"status":"FAILED", "response": trans.errors}
+
+ def reverse(self, money, authorization, options=None):
if not authorization.errors:
- new_trans = authorization.capture(money)
+ new_trans = authorization.reverse(money)
return{'status': "SUCCESS", "response": new_trans}
else:
return{"status":"FAILED", "response": authorization.errors}
-
+
+ def void(self, authorization, options=None):
+ if not authorization.errors:
+ new_trans = authorization.void()
+ return{'status': "SUCCESS", "response": new_trans}
+ else:
+ return{"status":"FAILED", "response": authorization.errors}
@@ -0,0 +1,35 @@
+from billing import Integration
+from django.conf import settings
+from django.views.decorators.http import require_GET
+from billing.signals import transaction_was_successful, transaction_was_unsuccessful
+from django.conf.urls.defaults import patterns, url
+import stripe, urllib
+from django.core.urlresolvers import reverse
+from billing.forms.stripe_forms import StripeForm
+from django.shortcuts import render_to_response
+from django.template import RequestContext
+from django.views.decorators.csrf import csrf_exempt
+from django.http import HttpResponse
+class StripeIntegration(Integration):
+ def __init__(self):
+ super(StripeIntegration,self).__init__()
+ stripe.api_key = settings.STRIPE_API_KEY
+ self.stripe = stripe
+
+ def generate_form(self):
+ initial_data = self.fields
+ form = StripeForm(initial=initial_data)
+ return form
+
+ @csrf_exempt
+ def get_token(self, request):
+ token = request.POST['stripeToken']
+ return HttpResponse('Success')
+
+ def get_urls(self):
+ urlpatterns = patterns('',
+ url('^stripe-get-token/$', self.get_token, name="get_token"),)
+ return urlpatterns
+
+
+
@@ -0,0 +1,6 @@
+<div class="payment-errors"> </div>
+<form method="post" id="payment-form">{% csrf_token %}
+
+ {{ integration.generate_form.as_p }}
+<input type="submit" value="Pay" class="submit-button"/>
+</form>
@@ -7,11 +7,12 @@
from billing.templatetags.google_checkout_tags import google_checkout
from billing.templatetags.amazon_fps_tags import amazon_fps
from billing.templatetags.braintree_payments_tags import braintree_payments
-
+from billing.templatetags.stripe_tags import stripe_payment
register = template.Library()
register.tag(google_checkout)
register.tag(paypal)
register.tag(world_pay)
register.tag(amazon_fps)
register.tag(braintree_payments)
+register.tag(stripe_payment)
@@ -0,0 +1,24 @@
+'''
+Template tags for Stripe Non PCI Complaince
+'''
+from django import template
+from django.template.loader import render_to_string
+from billing.forms.stripe_forms import StripeForm
+
+class StripeNode(template.Node):
+ def __init__(self, integration):
+ self.integration = template.Variable(integration)
+
+ def render(self, context):
+ int_obj = self.integration.resolve(context)
+ form_str = render_to_string("billing/stripe.html",
+ {"form":StripeForm(int_obj),
+ "integration": int_obj}, context)
+ return form_str
+
+def stripe_payment(parser, token):
+ try:
+ tag, int_obj = token.split_contents()
+ except ValueError:
+ raise template.TemplateSyntaxError("%r was expecting a single argument" %token.split_contents()[0])
+ return StripeNode(int_obj)
@@ -0,0 +1,68 @@
+----------------------------------------
+Stripe Payment Integration
+----------------------------------------
+
+`Stripe Payment Integration`_ is a service offered by
+`Stripe`_ to reduce the complexity of PCI compliance.
+
+.. note::
+
+ This integration makes use of the official `stripe`_ python package offered
+ by Stripe Payments. Please install it before you use this integration.
+
+Settings attributes required for this integration are:
+
+
+
+Here are the methods and attributes implemented on the ``StripeIntegration`` class:
+
+* ``__init__(self, options=None)``: The constructor method that configures the
+ stripe setting
+
+* ``get_urls(self)``: The method sets the url to which the token is sent
+ after the it is obtained from Stripe. This method is generally mapped
+ directly in the ``urls.py``.
+
+ .. code::
+
+ from billing import get_integration
+
+ stripe = get_integration("stripe")
+
+ urlpatterns += patterns('',
+ (r'^stripe/', include(stripe_obj.urls)),
+ )
+
+* ``get_token(self, request)``: The view method that recieves the
+token
+
+* ``generate_form(self)``: The method that generates and returns the form (present in
+ ``billing.forms.stripe_form``)
+
+
+Example:
+--------
+
+ In the views.py::
+
+ stripe_obj = get_integration("stripe")
+ return render_to_response("some_template.html",
+ {"stripe_obj": stripe_obj},
+ context_instance=RequestContext(request))
+
+ In the urls.py::
+
+ stripe_obj = get_integration("stripe")
+ urlpatterns += patterns('',
+ (r'^stripe/', include(stripe_obj.urls)),
+ )
+
+ In the template::
+
+ {% load billing_tags %}
+
+ {% stripe_payment stripe_obj %}
+
+
+.. _`Stripe`: https://stripe.com
+.. _`stripe`: http://pypi.python.org/pypi/stripe/
@@ -10,14 +10,16 @@
<a href='{% url app_authorize %}'>Authorize</a>
<a href='{% url app_eway %}'>Eway</a>
<a href='{% url app_paypal %}'>Paypal</a>
- <a href='{% url app_braintree %}'>Braintree Payments (S2S)</a>
+ <a href='{% url app_braintree %}'>Braintree Payments (S2S)</a>
+ <a href='{% url app_stripe %}'>Stripe Payment</a>
<br/><br/>
Offsite:
<a href='{% url app_offsite_paypal %}'>Paypal</a>
<a href='{% url app_offsite_google_checkout %}'>Google Checkout</a>
<a href='{% url app_offsite_world_pay %}'>WorldPay</a>
<a href='{% url app_offsite_amazon_fps %}'>Amazon FPS</a>
<a href='{% url app_offsite_braintree %}'>Braintree Payments (TR)</a>
+ <a href='{% url app_offsite_stripe %}'> Stripe </a>
<h1>{{ title }} </h1>
{% block content %}
@@ -0,0 +1,54 @@
+{% extends "app/base.html" %}
+{% load billing_tags %}
+
+{% block content %}
+<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.js"></script>
+<script type="text/javascript" src="https://js.stripe.com/v1/"></script>
+
+<script type="text/javascript">
+
+
+Stripe.setPublishableKey('pk_K2Z3rk1gTlIjSPRwDNyw3KEuDWmXY');
+
+$(document).ready(function() {
+ function stripeResponseHandler(status, response) {
+
+console.log(response);
+ if (response.error) {
+
+ //show the errors on the form
+ $(".payment-errors").html(response.error.message);
+
+ } else {
+
+ var url = "/stripe/stripe-get-token/";
+ var token = response['id'];
+ $.post(url, {
+ stripeToken:token
+ });
+ }
+ }
+
+ $("#payment-form").submit(function(event) {
+
+ // disable the submit button to prevent repeated clicks
+ $('.submit-button').attr("disabled", "disabled");
+
+ var amount = 1000; //amount you want to charge in cents
+ Stripe.createToken({
+ number: $('#id_credit_card__number').val(),
+ cvc: $('#id_credit_card__cvc').val(),
+ exp_month: $('#id_credit_card__expiration_month').val(),
+ exp_year: $('#id_credit_card__expiration_year').val()
+ }, amount, stripeResponseHandler);
+
+
+
+ return false;
+ });
+
+});
+</script>
+
+{% stripe_payment stripe_obj %}
+{% endblock %}
View
@@ -8,6 +8,7 @@
fps_recur_obj = get_integration("fps")
world_pay_obj = get_integration("world_pay")
braintree_obj = get_integration("braintree_payments")
+stripe_obj = get_integration("stripe")
urlpatterns = patterns('app.views',
url(r'^$', 'index', name='app_index'),
@@ -25,6 +26,7 @@
url(r'offsite/world_pay/$', 'offsite_world_pay', name='app_offsite_world_pay'),
url(r'offsite/amazon_fps/$', 'offsite_amazon_fps', name='app_offsite_amazon_fps'),
url(r'offsite/braintree/$', 'offsite_braintree', name='app_offsite_braintree'),
+ url(r'offsite/stripe/$', 'offsite_stripe', name='app_offsite_stripe'),
)
# paypal payment notification handler
@@ -42,6 +44,9 @@
urlpatterns += patterns('',
(r'^braintree/', include(braintree_obj.urls)),
)
+urlpatterns += patterns('',
+ (r'^stripe/', include(stripe_obj.urls)),
+)
urlpatterns += patterns('django.views.generic.simple',
url(r'offsite/paypal/done/$',
@@ -11,7 +11,8 @@
from app.forms import CreditCardForm
from app.urls import (google_checkout_obj, world_pay_obj,
pay_pal_obj, amazon_fps_obj,
- fps_recur_obj, braintree_obj)
+ fps_recur_obj, braintree_obj,
+ stripe_obj)
from django.conf import settings
from django.contrib.sites.models import RequestSite
@@ -232,3 +233,9 @@ def offsite_braintree(request):
template_vars = {'title': 'Braintree Payments Transparent Redirect',
"bp_obj": braintree_obj}
return render(request, "app/braintree_tr.html", template_vars)
+
+def offsite_stripe(request):
+
+ template_vars = {'title': 'Stripe Non PCI Compliance',
+ "stripe_obj": stripe_obj}
+ return render(request, "app/stripe.html", template_vars)
@@ -43,6 +43,8 @@
BRAINTREE_PUBLIC_KEY = ""
BRAINTREE_PRIVATE_KEY = ""
+#Stripe Payment Settings
+STRIPE_API_KEY = ''
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
View
@@ -100,6 +100,7 @@
'paypal.pro',
'paypal.standard.ipn',
'stripe',
+ 'samurai'
)
STATIC_URL = "http://merchant.agiliq.com/"

0 comments on commit 08c0e14

Please sign in to comment.