Stripe: Convert all payment methods to intents except multibanco (#3780)

Co-authored-by: Richard Schreiber <schreiber@rami.io>
This commit is contained in:
Raphael Michel
2024-01-12 10:23:00 +01:00
committed by GitHub
parent ea33c7b1b9
commit 94cbb19db0
11 changed files with 833 additions and 878 deletions

View File

@@ -30,7 +30,8 @@ from django.core import mail as djmail
from django.utils.timezone import now
from django_countries.fields import Country
from django_scopes import scopes_disabled
from stripe.error import APIConnectionError
from stripe import error
from tests.plugins.stripe.test_checkout import apple_domain_create
from tests.plugins.stripe.test_provider import MockedCharge
from pretix.base.models import InvoiceAddress, Order, OrderPosition
@@ -744,13 +745,14 @@ def test_payment_refund_fail(token_client, organizer, event, order, monkeypatch)
@pytest.mark.django_db
def test_payment_refund_success(token_client, organizer, event, order, monkeypatch):
def charge_retr(*args, **kwargs):
def refund_create(amount):
r = MockedCharge()
r.id = 'foo'
r.status = 'succeeded'
return r
def refund_create(*args, **kwargs):
r = MockedCharge()
r.id = 'foo'
r.status = 'succeeded'
return r
def charge_retr(*args, **kwargs):
c = MockedCharge()
c.refunds.create = refund_create
return c
@@ -765,7 +767,9 @@ def test_payment_refund_success(token_client, organizer, event, order, monkeypat
'id': 'ch_123345345'
})
)
monkeypatch.setattr("stripe.ApplePayDomain.create", apple_domain_create)
monkeypatch.setattr("stripe.Charge.retrieve", charge_retr)
monkeypatch.setattr("stripe.Refund.create", refund_create)
resp = token_client.post('/api/v1/organizers/{}/events/{}/orders/{}/payments/{}/refund/'.format(
organizer.slug, event.slug, order.code, p1.local_id
), format='json', data={
@@ -784,7 +788,7 @@ def test_payment_refund_success(token_client, organizer, event, order, monkeypat
def test_payment_refund_unavailable(token_client, organizer, event, order, monkeypatch):
def charge_retr(*args, **kwargs):
def refund_create(amount):
raise APIConnectionError(message='Foo')
raise error.APIConnectionError(message='Foo')
c = MockedCharge()
c.refunds.create = refund_create

View File

@@ -43,6 +43,7 @@ from django.utils.timezone import now
from django_countries.fields import Country
from django_scopes import scopes_disabled
from tests.base import SoupTest
from tests.plugins.stripe.test_checkout import apple_domain_create
from tests.plugins.stripe.test_provider import MockedCharge
from pretix.base.models import (
@@ -2081,15 +2082,17 @@ def test_refund_paid_order_automatically_failed(client, env, monkeypatch):
p.confirm()
client.login(email='dummy@dummy.dummy', password='dummy')
def charge_retr(*args, **kwargs):
def refund_create(amount):
raise PaymentException('This failed.')
def refund_create(*args, **kwargs):
raise PaymentException('This failed.')
def charge_retr(*args, **kwargs):
c = MockedCharge()
c.refunds.create = refund_create
return c
monkeypatch.setattr("stripe.ApplePayDomain.create", apple_domain_create)
monkeypatch.setattr("stripe.Charge.retrieve", charge_retr)
monkeypatch.setattr("stripe.Refund.create", refund_create)
r = client.post('/control/event/dummy/dummy/orders/FOO/refund', {
'start-partial_amount': '7.00',
@@ -2123,18 +2126,20 @@ def test_refund_paid_order_automatically(client, env, monkeypatch):
p.confirm()
client.login(email='dummy@dummy.dummy', password='dummy')
def charge_retr(*args, **kwargs):
def refund_create(amount):
r = MockedCharge()
r.id = 'foo'
r.status = 'succeeded'
return r
def refund_create(*args, **kwargs):
r = MockedCharge()
r.id = 'foo'
r.status = 'succeeded'
return r
def charge_retr(*args, **kwargs):
c = MockedCharge()
c.refunds.create = refund_create
return c
monkeypatch.setattr("stripe.ApplePayDomain.create", apple_domain_create)
monkeypatch.setattr("stripe.Charge.retrieve", charge_retr)
monkeypatch.setattr("stripe.Refund.create", refund_create)
client.post('/control/event/dummy/dummy/orders/FOO/refund', {
'start-partial_amount': '7.00',

View File

@@ -43,7 +43,7 @@ from pretix.base.models import (
from pretix.testutils.sessions import add_cart_session, get_cart_session_key
class MockedCharge():
class MockedCharge:
status = ''
paid = False
id = 'ch_123345345'
@@ -52,18 +52,27 @@ class MockedCharge():
pass
class Object():
class Object:
pass
class MockedPaymentintent():
class MockedPaymentintent:
status = ''
id = 'pi_1EUon12Tb35ankTnZyvC3SdE'
latest_charge = MockedCharge()
charges = Object()
charges.data = [MockedCharge()]
charges.data = [latest_charge]
last_payment_error = None
class MockedAppleDomain:
livemode = True
def apple_domain_create(**kwargs):
return MockedAppleDomain()
@pytest.fixture
def env(client):
orga = Organizer.objects.create(name='CCC', slug='ccc')
@@ -80,6 +89,7 @@ def env(client):
quota_tickets.items.add(ticket)
event.settings.set('attendee_names_asked', False)
event.settings.set('payment_stripe__enabled', True)
event.settings.set('payment_stripe_publishable_key', 'nokey')
add_cart_session(client, event, {'email': 'admin@localhost'})
return client, ticket
@@ -92,10 +102,11 @@ def test_payment(env, monkeypatch):
assert kwargs['payment_method'] == 'pm_189fTT2eZvKYlo2CvJKzEzeu'
c = MockedPaymentintent()
c.status = 'succeeded'
c.charges.data[0].paid = True
c.latest_charge.paid = True
setattr(paymentintent_create, 'called', True)
return c
monkeypatch.setattr("stripe.ApplePayDomain.create", apple_domain_create)
monkeypatch.setattr("stripe.PaymentIntent.create", paymentintent_create)
client, ticket = env

View File

@@ -55,6 +55,7 @@ def env():
organizer=o, name='Dummy', slug='dummy',
date_from=now(), live=True
)
event.settings.set('payment_stripe_publishable_key', 'nokey')
o1 = Order.objects.create(
code='FOOBAR', event=event, email='dummy@dummy.test',
status=Order.STATUS_PENDING,
@@ -75,11 +76,11 @@ def factory():
return RequestFactory()
class MockedRefunds():
class MockedRefunds:
pass
class MockedCharge():
class MockedCharge:
status = ''
paid = False
id = 'ch_123345345'
@@ -96,18 +97,27 @@ class MockedCharge():
pass
class Object():
class Object:
pass
class MockedPaymentintent():
class MockedPaymentintent:
status = ''
id = 'pi_1EUon12Tb35ankTnZyvC3SdE'
latest_charge = MockedCharge()
charges = Object()
charges.data = [MockedCharge()]
charges.data = [latest_charge]
last_payment_error = None
class MockedAppleDomain:
livemode = True
def apple_domain_create(**kwargs):
return MockedAppleDomain()
@pytest.mark.django_db
def test_perform_success(env, factory, monkeypatch):
event, order = env
@@ -118,9 +128,10 @@ def test_perform_success(env, factory, monkeypatch):
assert kwargs['payment_method'] == 'pm_189fTT2eZvKYlo2CvJKzEzeu'
c = MockedPaymentintent()
c.status = 'succeeded'
c.charges.data[0].paid = True
c.latest_charge.paid = True
return c
monkeypatch.setattr("stripe.ApplePayDomain.create", apple_domain_create)
monkeypatch.setattr("stripe.PaymentIntent.create", paymentintent_create)
prov = StripeCC(event)
@@ -152,9 +163,10 @@ def test_perform_success_zero_decimal_currency(env, factory, monkeypatch):
assert kwargs['payment_method'] == 'pm_189fTT2eZvKYlo2CvJKzEzeu'
c = MockedPaymentintent()
c.status = 'succeeded'
c.charges.data[0].paid = True
c.latest_charge.paid = True
return c
monkeypatch.setattr("stripe.ApplePayDomain.create", apple_domain_create)
monkeypatch.setattr("stripe.PaymentIntent.create", paymentintent_create)
prov = StripeCC(event)
req = factory.post('/', {
@@ -180,6 +192,7 @@ def test_perform_card_error(env, factory, monkeypatch):
def paymentintent_create(**kwargs):
raise error.CardError(message='Foo', param='foo', code=100)
monkeypatch.setattr("stripe.ApplePayDomain.create", apple_domain_create)
monkeypatch.setattr("stripe.PaymentIntent.create", paymentintent_create)
prov = StripeCC(event)
req = factory.post('/', {
@@ -206,6 +219,7 @@ def test_perform_stripe_error(env, factory, monkeypatch):
def paymentintent_create(**kwargs):
raise error.CardError(message='Foo', param='foo', code=100)
monkeypatch.setattr("stripe.ApplePayDomain.create", apple_domain_create)
monkeypatch.setattr("stripe.PaymentIntent.create", paymentintent_create)
prov = StripeCC(event)
req = factory.post('/', {
@@ -236,11 +250,12 @@ def test_perform_failed(env, factory, monkeypatch):
c = MockedPaymentintent()
c.status = 'failed'
c.failure_message = 'Foo'
c.charges.data[0].paid = True
c.latest_charge.paid = True
c.last_payment_error = Object()
c.last_payment_error.message = "Foo"
return c
monkeypatch.setattr("stripe.ApplePayDomain.create", apple_domain_create)
monkeypatch.setattr("stripe.PaymentIntent.create", paymentintent_create)
prov = StripeCC(event)
req = factory.post('/', {
@@ -264,18 +279,20 @@ def test_perform_failed(env, factory, monkeypatch):
def test_refund_success(env, factory, monkeypatch):
event, order = env
def charge_retr(*args, **kwargs):
def refund_create(amount):
r = MockedCharge()
r.id = 'foo'
r.status = 'succeeded'
return r
def refund_create(*args, **kwargs):
r = MockedCharge()
r.id = 'foo'
r.status = 'succeeded'
return r
def charge_retr(*args, **kwargs):
c = MockedCharge()
c.refunds.create = refund_create
return c
monkeypatch.setattr("stripe.ApplePayDomain.create", apple_domain_create)
monkeypatch.setattr("stripe.Charge.retrieve", charge_retr)
monkeypatch.setattr("stripe.Refund.create", refund_create)
order.status = Order.STATUS_PAID
p = order.payments.create(provider='stripe_cc', amount=order.total, info=json.dumps({
'id': 'ch_123345345'
@@ -294,15 +311,17 @@ def test_refund_success(env, factory, monkeypatch):
def test_refund_unavailable(env, factory, monkeypatch):
event, order = env
def charge_retr(*args, **kwargs):
def refund_create(amount):
raise error.APIConnectionError(message='Foo')
def refund_create(*args, **kwargs):
raise error.APIConnectionError(message='Foo')
def charge_retr(*args, **kwargs):
c = MockedCharge()
c.refunds.create = refund_create
return c
monkeypatch.setattr("stripe.ApplePayDomain.create", apple_domain_create)
monkeypatch.setattr("stripe.Charge.retrieve", charge_retr)
monkeypatch.setattr("stripe.Refund.create", refund_create)
order.status = Order.STATUS_PAID
p = order.payments.create(provider='stripe_cc', amount=order.total, info=json.dumps({
'id': 'ch_123345345'

View File

@@ -151,12 +151,22 @@ def test_webhook_all_good(env, client, monkeypatch):
@pytest.mark.django_db
def test_webhook_mark_paid_without_reference_and_payment(env, client, monkeypatch):
def test_webhook_mark_paid(env, client, monkeypatch):
order = env[1]
order.status = Order.STATUS_PENDING
order.save()
charge = get_test_charge(env[1])
charge["amount_refunded"] = 0
with scopes_disabled():
payment = env[1].payments.create(
provider='stripe', amount=env[1].total, info='{}', state=OrderPayment.PAYMENT_STATE_CREATED,
)
ReferencedStripeObject.objects.create(
order=order,
payment=payment,
reference="pi_123456",
)
monkeypatch.setattr("stripe.Charge.retrieve", lambda *args, **kwargs: charge)
client.post('/dummy/dummy/stripe/webhook/', json.dumps(
@@ -169,6 +179,10 @@ def test_webhook_mark_paid_without_reference_and_payment(env, client, monkeypatc
"object": {
"id": "ch_18TY6GGGWE2Ias8TZHanef25",
"object": "charge",
"payment_intent": "pi_123456",
"metadata": {
"event": order.event_id,
}
# Rest of object is ignored anway
}
},
@@ -253,6 +267,7 @@ def test_webhook_global(env, client, monkeypatch):
order.save()
charge = get_test_charge(env[1])
charge["amount_refunded"] = 0
monkeypatch.setattr("stripe.Charge.retrieve", lambda *args, **kwargs: charge)
with scopes_disabled():
@@ -261,6 +276,8 @@ def test_webhook_global(env, client, monkeypatch):
)
ReferencedStripeObject.objects.create(order=order, reference="ch_18TY6GGGWE2Ias8TZHanef25",
payment=payment)
ReferencedStripeObject.objects.create(order=order, reference="pi_123456",
payment=payment)
client.post('/_stripe/webhook/', json.dumps(
{
@@ -272,6 +289,10 @@ def test_webhook_global(env, client, monkeypatch):
"object": {
"id": "ch_18TY6GGGWE2Ias8TZHanef25",
"object": "charge",
"payment_intent": "pi_123456",
"metadata": {
"event": order.event_id,
}
# Rest of object is ignored anway
}
},
@@ -293,6 +314,7 @@ def test_webhook_global_legacy_reference(env, client, monkeypatch):
order.save()
charge = get_test_charge(env[1])
charge["amount_refunded"] = 0
monkeypatch.setattr("stripe.Charge.retrieve", lambda *args, **kwargs: charge)
with scopes_disabled():
@@ -300,6 +322,7 @@ def test_webhook_global_legacy_reference(env, client, monkeypatch):
provider='stripe', amount=order.total, info=json.dumps(charge), state=OrderPayment.PAYMENT_STATE_CREATED
)
ReferencedStripeObject.objects.create(order=order, reference="ch_18TY6GGGWE2Ias8TZHanef25")
ReferencedStripeObject.objects.create(order=order, reference="pi_123456")
client.post('/_stripe/webhook/', json.dumps(
{
@@ -311,6 +334,10 @@ def test_webhook_global_legacy_reference(env, client, monkeypatch):
"object": {
"id": "ch_18TY6GGGWE2Ias8TZHanef25",
"object": "charge",
"payment_intent": "pi_123456",
"metadata": {
"event": order.event_id,
}
# Rest of object is ignored anway
}
},