skip to Main Content

What is the equivalent to https://developer.paypal.com/docs/api/orders/v1/#orders_cancel in the V2 of Paypal API?

After an order has been Authorised (using API v2 authorize request with ‘CREATED’ response), I want the user to be able to cancel it after the click of a button. That button runs a cancel() method.

So my question is, what request should I put in the cancel method to cancel/void the existing order using the orderID? I know I could just leave the order be in V2, but I specifically want to void/cancel it.
(Also, I can only use post, put and patch requests with the implementation I’ve been given)

Would really appreciate it if you could provide the curl command or nodeJS fetch implementation for it.

Thank you in advance!

2

Answers


  1. If the v2 order was intent:'authorize' and you ‘COMPLETED’ it to get back an authorization object, you can void the authorization using the v2/payments API.

    If, however, the order was merely ‘CREATED’ or ‘APPROVED’, there is no way to cancel or void it. Simply have your system forget about it.

    Login or Signup to reply.
  2. You can’t using V2 but V1 API is still available so you can use that API.

    Python 3.x sample:

        import requests
        from requests.auth import HTTPBasicAuth
        
        def get_access_token(client_id, client_secret):
            res = requests.post(
                'https://api-m.sandbox.paypal.com/v1/oauth2/token',
                headers={
                    'Accept': 'application/json',
                    'Accept-Language': 'en_US',
                },
                auth=HTTPBasicAuth(client_id, client_secret),
                data={'grant_type': 'client_credentials'},
            ).json()
            return res['access_token']
        
        
        def cancel_order(access_token, order_id) -> bool:
            uri = f'https://api-m.sandbox.paypal.com/v1/checkout/orders/{order_id}'
            res = requests.delete(
                uri,
                headers={
                    'Content-Type': 'application/json',
                    'Authorization': f'Bearer {access_token}'
                }
            )
        
            if not res.ok:
                raise Exception(f"nnPayPal API Call failed for url {uri}: {res.reason} ({res.text}).nFull Response:n{res}nn")
        
            return
        
        
        acc_token = get_access_token('<<client_id>>', '<<client_secret>>')
        cancel_order(acc_token, '<<order_id>>')
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search