Paypal Capture Issue

21 views Asked by At

I have PayPal Capture issue. It's saying:

data: {
      name: 'RESOURCE_NOT_FOUND',
      message: 'The specified resource does not exist.',
      debug_id: '87a2478a134c8',
      details: [Array],`
}

Here is my code:

app.post('/charge', async (req, res) => {
  try {
   
    let payerID = "XXXXXXXXXXX"
    let paymentID = "PAYID-XXXXXXXXXXx
    let amount = 12
    // Step 1: Get access token
    const tokenResponse = await axios.post('https://api.sandbox.paypal.com/v1/oauth2/token', 'grant_type=client_credentials', {
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Authorization': `Basic ${Buffer.from(`${user_name_auth}:${password_auth}`).toString('base64')}`
      }
    });
    console.log(tokenResponse.data.access_token)
    let accessToken = tokenResponse.data.access_token;

 
    const executeResponse = await axios.post(`https://api-m.sandbox.paypal.com/v1/payments/payment/${paymentID}/execute`, {
      "payer_id": payerID
    }, {
      headers: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${accessToken}`
      }
    })

    // Step 3: Capture transaction
    // console.log(executeResponse)
    // console.log(executeResponse.data.id)
    console.log("executeResponse.data.id")

    console.log(executeResponse.data.transactions[0].related_resources[0])

    let transactionId = executeResponse.data.transactions[0].related_resources[0].sale.id;
    // console.log(executeResponse)

    console.log(accessToken)
    console.log(transactionId)

    const captureResponse =await axios.post(`https://api-m.sandbox.paypal.com/v2/payments/authorizations/${transactionId}/capture`, {
      "amount": {
        "value": amount,
        "currency_code": "USD"
      
      },
      "final_capture": true,
    }, {
      headers: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${accessToken}`
      }
    })
      console.log(captureResponse)

  res.json({ success: true, message: "Payment captured successfully." });
}catch (error) {
  // Handle errors
  console.log("Error capturing payment:", error);
  console.log(error.message)
  res.status(500).json({ success: false, message: "Failed to capture payment.", error: error.message });
}
});

I want to charge user with payer Id and PaymentId previous used. I get error on my capture. I have /pay API for charging user first time then I am saving the PaymentId and payerId for above API. Also, Suggest some better approach for it.

Tried versioning different, I think i am passing wrong id.

0

There are 0 answers