Skip to content

Commit

Permalink
fix: handle paypal payment recipt
Browse files Browse the repository at this point in the history
handle paypal payment recipt url

SONIC-784
  • Loading branch information
mubbsharanwar committed Dec 10, 2024
1 parent b1e03fb commit 78e52d4
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,5 @@ class Languages:
STRIPE_PAYMENT_STATUS_INTERFACE_CODE_SUCCEEDED = "succeeded"

EDX_STRIPE_PAYMENT_INTERFACE_NAME = "stripe_edx"

EDX_PAYPAL_PAYMENT_INTERFACE_NAME = "paypal_edx"
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ def get_edx_payment_intent_id(order: CTOrder) -> Union[str, None]:
return pmt.interface_id
return None

def get_edx_payment_service_provider(order: CTOrder) -> Union[str, None]:
for pr in order.payment_info.payments:
return pr.obj.payment_method_info.payment_interface


def get_edx_order_workflow_state_key(order: CTOrder) -> Optional[str]:
order_workflow_state = None
Expand Down
17 changes: 11 additions & 6 deletions commerce_coordinator/apps/commercetools/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@

from commerce_coordinator.apps.commercetools.catalog_info.edx_utils import (
get_edx_payment_intent_id,
get_edx_refund_amount
get_edx_refund_amount,
get_edx_payment_service_provider
)
from commerce_coordinator.apps.commercetools.catalog_info.constants import EDX_STRIPE_PAYMENT_INTERFACE_NAME
from commerce_coordinator.apps.commercetools.clients import CommercetoolsAPIClient
from commerce_coordinator.apps.commercetools.constants import COMMERCETOOLS_ORDER_MANAGEMENT_SYSTEM
from commerce_coordinator.apps.commercetools.data import order_from_commercetools
Expand Down Expand Up @@ -108,20 +110,23 @@ def run_filter(self, active_order_management_system, order_number, **kwargs): #
duration = (datetime.now() - start_time).total_seconds()
log.info(f"[Performance Check] get_order_by_number call took {duration} seconds")

psp = get_edx_payment_service_provider(ct_order)

intent_id = None
if psp == EDX_STRIPE_PAYMENT_INTERFACE_NAME:
intent_id = get_edx_payment_intent_id(ct_order)

ret_val = {
"order_data": ct_order,
"psp": psp,
"payment_intent_id": intent_id
}

intent_id = get_edx_payment_intent_id(ct_order)

if intent_id:
ct_payment = ct_api_client.get_payment_by_key(intent_id)
ret_val['payment_intent_id'] = intent_id
ret_val['amount_in_cents'] = get_edx_refund_amount(ct_order)
ret_val['has_been_refunded'] = has_refund_transaction(ct_payment)
ret_val['payment_data'] = ct_payment
else:
ret_val['payment_intent_id'] = None
ret_val['amount_in_cents'] = decimal.Decimal(0.00)
ret_val['has_been_refunded'] = False
ret_val['payment_data'] = None
Expand Down
Empty file.
8 changes: 8 additions & 0 deletions commerce_coordinator/apps/paypal/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"""
App configuration for the Commerce Coordinator stripe app.
"""
from django.apps import AppConfig


class PaypalConfig(AppConfig):
name = 'commerce_coordinator.apps.paypal'
Empty file.
26 changes: 26 additions & 0 deletions commerce_coordinator/apps/paypal/pipeline.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""
Pipelines for stripe app
"""

import logging

from django.conf import settings
from openedx_filters import PipelineStep

from commerce_coordinator.apps.commercetools.catalog_info.constants import EDX_PAYPAL_PAYMENT_INTERFACE_NAME

logger = logging.getLogger(__name__)


class GetPayPalPaymentReceipt(PipelineStep):
""" Purpare PayPal payment recipt """

# pylint: disable=unused-argument
def run_filter(self, psp=None, **params):

if psp == EDX_PAYPAL_PAYMENT_INTERFACE_NAME:
redirect_url = f"{settings.PAYPAL_BASE_URL}{settings.PAYPAL_USER_ACTIVITES_URL}?free_text_search={params.get('order_number')}"
return {
'redirect_url': redirect_url,
};
return None
1 change: 1 addition & 0 deletions commerce_coordinator/apps/paypal/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Constants for PayPal app tests."""
26 changes: 26 additions & 0 deletions commerce_coordinator/apps/paypal/tests/test_pipeline.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
""" Stripe Pipeline Tests"""
from unittest import TestCase
from django.conf import settings
from django.test import override_settings

