-
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
220d511
commit f651293
Showing
7 changed files
with
122 additions
and
7 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,53 @@ | ||
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.PAYPAL_CLIENT_ID, | ||
o_auth_client_secret=settings.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 | ||
# get order | ||
order = orders_controller.orders_get({"id": order_id}) | ||
print("order", order) | ||
# get capture id | ||
capture_id = order.body.purchase_units[0].payments.captures[0].id | ||
# refund the capture | ||
collect = {"capture_id": capture_id, "prefer": "return=minimal"} | ||
result = payments_controller.captures_refund(collect) | ||
|
||
print("result:", result) | ||
return ApiHelper.json_serialize(result.body) | ||
|
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