Skip to content

Commit

Permalink
16.0 dev (#10)
Browse files Browse the repository at this point in the history
* [FIX] Fixed payment transaction and design modular

* [FIX] Fixed test implementation

* [FIX] Fixed test implementation

* [FIX] Fixed test implementation

* [FIX] Fixed test implementation
  • Loading branch information
mjavint authored Dec 30, 2024
1 parent 1f8580c commit eee35fd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 47 deletions.
62 changes: 34 additions & 28 deletions recurring_payment_stripe/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,34 +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.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

Expand All @@ -78,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),
Expand Down Expand Up @@ -111,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
Expand Down
20 changes: 1 addition & 19 deletions recurring_payment_stripe/tests/test_payment_stripe_recurring.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,26 +146,8 @@ def create_sub(cls, vals):
return rec

def test_action_register_payment(self):
token = self.invoice._create_token(subscription=self.sub8)
self.assertTrue(token, "Payment token was not created")

# Check if the PaymentIntent was created
payment_intent = stripe.PaymentIntent.create(
amount=int(self.invoice.amount_total * 100),
currency=self.invoice.currency_id.name.lower(),
customer=token.provider_ref,
payment_method=token.stripe_payment_method,
off_session=True,
confirm=True,
metadata={"odoo_invoice_id": str(self.invoice.name)},
)
self.assertEqual(
payment_intent["status"],
"succeeded",
"PaymentIntent was not successful",
)
self.invoice.action_register_payment()
self.assertTrue(
self.invoice.payment_state == "paid",
"Invoice was not paid",
f"Invoice {self.invoice.id} should be paid",
)

0 comments on commit eee35fd

Please sign in to comment.