【问题标题】:Authentication failed due to invalid authentication credentials or a missing Authorization header. in nodejs paypal sdk由于身份验证凭据无效或缺少授权标头,身份验证失败。在 nodejs 贝宝 SDK
【发布时间】:2020-10-21 12:54:15
【问题描述】:

我正在尝试在 react-nodejs 项目中实现 paypal 订阅 api。我参考 https://developer.paypal.com/docs/api/subscriptions/v1/。在那之后,我得到了“P-*********”的身份。 我试过的是:

  1. 在 Ui 端(React)中,我创建了一个事件,用于请求服务器端创建计费计划。
  2. 在服务器端 (nodejs) 我执行 billingPlan 创建和更新操作。

代码是:(nodejs)

export const paypalSubscribe = async (user, data) => {
  const customerId = user.customer,
{ invoice: invoiceId } = data;

try {
    const billingPlanAttributes = {
  description: "Create Plan for Regular",
  merchant_preferences: {
    auto_bill_amount: "yes",
    cancel_url: "http://www.cancel.com",
    initial_fail_amount_action: "continue",
    max_fail_attempts: "1",
    return_url: "http://www.success.com",
    setup_fee: {
      currency: "USD",
      value: "25"
    }
  },
  name: "Testing1-Regular1",
  payment_definitions: [
    {
      amount: {
        currency: "USD",
        value: order.price.recurringAmount
      },
      charge_models: [
        {
          amount: {
            currency: "USD",
            value: "10.60"
          },
          type: "SHIPPING"
        },
        {
          amount: {
            currency: "USD",
            value: "20"
          },
          type: "TAX"
        }
      ],
      cycles: "0",
      frequency: "MONTH",
      frequency_interval: order.billingCycle,
      name: "Regular 1",
      type: "REGULAR"
    }
  ],
  type: "INFINITE"
};

const createdBillingPlan = await new Promise((resolve, reject) => {
  Paypal.billingPlan.create(billingPlanAttributes, function (
    error,
    billingPlan
  ) {
    if (error) {
      reject(error);
    } else {
      resolve(billingPlan);
    }
  });
});

console.log("data123....", createdBillingPlan);

// update
var billing_plan_update_attributes = [
  {
    op: "replace",
    path: "/",
    value: {
      state: "ACTIVE"
    }
  }
];

console.log(
  "billing_plan_update_attributes",
  billing_plan_update_attributes
);

const updateBillingPlan = await new Promise((resolve, reject) => {
  Paypal.billingPlan.update(
    createdBillingPlan.id,
    billing_plan_update_attributes,
    function (error, response) {
      if (error) {
        reject(error);
      } else {
        resolve(response);
      }
    }
  );
});

const getBillingPlan = await new Promise((resolve, reject) => {
  Paypal.billingPlan.get(createdBillingPlan.id, function (
    error,
    updatedBillingPlan
  ) {
    if (error) {
      console.log("errr", error.response);
      reject(error);
    } else {
      console.log("updatedBillingPlan", JSON.stringify(updatedBillingPlan));
      resolve(updatedBillingPlan);
      updatedBillingPlan.redire
    }
  });
});
console.log("getBillingPlan", getBillingPlan);
return { ok: true, data: getBillingPlan };
} catch (error) {
console.log("error", error);
}
};

而我得到的 getBillingPlan 是这样的:

  { id: 'P-**************',
    state: 'ACTIVE',
    name: 'Testing1-Regular1',
    description: 'Create Plan for Regular',
    type: 'INFINITE',
    payment_definitions:
    [ { id: 'PD-0EF41434TA3045459BCMIRMA',
   name: 'Regular 1',
   type: 'REGULAR',
   frequency: 'Month',
   amount: [Object],
   cycles: '0',
   charge_models: [Array],
   frequency_interval: '1' } ],
    merchant_preferences:
   { setup_fee: { currency: 'USD', value: '25' },
 max_fail_attempts: '1',
 return_url: 'http://www.success.com',
 cancel_url: 'http://www.cancel.com',
 auto_bill_amount: 'YES',
 initial_fail_amount_action: 'CONTINUE' },
 create_time: '2020-07-01T04:18:01.008Z',
 update_time: '2020-07-01T04:18:02.031Z',
 links:
      [ { href:
    'https://api.sandbox.paypal.com/v1/payments/billing-plans/P-***********',
   rel: 'self',
   method: 'GET' } ],
   httpStatusCode: 200 
   }

