def setUp(self): User = get_user_model() self.user_obj = User.objects.create_user(username="******") self.user_two_obj = User.objects.create_user(username="******") self.user_payment = create_new_payment("BITCOIN", 10, "USD", user=self.user_obj) self.anon_payment = create_new_payment("BITCOIN", 10, "USD") self.no_anon_payment = create_new_payment("BITCOINTEST", 10, "USD")
def test_cancel_old_payment_task(self): payment = create_new_payment(self.crypto, 10, "USD") payment_two = create_new_payment(self.crypto, 10, "USD") created_at = timezone.now() - timedelta(hours=48) CryptoCurrencyPayment.objects.filter(pk=payment_two.pk).update( created_at=created_at) cancel_unpaid_payment() payment_two.refresh_from_db() payment.refresh_from_db() self.assertEqual(payment.status, CryptoCurrencyPayment.PAYMENT_NEW) self.assertEqual(payment_two.status, CryptoCurrencyPayment.PAYMENT_CANCELLED)
def test_address_transaction_can_be_resued(self): initial_payment = create_new_payment(self.crypto, 10, "USD") initial_payment.status = CryptoCurrencyPayment.PAYMENT_PAID initial_payment.save() payment = create_new_payment(self.crypto, 10, "USD") payment_count = CryptoCurrencyPayment.objects.filter( crypto=self.crypto).count() self.assertEqual(payment.address, self.backend.generate_new_address(payment_count - 1)) payment.status = CryptoCurrencyPayment.PAYMENT_PAID payment.save() payment_two = create_new_payment(self.crypto, 100, "EUR", reuse_address=True) self.assertEqual(initial_payment.address, payment_two.address)
def test_payment_cancelled_when_no_hash(self, fake_payment_paid): payment = create_new_payment(self.crypto, 10, "USD") self.assertEqual(payment.status, CryptoCurrencyPayment.PAYMENT_NEW) payment_task = CryptoCurrencyPaymentTask(self.crypto) payment_task.update_crypto_currency_payment_status() payment.refresh_from_db() self.assertEqual(payment.status, CryptoCurrencyPayment.PAYMENT_CANCELLED)
def test_new_address_generated_for_every_transaction(self): payment = create_new_payment(self.crypto, 10, "USD") payment_count = CryptoCurrencyPayment.objects.filter( crypto=self.crypto).count() self.assertEqual(payment.address, self.backend.generate_new_address(payment_count - 1)) payment_two = create_new_payment(self.crypto, 100, "EUR") payment_count = CryptoCurrencyPayment.objects.filter( crypto=self.crypto).count() self.assertEqual(payment_two.address, self.backend.generate_new_address(payment_count - 1)) payment_three = create_new_payment(self.crypto_two, 100, "EUR") payment_count = CryptoCurrencyPayment.objects.filter( crypto=self.crypto_two).count() self.assertEqual(payment_count, 1) self.assertEqual(payment_three.address, self.backend.generate_new_address(0))
def test_payment_created_with_generic_object(self): inv_obj = Invoice(title="Fake Invoice with payment") inv_obj.save() payment = create_new_payment(self.crypto_two, 122, "EUR", related_object=inv_obj) self.assertEqual(payment.content_object, inv_obj) self.assertEqual(payment, inv_obj.payments.all()[0])
def test_payment_status_no_underpaid_payment(self, fake_payment_paid, convert_to_fiat): payment = create_new_payment(self.crypto, 10, "USD") self.assertEqual(payment.status, CryptoCurrencyPayment.PAYMENT_NEW) payment_task = CryptoCurrencyPaymentTask(self.crypto) payment_task.update_crypto_currency_payment_status() payment.refresh_from_db() self.assertEqual(payment.status, CryptoCurrencyPayment.PAYMENT_PAID) self.assertEqual(payment.paid_crypto_amount, 1) self.assertIsNone(payment.child_payment)
def test_refresh_prices_task(self, convert_fiat_side): payment = create_new_payment(self.crypto, 10, "USD") self.assertEqual(payment.crypto_amount, 20) refresh_payment_prices() payment.refresh_from_db() self.assertEqual(payment.crypto_amount, 20) updated_at = timezone.now() - timedelta(minutes=1000) CryptoCurrencyPayment.objects.filter(pk=payment.pk).update( updated_at=updated_at) refresh_payment_prices() payment.refresh_from_db() self.assertEqual(payment.crypto_amount, 50)
def test_payment_status_paid_task_update(self, fake_payment_paid): payment = create_new_payment(self.crypto, 10, "USD") self.assertEqual(payment.status, CryptoCurrencyPayment.PAYMENT_NEW) update_payment_status() payment.refresh_from_db() self.assertEqual(payment.status, CryptoCurrencyPayment.PAYMENT_PROCESSING) self.assertEqual(payment.tx_hash, "hash") update_payment_status() payment.refresh_from_db() self.assertEqual(payment.status, CryptoCurrencyPayment.PAYMENT_PAID) self.assertEqual(payment.paid_crypto_amount, 123)
def test_child_payment_created_gets_generic_parent_object(self): inv_obj = Invoice(title="Fake Invoice with payment") inv_obj.save() payment = create_new_payment(self.crypto_two, 122, "EUR", related_object=inv_obj) child_payment = create_child_payment(payment, 5) self.assertEqual(child_payment.content_object, inv_obj) inv_pks = [inv.pk for inv in inv_obj.payments.all()] self.assertEqual(len(inv_pks), 2) self.assertIn(child_payment.pk, inv_pks) self.assertIn(payment.pk, inv_pks)
def add_to_cart(request, slug): item = get_object_or_404(Item, slug=slug) order_item, created = OrderItem.objects.get_or_create( item=item, user=request.user, ordered=False ) payment = create_new_payment(crypto='BITCOIN', #Cryptocurrency from your backend settings fiat_amount=item.to_pay, #Amount of actual item in fiat fiat_currency='USD', #Fiat currency used to convert to crypto amount payment_title=item.title, #Title associated with payment payment_description=item.label, #Description associated with payment related_object=None, #Generic linked object for this payment -> crypto_payments = GenericRelation(CryptoCurrencyPayment) user=request.user, #User of this payment for non-anonymous payment parent_payment=None, #Obvious address_index=None,# Use a particular address index for this payment reuse_address=None) #Used previously paid address for this payment pid = payment.id return redirect(f"/paydetails/payment/{pid}")
def test_create_new_payment(self): fait_amount = 20 fiat_currency = "USD" payment_title = "Buy Shirt" payment_description = "Buy One Get Free " payment = create_new_payment( self.crypto, fiat_amount=fait_amount, fiat_currency=fiat_currency, payment_title=payment_title, payment_description=payment_description, ) self.assertEqual(payment.crypto, self.crypto) self.assertEqual(payment.fiat_amount, fait_amount) self.assertEqual(payment.fiat_currency, fiat_currency) self.assertEqual(payment.payment_title, payment_title) self.assertEqual(payment.payment_description, payment_description) self.assertEqual(payment.address, self.backend.generate_new_address(0))
def test_particular_address_index_used(self): payment = create_new_payment(self.crypto, 10, "USD", address_index=10) self.assertEqual(payment.address, self.backend.generate_new_address(10))
def test_payment_paid_when_fiat_is_zero(self): payment = create_new_payment(self.crypto, 0, "USD") self.assertEqual(payment.status, CryptoCurrencyPayment.PAYMENT_PAID)
def test_create_payment_child(self): payment = create_new_payment(self.crypto, 10, "USD", address_index=6) child_payment = create_child_payment(payment, 5) self.assertEqual(child_payment.address, payment.address) self.assertEqual(child_payment.parent_payment, payment) self.assertEqual(payment.child_payment, child_payment)
def test_payment_can_belongs_to_a_user(self): UserModel = get_user_model() user = UserModel.objects.create_user(username="******") payment = create_new_payment(self.crypto_two, 122, "EUR", user=user) self.assertEqual(payment.user, user) self.assertEqual(payment, user.crypto_payments.all()[0])