from commerce_coordinator.apps.paypal.pipeline import GetPayPalPaymentReceipt


class TestGetPayPalPaymentReceipt(TestCase):
"""A pytest Test case for the CreateOrGetStripeDraftPayment Pipeline Step"""

@override_settings(
PAYPAL_BASE_URL="https://paypal.com/",
PAYPAL_USER_ACTIVITES_URL="myaccount/activities/"
)
def test_pipeline_step(self):
order_number = '123'
paypal_payment_pipe = GetPayPalPaymentReceipt("test_pipe", None)

result: dict = paypal_payment_pipe.run_filter(
edx_lms_user_id=1,
psp='paypal_edx',
order_number=order_number
)
redirect_url = f"{settings.PAYPAL_BASE_URL}{settings.PAYPAL_USER_ACTIVITES_URL}?free_text_search={order_number}"
self.assertEqual(redirect_url, result['redirect_url'])
34 changes: 19 additions & 15 deletions commerce_coordinator/apps/stripe/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from stripe.error import StripeError

from commerce_coordinator.apps.core.constants import PaymentMethod, PipelineCommand
from commerce_coordinator.apps.commercetools.catalog_info.constants import EDX_STRIPE_PAYMENT_INTERFACE_NAME, EDX_PAYPAL_PAYMENT_INTERFACE_NAME
from commerce_coordinator.apps.stripe.clients import StripeAPIClient
from commerce_coordinator.apps.stripe.constants import Currency
from commerce_coordinator.apps.stripe.exceptions import (
Expand Down Expand Up @@ -214,26 +215,29 @@ class GetPaymentIntentReceipt(PipelineStep):
""" Pull the receipt if the payment_intent is set """

# pylint: disable=unused-argument
def run_filter(self, payment_intent_id=None, **params):
def run_filter(self, payment_intent_id=None, psp=None, **params):
tag = type(self).__name__

if payment_intent_id is None:
if psp == EDX_STRIPE_PAYMENT_INTERFACE_NAME and payment_intent_id is None:
logger.debug(f'[{tag}] payment_intent_id not set, skipping.')
return PipelineCommand.CONTINUE.value

elif psp == EDX_STRIPE_PAYMENT_INTERFACE_NAME:
stripe_api_client = StripeAPIClient()
payment_intent = stripe_api_client.retrieve_payment_intent(
payment_intent_id,
["latest_charge"]
)
receipt_url = payment_intent.latest_charge.receipt_url

stripe_api_client = StripeAPIClient()

payment_intent = stripe_api_client.retrieve_payment_intent(
payment_intent_id,
["latest_charge"]
)

receipt_url = payment_intent.latest_charge.receipt_url

return {
'payment_intent': payment_intent,
'redirect_url': receipt_url
}
return {
'payment_intent': payment_intent,
'redirect_url': receipt_url
}
else:
return {
'psp': psp
}


class RefundPaymentIntent(PipelineStep):
Expand Down
7 changes: 6 additions & 1 deletion commerce_coordinator/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,8 @@ def root(*path_fragments):
'commerce_coordinator.apps.ecommerce.pipeline.GetLegacyEcommerceReceiptRedirectUrl',
'commerce_coordinator.apps.rollout.pipeline.HaltIfRedirectUrlProvided',
'commerce_coordinator.apps.commercetools.pipeline.FetchOrderDetailsByOrderNumber',
'commerce_coordinator.apps.stripe.pipeline.GetPaymentIntentReceipt'
'commerce_coordinator.apps.stripe.pipeline.GetPaymentIntentReceipt',
'commerce_coordinator.apps.paypal.pipeline.GetPayPalPaymentReceipt'
]
},
"org.edx.coordinator.lms.order.refund.requested.v1": {
Expand Down Expand Up @@ -474,3 +475,7 @@ def root(*path_fragments):
SEGMENT_KEY = None

FAVICON_URL = "https://edx-cdn.org/v3/prod/favicon.ico"

# PAYPAL SETTINIS
PAYPAL_BASE_URL = 'https://sandbox.paypal.com/'
PAYPAL_USER_ACTIVITES_URL = 'myaccount/activities/'

0 comments on commit 78e52d4

Please sign in to comment.