@@ -506,48 +506,41 @@ def __init__(self, amount=None, currency=None, description=None,
506
506
self .status = 'pending'
507
507
self .receipt_email = None
508
508
self .receipt_number = None
509
+ self .payment_intent = None
509
510
self .payment_method = source .id
510
511
self .statement_descriptor = statement_descriptor
511
512
self .failure_code = None
512
513
self .failure_message = None
513
514
self .captured = capture
514
515
self .balance_transaction = None
515
516
516
- def _trigger_payment (self , on_success = None , on_failure_now = None ,
517
- on_failure_later = None ):
517
+ def _is_async_payment_method (self ):
518
518
pm = PaymentMethod ._api_retrieve (self .payment_method )
519
- async_payment = pm .type == 'sepa_debit'
520
-
521
- if async_payment :
522
- if not self ._authorized :
523
- async def callback ():
524
- await asyncio .sleep (0.5 )
525
- self .status = 'failed'
526
- if on_failure_later :
527
- on_failure_later ()
528
- else :
529
- async def callback ():
530
- await asyncio .sleep (0.5 )
531
- txn = BalanceTransaction (amount = self .amount ,
532
- currency = self .currency ,
533
- description = self .description ,
534
- exchange_rate = 1.0 ,
535
- reporting_category = 'charge' ,
536
- source = self .id , type = 'charge' )
537
- self .balance_transaction = txn .id
538
- self .status = 'succeeded'
539
- if on_success :
540
- on_success ()
541
- asyncio .ensure_future (callback ())
519
+ return pm .type == 'sepa_debit'
542
520
543
- else :
544
- if not self ._authorized :
521
+ def _handle_auth_failure (self , on_failure_now = None , on_failure_later = None ):
522
+ if self ._is_async_payment_method ():
523
+ async def callback ():
524
+ await asyncio .sleep (0.5 )
545
525
self .status = 'failed'
546
- self .failure_code = 'card_declined'
547
- self .failure_message = 'Your card was declined.'
548
- if on_failure_now :
549
- on_failure_now ()
550
- else :
526
+ if on_failure_later :
527
+ on_failure_later ()
528
+ asyncio .ensure_future (callback ())
529
+ else :
530
+ self .status = 'failed'
531
+ self .failure_code = 'card_declined'
532
+ self .failure_message = 'Your card was declined.'
533
+ if on_failure_now :
534
+ on_failure_now ()
535
+
536
+ raise UserError (402 , 'Your card was declined.' ,
537
+ {'code' : 'card_declined' , 'charge' : self .id })
538
+
539
+
540
+ def _trigger_payment (self , on_success = None ):
541
+ if self ._is_async_payment_method ():
542
+ async def callback ():
543
+ await asyncio .sleep (0.5 )
551
544
txn = BalanceTransaction (amount = self .amount ,
552
545
currency = self .currency ,
553
546
description = self .description ,
@@ -558,25 +551,39 @@ async def callback():
558
551
self .status = 'succeeded'
559
552
if on_success :
560
553
on_success ()
554
+ asyncio .ensure_future (callback ())
555
+
556
+ else :
557
+ txn = BalanceTransaction (amount = self .amount ,
558
+ currency = self .currency ,
559
+ description = self .description ,
560
+ exchange_rate = 1.0 ,
561
+ reporting_category = 'charge' ,
562
+ source = self .id , type = 'charge' )
563
+ self .balance_transaction = txn .id
564
+ self .status = 'succeeded'
565
+ if on_success :
566
+ on_success ()
561
567
562
568
@classmethod
563
569
def _api_create (cls , ** data ):
564
570
obj = super ()._api_create (** data )
565
571
566
- # for successful pre-auth, return unpaid charge
567
- if not obj .captured and obj ._authorized :
568
- return obj
572
+ obj ._initialize_charge ()
569
573
570
- def on_failure ():
571
- raise UserError (402 , 'Your card was declined.' ,
572
- {'code' : 'card_declined' , 'charge' : obj .id })
574
+ return obj
573
575
574
- obj ._trigger_payment (
575
- on_failure_now = on_failure ,
576
- on_failure_later = on_failure
577
- )
576
+ def _initialize_charge (self , on_success = None , on_failure_now = None ,
577
+ on_failure_later = None ):
578
+ if not self ._authorized :
579
+ self ._handle_auth_failure (on_failure_now = on_failure_now ,
580
+ on_failure_later = on_failure_later )
581
+ return
578
582
579
- return obj
583
+ self .status = 'succeeded'
584
+
585
+ if self .captured :
586
+ self ._trigger_payment (on_success )
580
587
581
588
@classmethod
582
589
def _api_capture (cls , id , amount = None , ** kwargs ):
@@ -592,24 +599,26 @@ def _api_capture(cls, id, amount=None, **kwargs):
592
599
obj ._capture (amount )
593
600
return obj
594
601
595
- def _capture (self , amount ):
602
+ def _capture (self , amount , on_success = None ):
596
603
if amount is None :
597
604
amount = self .amount
598
605
599
606
amount = try_convert_to_int (amount )
600
607
try :
601
608
assert type (amount ) is int and 0 <= amount <= self .amount
602
- assert self .captured is False
609
+ assert self .captured is False and self . status == 'succeeded'
603
610
except AssertionError :
604
611
raise UserError (400 , 'Bad request' )
605
612
606
- def on_success ():
613
+ def on_success_capture ():
607
614
self .captured = True
608
615
if amount < self .amount :
609
616
refunded = self .amount - amount
610
617
Refund (charge = self .id , amount = refunded )
618
+ if on_success :
619
+ on_success ()
611
620
612
- self ._trigger_payment (on_success )
621
+ self ._trigger_payment (on_success = on_success_capture )
613
622
614
623
@property
615
624
def paid (self ):
@@ -1106,7 +1115,6 @@ def _api_update(cls, id, **data):
1106
1115
def _api_delete (cls , id ):
1107
1116
raise UserError (405 , 'Method Not Allowed' )
1108
1117
1109
-
1110
1118
class Invoice (StripeObject ):
1111
1119
object = 'invoice'
1112
1120
_id_prefix = 'in_'
@@ -1867,32 +1875,34 @@ def __init__(self, amount=None, currency=None, customer=None,
1867
1875
self ._canceled = False
1868
1876
self ._authentication_failed = False
1869
1877
1870
- def _trigger_payment (self ):
1871
- if self .status != 'requires_confirmation' :
1872
- raise UserError (400 , 'Bad request' )
1878
+ def _on_success (self ):
1879
+ if self .invoice :
1880
+ invoice = Invoice ._api_retrieve (self .invoice )
1881
+ invoice ._on_payment_success ()
1873
1882
1874
- def on_success ( ):
1875
- if self .invoice :
1876
- invoice = Invoice ._api_retrieve (self .invoice )
1877
- invoice ._on_payment_success ()
1883
+ def _on_failure_now ( self ):
1884
+ if self .invoice :
1885
+ invoice = Invoice ._api_retrieve (self .invoice )
1886
+ invoice ._on_payment_failure_now ()
1878
1887
1879
- def on_failure_now ( ):
1880
- if self .invoice :
1881
- invoice = Invoice ._api_retrieve (self .invoice )
1882
- invoice ._on_payment_failure_now ()
1888
+ def _on_failure_later ( self ):
1889
+ if self .invoice :
1890
+ invoice = Invoice ._api_retrieve (self .invoice )
1891
+ invoice ._on_payment_failure_later ()
1883
1892
1884
- def on_failure_later ():
1885
- if self .invoice :
1886
- invoice = Invoice ._api_retrieve (self .invoice )
1887
- invoice ._on_payment_failure_later ()
1893
+ def _create_charge (self ):
1894
+ if self .status != 'requires_confirmation' :
1895
+ raise UserError (400 , 'Bad request' )
1888
1896
1889
1897
charge = Charge (amount = self .amount ,
1890
1898
currency = self .currency ,
1891
1899
customer = self .customer ,
1892
1900
source = self .payment_method ,
1893
1901
capture = (self .capture_method != "manual" ))
1902
+ charge .payment_intent = self .id
1894
1903
self .latest_charge = charge
1895
- charge ._trigger_payment (on_success , on_failure_now , on_failure_later )
1904
+ charge ._initialize_charge (self ._on_success , self ._on_failure_now ,
1905
+ self ._on_failure_later )
1896
1906
1897
1907
@property
1898
1908
def status (self ):
@@ -1984,7 +1994,7 @@ def _api_confirm(cls, id, payment_method=None, **kwargs):
1984
1994
'stripe_js' : '' },
1985
1995
}
1986
1996
else :
1987
- obj ._trigger_payment ()
1997
+ obj ._create_charge ()
1988
1998
1989
1999
return obj
1990
2000
@@ -2030,7 +2040,7 @@ def _api_authenticate(cls, id, client_secret=None, success=False,
2030
2040
2031
2041
obj .next_action = None
2032
2042
if success :
2033
- obj ._trigger_payment ()
2043
+ obj ._create_charge ()
2034
2044
else :
2035
2045
obj ._authentication_failed = True
2036
2046
obj .payment_method = None
@@ -2051,7 +2061,8 @@ def _api_capture(cls, id, amount_to_capture=None, **kwargs):
2051
2061
raise UserError (400 , 'Bad request' )
2052
2062
2053
2063
obj = cls ._api_retrieve (id )
2054
- obj .latest_charge ._capture (amount = amount_to_capture )
2064
+ obj .latest_charge ._capture (amount = amount_to_capture ,
2065
+ on_success = obj ._on_success )
2055
2066
return obj
2056
2067
2057
2068
0 commit comments