由于身份验证凭据无效或缺少授权标头,导致身份验证失败。在nodejs paypal sdk

时间:2020-07-01 06:10:37

标签: javascript node.js reactjs paypal-subscriptions

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

  1. 在Ui Side(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."

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

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 }

在我的用户界面中,提交提交显示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
  };
  }

1 个答案:

答案 0 :(得分:0)

没有用于Subscriptions API集成的SDK,您需要实现直接的REST API调用。

PayPal-Node-SDK从不支持Subscriptions API,仅支持以前不兼容的计费API。另外,不再维护PayPal-Node-SDK。

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

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