def pay_with_payfast(request, *args, **kwargs): context = {} # Order model have to be defined by user, it is not a part # of django-payfast amount = request.GET.get('amount') item_name = request.GET.get('product') form = PayFastForm( initial={ # required params: 'amount': amount, 'item_name': item_name, 'return_url': request.scheme + "://" + request.get_host() + '/payfast/return/', 'cancel_url': request.scheme + "://" + request.get_host() + '/payfast/cancel/', 'notify_url': request.scheme + "://" + request.get_host() + '/payfast/notify/' }) context['form'] = form context['previous'] = request.META.get('HTTP_REFERER', None) or '/' context['amount'] = amount context['product'] = item_name return render(request, 'payfast/pay.html', context)
def test_init_with_user_custom_names(self): user = User.objects.create( username='******', email='*****@*****.**', first_name='First', last_name='Last', ) form = PayFastForm(initial={ 'amount': 100, 'item_name': 'Example item', }, user=user) self.assertEqual( { 'amount': 100, 'email_address': '*****@*****.**', 'item_name': 'Example item', 'm_payment_id': '1', 'merchant_id': '10000100', 'merchant_key': '46f0cd694581a', 'name_first': 'example_user First Name', 'name_last': 'example_user Last Name', 'notify_url': notify_url(), }, form.initial) self.assertEqual(user, form.order.user)
def _create_order(self): """ Create a payment order, and return the notification data for it. """ data = _test_data() # user posts the pay request payment_form = PayFastForm(initial={ 'amount': data['amount'], 'item_name': data['item_name'] }) self.assertEqual(_order().trusted, None) return _notify_data(data, payment_form)
def test_init(self): form = PayFastForm(initial={ 'amount': 100, 'item_name': 'Example item', }) self.assertEqual( { 'amount': 100, 'item_name': 'Example item', 'm_payment_id': '1', 'merchant_id': '10000100', 'merchant_key': '46f0cd694581a', 'notify_url': notify_url(), }, form.initial) self.assertIsNone(form.order.user)
def test_invalid_request(self): form = PayFastForm(initial={'amount': 100, 'item_name': 'foo'}) response = self.client.post(notify_url(), {'m_payment_id': form.order.pk}) self.assertEqual(response.status_code, 404) self.assertFalse(self.signal_handler.called) order = _order() self.assertEqual(order.request_ip, u'127.0.0.1') self.assertEqual( set(order.debug_info.split(u'|')), { u'amount_gross: Amount is not the same: {} != None'.format( # Django 1.8 returns more precise DecimalField values. u'100' if django.VERSION < (1, 8) else u'100.00'), u'item_name: This field is required.', u'merchant_id: This field is required.', }) self.assertEqual(order.trusted, False)
def test_invalid_request(self): form = PayFastForm(initial={'amount': 100, 'item_name': 'foo'}) notify_data = {'m_payment_id': form.order.m_payment_id} notify_data['signature'] = NotifyForm._calculate_itn_signature( notify_data) response = self.client.post(notify_url(), notify_data) expected_amount = ('100' if django.VERSION < (1, 8) else '100.00' if django.VERSION < (2, 0) else '100') self._assertBadRequest( response, { 'amount_gross': [{ 'code': '', 'message': ('Amount is not the same: {} != None'.format( expected_amount)) }], 'item_name': [{ 'code': 'required', 'message': 'This field is required.' }], 'merchant_id': [{ 'code': 'required', 'message': 'This field is required.' }], }) self.assertEqual(self.notify_handler_orders, []) order = _order() self.assertEqual(order.request_ip, '127.0.0.1') self.assertEqual( set(order.debug_info.split('|')), { 'amount_gross: Amount is not the same: {} != None'.format( '100' if django.VERSION < (1, 8) else # Django 1.8+ returns more precise DecimalField values '100.00' if django.VERSION < (2, 0) else # Django 2.0+ returns less precise DecimalField values again. '100'), 'item_name: This field is required.', 'merchant_id: This field is required.', }) self.assertEqual(order.trusted, False)