-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add auto refund feature for paypal
- Loading branch information
1 parent
6892ab1
commit 5734a07
Showing
7 changed files
with
148 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import logging | ||
|
||
from django.conf import settings | ||
from paypalserversdk.http.auth.o_auth_2 import ClientCredentialsAuthCredentials | ||
from paypalserversdk.logging.configuration.api_logging_configuration import ( | ||
LoggingConfiguration, | ||
RequestLoggingConfiguration, | ||
ResponseLoggingConfiguration, | ||
) | ||
from paypalserversdk.paypalserversdk_client import PaypalserversdkClient | ||
from paypalserversdk.controllers.orders_controller import OrdersController | ||
from paypalserversdk.controllers.payments_controller import PaymentsController | ||
from paypalserversdk.api_helper import ApiHelper | ||
|
||
|
||
class PayPalClient: | ||
def __init__(self): | ||
self.paypal_client: PaypalserversdkClient = PaypalserversdkClient( | ||
client_credentials_auth_credentials=ClientCredentialsAuthCredentials( | ||
o_auth_client_id=settings.PAYMENT_PROCESSOR_CONFIG['edx']['paypal']['client_id'], | ||
o_auth_client_secret=settings.PAYMENT_PROCESSOR_CONFIG['edx']['paypal']['client_secret'], | ||
), | ||
logging_configuration=LoggingConfiguration( | ||
log_level=logging.INFO, | ||
# Disable masking of sensitive headers for Sandbox testing. | ||
# This should be set to True (the default if unset)in production. | ||
mask_sensitive_headers=False, | ||
request_logging_config=RequestLoggingConfiguration( | ||
log_headers=True, log_body=True | ||
), | ||
response_logging_config=ResponseLoggingConfiguration( | ||
log_headers=True, log_body=True | ||
), | ||
), | ||
) | ||
|
||
|
||
def refund_order(self, order_id): | ||
paypal_client = self.paypal_client | ||
orders_controller: OrdersController = paypal_client.orders | ||
payments_controller: PaymentsController = paypal_client.payments | ||
|
||
order = orders_controller.orders_get({"id": order_id}) | ||
print("order", order) | ||
|
||
capture_id = order.body.purchase_units[0].payments.captures[0].id | ||
|
||
collect = {"capture_id": capture_id, "prefer": "return=minimal"} | ||
result = payments_controller.captures_refund(collect) | ||
|
||
paypal_capture_id = None | ||
|
||
#TODO will move this code in utils and reuse this for webhook case too | ||
result = ApiHelper.json_serialize(result.body) | ||
for link in result.get('refund_urls'): | ||
if link.get("rel") == "up" and "captures" in link.get("href"): | ||
paypal_capture_id = link.get("href").split("/")[-1] | ||
break | ||
|
||
return {"paypal_capture_id": paypal_capture_id} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters