Dev Yego
Dev Yego

Reputation: 553

PayPal errors with 'Unspecified Method' on a valid METHOD argument (error code 81002)

I am trying to integrate the PayPal's in-context express checkout experience and I am stuck at this error. Here is the code I am testing out:

axios
  .post(
    "https://api-3t.sandbox.paypal.com/nvp",
    {
      USER: process.env.PAYPAL_USER,
      PWD: process.env.PAYPAL_PASSWORD,
      SIGNATURE: process.env.PAYPAL_SIGNATURE,
      METHOD: "SetExpressCheckout",
      VERSION: "124.0",
      PAYMENTREQUEST_0_CURRENCYCODE: "USD",
      PAYMENTREQUEST_0_AMT: "4.5",
      RETURNURL: "http://localhost:3000/pay",
      PAYMENTREQUEST_0_SELLERPAYPALACCOUNTID:
        "[email protected]",
    },
    {
      headers: {
        "Content-Type": "application/url-form-encoded",
      },
    }
  )
    .then((res) => {
      console.log("Got res", res.data);
    })
    .catch((err) => {
      console.error("Caught err", err);
    });

Can somebody help me spot the problem?

Upvotes: 0

Views: 99

Answers (2)

Dev Yego
Dev Yego

Reputation: 553

I figured it out: the content type is all wrong and the post data needs a different encoding which I achieved using qs package.

axios.post(
  "https://api-3t.sandbox.paypal.com/nvp",
  qs.stringify({
    USER: process.env.PAYPAL_USER,
    PWD: process.env.PAYPAL_PASSWORD,
    SIGNATURE: process.env.PAYPAL_SIGNATURE,
    METHOD: "SetExpressCheckout",
    VERSION: "124.0",
    PAYMENTREQUEST_0_CURRENCYCODE: "USD",
    PAYMENTREQUEST_0_AMT: "4.5",
    RETURNURL: "http://localhost:3000/pay",
    PAYMENTREQUEST_0_SELLERPAYPALACCOUNTID:
      "[email protected]",
  }),
  {
    headers: {
      "Content-Type": "application/x-www-form-url-encoded", // right one!
    },
  }
)
.then((res) => {
  console.log("Got res", res.data);
})
.catch((err) => {
  console.error("Caught err", err);
});

Upvotes: 2

Preston PHX
Preston PHX

Reputation: 30379

That API is at least a couple generations out of date, why not use orders V2 'Set Up Transaction' and 'Capture Transaction', documented here: https://developer.paypal.com/docs/checkout/reference/server-integration/

Upvotes: 1

Related Questions