def repeat_payment(self, **kwargs): # Check if backend is still valid from wlhosted.payments.backends import get_backend try: get_backend(self.backend) except KeyError: return False with transaction.atomic(using='payments_db'): # Check for failed payments previous = Payment.objects.filter(repeat=self) if previous.exists(): failures = previous.filter(state=Payment.REJECTED) try: last_good = previous.filter( state=Payment.PROCESSED).order_by('-created')[0] failures = failures.filter(created__gt=last_good.created) except IndexError: pass if failures.count() >= 3: return False # Create new payment object extra = {} extra.update(self.extra) extra.update(kwargs) payment = Payment.objects.create(amount=self.amount, backend=self.backend, description=self.description, recurring='', customer=self.customer, amount_fixed=self.amount_fixed, repeat=self, extra=extra) return payment
def prepare_recurring(self, method): self.create_payment(period='y') payment = Payment.objects.all()[0] # Complete the payment backend = get_backend(method)(payment) backend.initiate(None, '', '') Customer.objects.update( name='Michal Čihař', address='Zdiměřická 1439', city='149 00 Praha 4', country='CZ', vat='CZ8003280318', ) backend.complete(None) self.assertRedirects( self.client.get(reverse('create-billing'), {'payment': payment.uuid}), reverse('billing')) # Check recurrence is stored bill = Billing.objects.all()[0] invoices = bill.invoice_set.count() # Fake end of last invoice last_invoice = bill.invoice_set.order_by('-start')[0] last_invoice.end = timezone.now() - relativedelta(days=7) last_invoice.save() return payment, bill, invoices
def prepare_recurring(self, method): self.create_payment(period="y") payment = Payment.objects.all()[0] # Complete the payment backend = get_backend(method)(payment) backend.initiate(None, "", "") Customer.objects.update( name="Michal Čihař", address="Zdiměřická 1439", city="149 00 Praha 4", country="CZ", vat="CZ8003280318", ) backend.complete(None) self.assertRedirects( self.client.get(reverse("create-billing"), {"payment": payment.uuid}), reverse("billing"), ) # Check recurrence is stored bill = Billing.objects.all()[0] invoices = bill.invoice_set.count() # Fake end of last invoice last_invoice = bill.invoice_set.order_by("-start")[0] last_invoice.end = timezone.now() - relativedelta(days=7) last_invoice.save() return payment, bill, invoices
def test_assertions(self): backend = get_backend("pending")(self.payment) backend.payment.state = Payment.PENDING with self.assertRaises(InvalidState): backend.initiate(None, "", "") backend.payment.state = Payment.ACCEPTED with self.assertRaises(InvalidState): backend.complete(None)
def dispatch(self, request, *args, **kwargs): with transaction.atomic(using='payments_db'): self.object = self.get_object() if self.object.state == Payment.NEW: return redirect('payment', pk=self.object.pk) if self.object.state != Payment.PENDING: return self.redirect_origin() backend = get_backend(self.object.backend)(self.object) backend.complete(self.request) return self.redirect_origin()
def form_valid(self, form): if not self.can_pay: return redirect('payment', pk=self.object.pk) # Actualy call the payment backend method = form.cleaned_data['method'] backend = get_backend(method)(self.object) result = backend.initiate( self.request, self.request.build_absolute_uri( reverse('payment', kwargs={'pk': self.object.pk})), self.request.build_absolute_uri( reverse('payment-complete', kwargs={'pk': self.object.pk})), ) if result is not None: return result backend.complete(self.request) return self.redirect_origin()
def dispatch(self, request, *args, **kwargs): with transaction.atomic(using="payments_db"): self.object = self.get_object() if self.object.state == Payment.NEW: return redirect("payment", pk=self.object.pk) if self.object.state != Payment.PENDING: return self.redirect_origin() backend = get_backend(self.object.backend)(self.object) backend.complete(self.request) # If payment is still pending, display info page if backend.payment.state == Payment.PENDING: return render( request, "payment/pending.html", {"object": backend.payment, "backend": backend}, ) return self.redirect_origin()
def test_recurring(self): """Test recurring payments.""" payment, bill, invoices = self.prepare_recurring('pay') self.assertEqual(bill.payment['recurring'], str(payment.pk)) self.run_recurring() # Complete the payment (we've faked the payment server above) recure_payment = Payment.objects.exclude(pk=payment.pk)[0] backend = get_backend('pay')(recure_payment) backend.initiate(None, '', '') backend.complete(None) # Process pending payments pending_payments() # There should be additional invoice on the billing self.assertEqual(invoices + 1, bill.invoice_set.count())
def test_proforma(self): backend = get_backend("fio-bank")(self.payment) self.assertIsNotNone(backend.initiate(None, "", "/complete/")) self.check_payment(Payment.PENDING) self.assertFalse(backend.complete(None)) self.check_payment(Payment.PENDING) httpretty.register_uri(httpretty.GET, FIO_API, body=json.dumps(FIO_TRASACTIONS)) FioBank.fetch_payments() self.check_payment(Payment.PENDING) received = copy(FIO_TRASACTIONS) proforma_id = backend.payment.invoice transaction = received["accountStatement"]["transactionList"]["transaction"] transaction[0]["column16"]["value"] = proforma_id transaction[1]["column16"]["value"] = proforma_id transaction[1]["column1"]["value"] = backend.payment.amount * 1.21 httpretty.register_uri(httpretty.GET, FIO_API, body=json.dumps(received)) FioBank.fetch_payments() payment = self.check_payment(Payment.ACCEPTED) self.maxDiff = None self.assertEqual( payment.details["transaction"]["recipient_message"], proforma_id )
def test_recurring_one_error(self): """Test handling of single failed recurring payments.""" payment, bill, invoices = self.prepare_recurring('pay') self.assertEqual(bill.payment['recurring'], str(payment.pk)) Payment.objects.create(repeat=payment, customer=payment.customer, state=Payment.REJECTED, amount=1) self.run_recurring() # Complete the payment (we've faked the payment server above) recure_payment = Payment.objects.exclude(pk=payment.pk).exclude( amount=1)[0] backend = get_backend('pay')(recure_payment) backend.initiate(None, '', '') backend.complete(None) # Process pending payments pending_payments() # There should be additional invoice on the billing self.assertEqual(invoices + 1, bill.invoice_set.count())
def test_pending(self): backend = get_backend("pending")(self.payment) self.assertIsNotNone(backend.initiate(None, "", "")) self.check_payment(Payment.PENDING) self.assertTrue(backend.complete(None)) self.check_payment(Payment.ACCEPTED)
def test_reject(self): backend = get_backend("reject")(self.payment) self.assertIsNone(backend.initiate(None, "", "")) self.check_payment(Payment.PENDING) self.assertFalse(backend.complete(None)) self.check_payment(Payment.REJECTED)
def test_pay(self): backend = get_backend('pay')(self.payment) self.assertIsNone(backend.initiate(None, '', '')) self.check_payment(Payment.PENDING) self.assertTrue(backend.complete(None)) self.check_payment(Payment.ACCEPTED)