Skip to content

Commit

Permalink
Merge pull request #90 from jazkarta/nci-issues/336-recurring-donatio…
Browse files Browse the repository at this point in the history
…n-delay

Retrying subscription transactions with increasing delays
  • Loading branch information
witekdev authored Nov 27, 2024
2 parents e549714 + ced3295 commit ff919fd
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 13 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ jobs:
- name: Build jazkarta.shop docker image
run: docker build . -t jazkarta.shop
env:
DOCKER_BUILDKIT: "1"
DOCKER_BUILDKIT: "1"

- name: Start Plone server
run: docker-compose up -d
run: docker compose up -d
env:
DOCKER_BUILDKIT: "1"
DOCKER_BUILDKIT: "1"

- name: Check that jazkarta.shop was installed
run: curl --fail -v http://localhost:8080/Plone/review-cart
Expand All @@ -39,4 +39,4 @@ jobs:

- name: Print plone server logs
if: always() # This step should be also run when a previous step failed
run: docker-compose logs plone
run: docker compose logs plone
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ services:
- data:/data
ports:
- "8100"
# The `dummy` service is here to make sure `docker-compose up` exits only when
# The `dummy` service is here to make sure `docker compose up` exits only when
# `plone`'s health is `healthy`.
dummy:
image: busybox
Expand Down
43 changes: 37 additions & 6 deletions jazkarta/shop/browser/checkout/authorize_net_accept_js.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import six
import time
from AccessControl import getSecurityManager
from persistent.mapping import PersistentMapping
from ZODB.POSException import ConflictError
Expand All @@ -17,11 +18,16 @@
from ...utils import resolve_uid
from ...utils import run_in_transaction
from ...validators import is_email
import time
from ... import logger

SUBSCRIPTION_SLEEP_INCREMENT = 2
SUBSCRIPTION_RETRIES = 4


class CheckoutFormAuthorizeNetAcceptJs(CheckoutFormBase):
""" Renders a checkout form set up to submit through Stripe """
""" Renders a checkout form set up to submit through authorize.net Accept.js
"""

index = ViewPageTemplateFile(
'../templates/checkout_form_authorize_net_accept_js.pt')

Expand Down Expand Up @@ -50,6 +56,31 @@ def refId(self):
capture_payment = True
is_recurring = False
recurring_months = None
retries = 0

def retry_subscription_request(self, opaque_data, contact_info):
while self.retries < SUBSCRIPTION_RETRIES:
try:
return ARBCreateSubscriptionRequest(
self.cart, self.refId, opaque_data, contact_info,
months=self.recurring_months
)
except PaymentProcessingException as e:
self.retries += 1
# Raise the error once retries have been exceeded
if self.retries >= SUBSCRIPTION_RETRIES:
raise e
# Retry after delay for specific error
if 'Invalid OTS Token' in str(e):
delay = SUBSCRIPTION_SLEEP_INCREMENT * self.retries
logger.warn(
"Error on Auth.net subscription request. Retrying "
"({}) after {} seconds".format(self.retries, delay)
)
time.sleep(delay)
continue
# Raise the error for any other error
raise e

def handle_submit(self):
if not len(self.cart.items):
Expand Down Expand Up @@ -104,9 +135,9 @@ def handle_submit(self):

try:
if self.is_recurring:
response = ARBCreateSubscriptionRequest(
self.cart, self.refId, opaque_data, contact_info,
months=self.recurring_months)
response = self.retry_subscription_request(
opaque_data, contact_info
)
else:
transactionType = (
'authCaptureTransaction' if self.capture_payment
Expand All @@ -116,7 +147,7 @@ def handle_submit(self):
self.cart, self.refId, opaque_data, contact_info,
transactionType=transactionType)
except PaymentProcessingException as e:
self.error = e.message
self.error = str(e)

if not self.error:
try:
Expand Down
4 changes: 2 additions & 2 deletions jazkarta/shop/browser/checkout/stripe.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ def handle_submit(self):
except PaymentProcessingException as e:
charge_result = {
'success': False,
'err_msg': e.message,
'err_msg': str(e),
'err_code': getattr(e, 'code', None),
}
self.error = e.message
self.error = str(e)

if not self.error:
try:
Expand Down

0 comments on commit ff919fd

Please sign in to comment.