Search code examples
pythonpaypalkivypaypal-rest-sdk

How to capture an order from PayPal in Python


I've been trying to understand how exactly the capturing process of the PayPal SDK works. I'm currently working on a Python Kivy Mobile App with a PayPal Checkout option. I've been trying to make this example here to work: https://github.com/paypal/Checkout-Python-SDK#capturing-an-order but get this error when executed:

422
{'Cache-Control': 'max-age=0, no-cache, no-store, must-revalidate', 'Content-Length': '584', 'Content-Type': 'application/json', 'Date': 'Thu, 04 Mar 2021 18:32:56 GMT', 'Paypal-Debug-Id': 'd092a377ca029'}
{"name":"UNPROCESSABLE_ENTITY","details":[{"issue":"ORDER_NOT_APPROVED","description":"Payer has not yet approved the Order for payment. Please redirect the payer to the 'rel':'approve' url returned as part of the HATEOAS links within the Create Order call or provide a valid payment_source in the request."}],"message":"The requested action could not be performed, semantically incorrect, or failed business validation.","debug_id":"d092a377ca029","links":[{"href":"https://developer.paypal.com/docs/api/orders/v2/#error-ORDER_NOT_APPROVED","rel":"information_link","method":"GET"}]}

From what I understand this happens because I both try to create and capture the order at the same time. How could I make it that the capture only starts when the customer has approved so I would most likely not see this error message? Any help is appreciated!

.py

def PayPal(self):
    client_id = "ID"
    client_secret = "SECRET"

    environment = SandboxEnvironment(client_id=client_id, client_secret=client_secret)
    client = PayPalHttpClient(environment)

    request = OrdersCreateRequest()
    request.prefer("return=representation")

    request.request_body({
        "application_context": {
            "return_url": ""},

        "intent": "CAPTURE",

        "purchase_units": [{
            "amount": {
                "currency_code": "CAD",
                "value": str(App.get_running_app().cart)
            }}]})

    try:
        response = client.execute(request)
        print("Order With Complete Payload:")
        print("Status Code:", response.status_code)
        print("Status:", response.result.status)
        print("Order ID:", response.result.id)
        print("Intent:", response.result.intent)
        print("Links:")
        for link in response.result.links:
            print('\t{}: {}\tCall Type: {}'.format(link.rel, link.href, link.method))
            print("Total Amount: {} {}".format(response.result.purchase_units[0].amount.currency_code,
                                               response.result.purchase_units[0].amount.value))
            order = response.result
            print(order)
    except IOError as ioe:
        print(ioe)
        if isinstance(ioe, HttpError):
            print(ioe.status_code)
    webbrowser.open("https://www.sandbox.paypal.com/checkoutnow?token=" + response.result.id)

    # Capture order
    request = OrdersCaptureRequest(order_id= response.result.id)

    try:
        response = client.execute(request)
        order = response.result.id

    except IOError as ioe:
        if isinstance(ioe, HttpError):
            print(ioe.status_code)
            print(ioe.headers)
            print(ioe)
        else:
            print(ioe)

Solution

  • The capture should only be done after the customer goes through an approval flow (at PayPal) and returns to your app. If you specify a return_url when you create the order, this can be set to a deeplink back to your app, which should be an intent that then calls the function that only then does the capture.