Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Wait on a transaction to start pay (bug 820192)

  • Loading branch information...
commit 73c3217b924e75373ab6b21838d7537dd4f6f729 1 parent a61b950
@kumar303 kumar303 authored
View
32 media/js/pay/wait.js
@@ -0,0 +1,32 @@
+$(function() {
+ "use strict";
+
+ var startUrl;
+ var timeout;
+
+ if ($('body').data('waitflow')) {
+ startUrl = $('body').data('trans-start-url');
+ poll();
+ }
+
+ function poll() {
+ $.get(startUrl)
+ .success(function(data, textStatus, jqXHR) {
+ if (data.url) {
+ if (timeout) {
+ window.clearTimeout(timeout);
+ }
+ window.location = data.url;
+ } else {
+ // The transaction is pending or it failed.
+ // TODO(Kumar) check for failed transactions here.
+ console.log('transaction state: ' + data.state)
+ timeout = window.setTimeout(poll, 1000);
+ }
+ })
+ .error(function() {
+ console.log('error checking transaction');
+ });
+ }
+
+});
View
23 webpay/base/decorators.py
@@ -0,0 +1,23 @@
+import functools
+import json
+
+from django import http
+
+
+def json_view(f=None, status_code=200):
+ def decorator(func):
+ @functools.wraps(func)
+ def wrapper(*args, **kw):
+ response = func(*args, **kw)
+ if isinstance(response, http.HttpResponse):
+ return response
+ else:
+ return http.HttpResponse(
+ json.dumps(response),
+ content_type='application/json',
+ status=status_code)
+ return wrapper
+ if f:
+ return decorator(f)
+ else:
+ return decorator
View
7 webpay/pay/templates/pay/fake-bango-url.html
@@ -0,0 +1,7 @@
+{% extends "base.html" %}
+
+{% block content %}
+ <div class="message">
+ Pretend this is a Bango payment screen for configuration ID {{ bill_config_id }}
+ </div>
+{% endblock %}
View
12 webpay/pay/templates/pay/wait-to-start.html
@@ -0,0 +1,12 @@
+{% extends "base.html" %}
+
+{% block body_attrs -%}
+ data-waitflow="true"
+ data-trans-start-url="{{ url('pay.trans_start_url') }}"
+{%- endblock %}
+
+{% block content %}
+ <div id="begin" class="message">
+ Waiting for payment to complete ...
+ </div>
+{% endblock %}
View
70 webpay/pay/tests/test_views.py
@@ -11,7 +11,8 @@
from webpay.pay.forms import VerifyForm
from webpay.pay.models import (Issuer, ISSUER_ACTIVE, ISSUER_INACTIVE,
- Transaction, TRANS_STATE_PENDING)
+ Transaction, TRANS_STATE_PENDING,
+ TRANS_STATE_READY)
from webpay.pay.samples import JWTtester
sample = os.path.join(os.path.dirname(__file__), 'sample.key')
@@ -131,8 +132,8 @@ def test_debug(self):
eq_(res.status_code, 400)
# Output should show exception message.
self.assertContains(res,
- 'InvalidJWT: Signature verification failed',
- status_code=400)
+ 'InvalidJWT: Signature verification failed',
+ status_code=400)
def test_not_debug(self):
with self.settings(VERBOSE_LOGGING=False):
@@ -202,3 +203,66 @@ def test_valid_purchase(self):
assert form.is_valid()
eq_(form.key, settings.KEY)
eq_(form.secret, settings.SECRET)
+
+
+class TestWaitToStart(Base):
+
+ def setUp(self):
+ super(TestWaitToStart, self).setUp()
+ self.wait = reverse('pay.wait_to_start')
+ self.start = reverse('pay.trans_start_url')
+ self.trans = Transaction.create(
+ state=TRANS_STATE_PENDING,
+ issuer_key='some app',
+ amount='0.99',
+ currency='BRL',
+ name='Virtual Eagle',
+ description='you know, just, an eagle',
+ json_request='{}')
+
+ # Set up a session for this client because the session code in
+ # Django's docs isn't working.
+ engine = __import__(settings.SESSION_ENGINE, {}, {}, [''])
+ self.session = engine.SessionStore()
+ self.session.create()
+ session_key = self.session.session_key
+
+ # Log in.
+ self.session['uuid'] = 'verified-user'
+ # Start a payment.
+ self.session['trans_id'] = self.trans.pk
+ self.session.save()
+
+ self.client = test.Client()
+ self.client.cookies[settings.SESSION_COOKIE_NAME] = session_key
+
+ def update(self, **kw):
+ Transaction.objects.filter(pk=self.trans.pk).update(**kw)
+ self.trans = Transaction.objects.get(pk=self.trans.pk)
+
+ @mock.patch.object(settings, 'BANGO_PAY_URL', 'http://bango/pay?bcid=%s')
+ def test_redirect_when_ready(self):
+ self.update(state=TRANS_STATE_READY, bango_config_id=123)
+ res = self.client.get(self.wait)
+ eq_(res['Location'], settings.BANGO_PAY_URL % 123)
+
+ @mock.patch.object(settings, 'BANGO_PAY_URL', 'http://bango/pay?bcid=%s')
+ def test_start_ready(self):
+ self.update(state=TRANS_STATE_READY, bango_config_id=123)
+ res = self.client.get(self.start)
+ eq_(res.status_code, 200, res.content)
+ data = json.loads(res.content)
+ eq_(data['url'], settings.BANGO_PAY_URL % 123)
+ eq_(data['state'], self.trans.state)
+
+ def test_start_not_ready(self):
+ res = self.client.get(self.start)
+ eq_(res.status_code, 200, res.content)
+ data = json.loads(res.content)
+ eq_(data['url'], None)
+ eq_(data['state'], self.trans.state)
+
+ def test_wait(self):
+ res = self.client.get(self.wait)
+ eq_(res.status_code, 200)
+ self.assertContains(res, 'Waiting')
View
6 webpay/pay/urls.py
@@ -6,7 +6,11 @@
url(r'^$', views.lobby, name='pay.lobby'),
url(r'^complete$', views.complete, name='pay.complete'),
url(r'^fakepay$', views.fakepay, name='pay.fakepay'),
+ # Be careful if you change this because it could be hard
+ # coded into settings. See BANGO_PAY_URL.
+ url(r'^fake-bango-url$', views.fake_bango_url,
+ name='pay.fake_bango_url'),
url(r'^wait_to_start$', views.wait_to_start, name='pay.wait_to_start'),
- url(r'^trans_start_url/([\d]+)$', views.trans_start_url,
+ url(r'^trans_start_url$', views.trans_start_url,
name='pay.trans_start_url'),
)
View
39 webpay/pay/views.py
@@ -13,11 +13,12 @@
from tower import ugettext as _
from webpay.auth.decorators import user_verified
+from webpay.base.decorators import json_view
from webpay.pin.forms import VerifyPinForm
from . import tasks
from .forms import VerifyForm
-from .models import (Issuer, Transaction, TRANS_STATE_PENDING,
- TRANS_STATE_COMPLETED)
+from .models import (Issuer, Transaction, TRANS_STATE_COMPLETED,
+ TRANS_STATE_PENDING, TRANS_STATE_READY)
log = commonware.log.getLogger('w.pay')
@@ -110,6 +111,13 @@ def fakepay(request):
@user_verified
@require_GET
+def fake_bango_url(request):
+ return render(request, 'pay/fake-bango-url.html',
+ {'bill_config_id': request.GET['bcid']})
+
+
+@user_verified
+@require_GET
def wait_to_start(request):
"""
Wait until the transaction is in a ready state.
@@ -118,24 +126,23 @@ def wait_to_start(request):
When ready, redirect to the Bango payment URL using
the generated billing configuration ID.
"""
- # TODO(Kumar) Create view to poll transaction state and redirect
- # to bango URL based on billing config ID. Bug 820192.
- return http.HttpResponse('view not yet implemented')
- #return render(request, 'pay/wait_to_start.html')
+ trans = Transaction.objects.get(pk=request.session['trans_id'])
+ if trans.state == TRANS_STATE_READY:
+ # The transaction is ready; no need to wait for it.
+ return http.HttpResponseRedirect(
+ settings.BANGO_PAY_URL % trans.bango_config_id)
+ return render(request, 'pay/wait-to-start.html')
@user_verified
+@json_view
@require_GET
-def trans_start_url(request, trans_id):
+def trans_start_url(request):
"""
JSON handler to get the Bango payment URL to start a transaction.
"""
- # TODO(Kumar) Poll transaction for URL. Bug 820192.
- # TODO(Kumar) 403 if the transaction was not started by the logged in user.
- raise NotImplementedError('view does not exist')
- #trans = Transaction.objects.get(pk=trans_id)
- #data = {'url': None, 'state': trans.state}
- #if trans.state == TRANS_STATE_READY:
- # data['url'] = ('http://mozilla.test.bango.org/mozpayments/?bcid=%s'
- # % trans.bango_config_id)
- #return data
+ trans = Transaction.objects.get(pk=request.session['trans_id'])
+ data = {'url': None, 'state': trans.state}
+ if trans.state == TRANS_STATE_READY:
+ data['url'] = settings.BANGO_PAY_URL % trans.bango_config_id
+ return data
View
2  webpay/pin/tests/test_views.py
@@ -88,7 +88,7 @@ def test_unauth(self):
@patch.object(client, 'verify_pin', lambda x, y: True)
def test_good_pin(self):
res = self.client.post(self.url, data={'pin': '1234'})
- self.assertRedirects(res, get_payment_url())
+ assert res['Location'].endswith(get_payment_url())
@patch.object(client, 'verify_pin', lambda x, y: False)
def test_bad_pin(self):
View
5 webpay/settings/base.py
@@ -36,6 +36,7 @@
'js/lib/format.js',
'js/css3.js',
'js/pay/pay.js',
+ 'js/pay/wait.js',
'js/pin/pin.js',
'js/pin/pad.js',
),
@@ -191,3 +192,7 @@
# Living on the bleeding B2G edge!
BROWSERID_VERIFICATION_URL = 'https://notoriousb2g.personatest.org/verify'
BROWSERID_JS_URL = 'https://notoriousb2g.personatest.org/include.js'
+
+# This is the URL for the bango payment screen.
+# It will receive one string substitution: the billing configuration ID.
+BANGO_PAY_URL = 'http://mozilla.test.bango.org/mozpayments/?bcid=%s'
View
4 webpay/settings/local.py-dist
@@ -116,3 +116,7 @@ SOLITUDE_URL = 'http://localhost:9000'
# If you want to test the pay flow without making a real Bango
# payment, set this to True.
#FAKE_PAYMENTS = True
+
+# Uncomment this to redirect to a fake Bango payments screen
+# when FAKE_PAYMENTS is False.
+#BANGO_PAY_URL = '/mozpay/fake-bango-url?bcid=%s'
Please sign in to comment.
Something went wrong with that request. Please try again.