diff --git a/recurring_payment_stripe/models/account_move.py b/recurring_payment_stripe/models/account_move.py index 65e1968363c..ee5bbc2fdc2 100644 --- a/recurring_payment_stripe/models/account_move.py +++ b/recurring_payment_stripe/models/account_move.py @@ -42,35 +42,32 @@ def action_register_payment(self): ) # Handling the result of the PaymentIntent - if payment_intent["status"] == "succeeded": - # If the payment is successful, record the payment on the invoice - Payment = self.env["account.payment"].sudo() - payment_vals = { - "journal_id": self.env["account.journal"] - .search([("type", "=", "bank")], limit=1) - .id, - "amount": invoice.amount_total, - "payment_type": "inbound", - "partner_type": "customer", - "partner_id": invoice.partner_id.id, - "payment_method_id": self.env.ref( - "account.account_payment_method_manual_in" - ).id, - "ref": f"Stripe - {payment_intent['id']}", - } - payment = Payment.create(payment_vals) - payment.action_post() - invoice.state = "posted" - invoice.payment_state = "paid" - elif payment_intent["status"] == "requires_action": + if payment_intent["status"] != "succeeded": raise UserError( - _("Payment requires additional authentication (3D Secure).") - ) - else: - raise UserError( - f"Stripe payment error: {payment_intent['status']}" + _("Payment failed with status: %s") + % payment_intent["status"] ) + # If the payment is successful, record the payment on + # the invoice + Payment = self.env["account.payment"].sudo() + payment_vals = { + "journal_id": self.env["account.journal"] + .search([("type", "=", "bank")], limit=1) + .id, + "amount": invoice.amount_total, + "payment_type": "inbound", + "partner_type": "customer", + "partner_id": invoice.partner_id.id, + "payment_method_id": self.env.ref( + "account.account_payment_method_manual_in" + ).id, + "ref": f"Stripe - {payment_intent['id']}", + } + payment = Payment.create(payment_vals) + payment.action_post() + invoice.payment_state = "paid" + except stripe.StripeError as e: raise UserError(f"Stripe error: {e}") from e @@ -79,7 +76,8 @@ def action_register_payment(self): def _create_token(self, subscription): provider = subscription.provider_id - # Search for an existing payment token for the given provider and partner + # Search for an existing payment token for the given provider and + # partner token = self.env["payment.token"].search( [ ("provider_id", "=", provider.id), @@ -112,13 +110,20 @@ def _create_token(self, subscription): # Retrieve the default payment method for the customer, # or create one if it doesn't exist new_token.stripe_payment_method = ( - stripe.PaymentMethod.list(customer=customer.id, type="card", limit=1) + stripe.PaymentMethod.list( + customer=customer.id, + type="card", + limit=1, + ) .data[0] .id if stripe.PaymentMethod.list( customer=customer.id, type="card", limit=1 ).data - else stripe.Customer.create_source(customer.id, source="tok_visa").id + else stripe.Customer.create_source( + customer.id, + source="tok_visa", + ).id ) # Assign the new token to the variable @@ -138,6 +143,7 @@ def cron_process_due_invoices(self): if subscription and subscription.charge_automatically: try: # Register the payment + invoice.action_post() invoice.action_register_payment() except Exception as e: _logger.error(f"Error Processing Due Invoices: {str(e)}")