Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added samurai integration example and docs. Still a work in progress.

  • Loading branch information...
commit 4e600b6718f1b37bb41bea1c452481c5aabc967d 1 parent 023ef61
@theju theju authored
View
8 billing/templates/billing/samurai.html
@@ -10,12 +10,12 @@
Samurai.ready(function() {
Samurai.on('form', 'payment', function(event, data) {
$.post('{% url samurai_transaction %}', data.payment_method, function(data) {
- var transaction = $.parseJSON(data).transaction;
- if(transaction.success) {
+ var transaction = $.parseJSON(data);
+ if (transaction.errors) {
+ Samurai.PaymentErrorHandler.forForm($('form').get(0)).handleErrorsFromResponse(transaction);
+ } else {
$('form').children('.results').html('<h3>Your purchase is complete!</h3><h4>'+transaction.payment_method.payment_method.first_name+' '+transaction.payment_method.payment_method.last_name+': $'+transaction.amount+' - '+transaction.description+'</h4>');
Samurai.trigger('form', 'completed');
- } else {
- Samurai.PaymentErrorHandler.forForm($('form').get(0)).handleErrorsFromResponse(transaction);
}
});
});
View
6 billing/tests/samurai_tests.py
@@ -2,6 +2,7 @@
from billing import get_gateway, CreditCard
from billing.gateway import CardNotSupported
from billing.utils.credit_card import Visa
+from samurai.payment_method import PaymentMethod
class SamuraiGatewayTestCase(TestCase):
@@ -55,3 +56,8 @@ def testUnstore(self):
self.assertEquals(resp["status"], "SUCCESS")
response = self.merchant.unstore(resp["response"].payment_method_token)
self.assertEquals(response["status"], "SUCCESS")
+
+ def testPurchaseWithToken(self):
+ resp = PaymentMethod.create('4111111111111111', '111', '07', '14')
+ resp = self.merchant.purchase(500, resp.payment_method_token)
+ self.assertEquals(resp["status"], "SUCCESS")
View
24 docs/offsite/samurai_integration.rst
@@ -32,7 +32,7 @@ Here are the methods and attributes implemented on the ``SamuraiIntegration`` cl
(r'^samurai/', include(samurai_obj.urls)),
)
-* ``get_token(self, request)``: The view method that recieves the
+* ``transaction(self, request)``: The view method that recieves the
token
* ``generate_form(self)``: The method that generates and returns the form (present in
@@ -42,16 +42,32 @@ token
Example:
--------
+ In <some_app>/integrations/samurai_example_integration.py::
+
+ from billing.integrations.samurai_integration import SamuraiIntegration
+
+ class SamuraiExampleIntegration(SamuraiIntegration):
+ class transaction(self, request):
+ # The token is received in the POST request
+ resp = self.samurai_gateway.purchase(100, request.POST["payment_method_token"])
+ if resp["status"] == "SUCCESS":
+ # Redirect if the transaction is successful
+ ...
+ else:
+ # Transaction failed
+ ...
+
+
In the views.py::
- samurai_obj = get_integration("samurai")
+ samurai_obj = get_integration("samurai_example")
return render_to_response("some_template.html",
{"samurai_obj": samurai_obj},
context_instance=RequestContext(request))
In the urls.py::
- samurai_obj = get_integration("samurai")
+ samurai_obj = get_integration("samurai_example")
urlpatterns += patterns('',
(r'^samurai/', include(samurai_obj.urls)),
)
@@ -64,5 +80,5 @@ Example:
.. _`Samurai Payment`: https://samurai.feefighters.com/
-.. _`samurai`: http://pypi.python.org/pypi/samurai/0.6
+.. _`samurai`: http://pypi.python.org/pypi/samurai
.. _`FeeFighters`: http://feefighters.com/
View
17 example/app/integrations/samurai_example_integration.py
@@ -0,0 +1,17 @@
+from billing.integrations.samurai_integration import SamuraiIntegration
+from django.views.decorators.csrf import csrf_exempt
+from django.http import HttpResponse
+try:
+ import json
+except ImportError:
+ import simplejson as json
+except ImportError:
+ from django.utils import simplejson as json
+
+class SamuraiExampleIntegration(SamuraiIntegration):
+ @csrf_exempt
+ def transaction(self, request):
+ print request.POST["payment_method_token"]
+ resp = self.samurai_gateway.purchase(1, request.POST["payment_method_token"])
+ print resp["response"].errors
+ return HttpResponse(resp["response"].to_json())
View
2  example/app/urls.py
@@ -9,7 +9,7 @@
world_pay_obj = get_integration("world_pay")
braintree_obj = get_integration("braintree_payments")
stripe_obj = get_integration("stripe_example")
-samurai_obj = get_integration("samurai")
+samurai_obj = get_integration("samurai_example")
urlpatterns = patterns('app.views',
url(r'^$', 'index', name='app_index'),
Please sign in to comment.
Something went wrong with that request. Please try again.