当我试图打开链接数组中的链接时 即,https://api.sandbox.paypal.com/v1/payments/billing-plans/P-*************' 我得到了错误:

"Authentication failed due to invalid authentication credentials or a missing Authorization 
header."

我哪里出错了?我该如何解决这个问题并在我的项目中实现 paypal 订阅。

REST API

我将代码更改为休息 api 调用,最后我得到了这样的响应:

{ status: 'APPROVAL_PENDING',
  id: 'I-1FU83BNMBCFS',
  create_time: '2020-07-06T09:47:02Z',
  links:
       [ { href:
    'https://www.sandbox.paypal.com/webapps/billing/subscriptions? ba_token=BA-3D945638N1691194P',
   rel: 'approve',
   method: 'GET' },
 { href:
    'https://api.sandbox.paypal.com/v1/billing/subscriptions/I- 1FU83BNMBCFS',
   rel: 'edit',
   method: 'PATCH' },
 { href:
    'https://api.sandbox.paypal.com/v1/billing/subscriptions/I-1FU83BNMBCFS',
   rel: 'self',
   method: 'GET' } ],
   responseCode: 201 }

在我的 ui 端,我在提交后在新窗口中打开了批准链接,显示 404。为什么?

代码:

 window.open(URL, "_blank");

更新:订阅:

 const subscriptionString = {
    plan_id: result.id,
    start_time: "2021-11-01T00:00:00Z",
    shipping_amount: {
      currency_code: "USD",
      value: "10.00"
    },
    subscriber: {
      name: {
        given_name: "John",
        surname: "Doe"
      },
      email_address: "customer@example.com",
      shipping_address: {
        name: {
          full_name: "John Doe"
        },
        address: {
          address_line_1: "2211 N First Street",
          address_line_2: "Building 17",
          admin_area_2: "San Jose",
          admin_area_1: "CA",
          postal_code: "95131",
          country_code: "US"
        }
      }
    },
    application_context: {
      brand_name: "walmart",
      locale: "en-US",
      shipping_preference: "SET_PROVIDED_ADDRESS",
      user_action: "SUBSCRIBE_NOW",
      payment_method: {
        payer_selected: "PAYPAL",
        payee_preferred: "IMMEDIATE_PAYMENT_REQUIRED"
      },
      return_url: "https://example.com/returnUrl",
      cancel_url: "https://example.com/cancelUrl"
    }
  },
  options = {
    url: "https://api.sandbox.paypal.com/v1/billing/subscriptions",
    method: "POST",
    headers: headers,
    body: JSON.stringify(subscriptionString)
  },
  activateResult = await payment.callPayaplApi(options);


  return {
      ok: true,
      data: activateResult
  };
  }

【问题讨论】:

    标签: javascript node.js reactjs paypal-subscriptions


    【解决方案1】:

    订阅 API 集成没有 SDK,您需要实现直接 REST API 调用。

    PayPal-Node-SDK 从不支持订阅 API,只支持不兼容的以前的计费 API。此外,不再维护 PayPal-Node-SDK。

    您的特定身份验证错误是由其他原因引起的,但由于上述问题,不值得排除故障。

    重新开始并与直接 REST API 调用正确集成。

    【讨论】:

    猜你喜欢
    • 2019-06-14
    • 2019-09-24
    • 2020-12-09
    • 2015-05-05
    • 2015-06-01
    • 1970-01-01
    • 2021-09-24
    • 2014-01-05
    • 2014-04-06
    相关资源
    最近更新 更多