首页 > 解决方案 > PayPal 订阅和 webhook

问题描述

我制作了一个网站,其中包含我要收费的服务。我想创建一个 PayPal 订阅。我需要将该订阅与我的后端(firebase 函数 - node.js)连接起来,这样我就可以更改数据库中的一些数据,以便为我的用户提供不同的内容,具体取决于他们是否付费。我想为我的订阅使用 PayPal 按钮,但我找不到将该按钮与我的后端连接的方法,因此 PayPal 按钮似乎不是我的问题的最佳选择。我无法使用 Stripe,因为我所在的国家/地区不支持它。您能否为我的订阅付款提供不同的解决方案或展示如何使用 PayPal?

标签: node.jspaypalpaypal-subscriptions

解决方案


您可以将Paypal Node SDK用于您的用例,而不是依赖可嵌入的 Paypal 订阅按钮。SDK 将为您提供与 NodeJs 的更好集成。

基本上有两个步骤可以做到这一点: 1.) 定义计费计划对象 计费计划对象定义订阅计划,包括周期数、支付频率、任何设置费用等。

var billingPlanAttribs = {
  name: 'Food of the World Club Membership: Standard',
  description: 'Monthly plan for getting the t-shirt of the month.',
  type: 'fixed',
  payment_definitions: [{
    name: 'Standard Plan',
    type: 'REGULAR',
    frequency_interval: '1',
    frequency: 'MONTH',
    cycles: '11',
    amount: {
      currency: 'USD',
      value: '19.99'
    }
  }],
  merchant_preferences: {
    setup_fee: {
      currency: 'USD',
      value: '1'
    },
    cancel_url: 'http://localhost:3000/cancel',
    return_url: 'http://localhost:3000/processagreement',
    max_fail_attempts: '0',
    auto_bill_amount: 'YES',
    initial_fail_amount_action: 'CONTINUE'
  }
};

当然,您需要将 cancel_url 和 return_url 更改为您的实际 Firebase 函数端点(或者 localhost,如果您出于开发目的在 localhost 中运行函数)

2.) 创建并激活计费计划,因此一旦您创建或定义了计费 - 您将需要创建该对象并激活计费计划,如下所示:

paypal.billingPlan.create(billingPlanAttribs, function (error, billingPlan){
  var billingPlanUpdateAttributes;

  if (error){
    console.error(JSON.stringify(error));
    throw error;
  } else {
    // Create billing plan patch object
    billingPlanUpdateAttributes = [{
      op: 'replace',
      path: '/',
      value: {
        state: 'ACTIVE'
      }
    }];

    // Activate the plan by changing status to active
    paypal.billingPlan.update(billingPlan.id, billingPlanUpdateAttributes, function(error, response){
      if (error){
        console.error(JSON.stringify(error));
        throw error;
      } else {
        console.log('Billing plan created under ID: ' + billingPlan.id);
      }
    });
  }
});

同样,所有这些都记录在Paypal 的 Developer Section中。

这也是他们使用 NodeJs 的 github 示例的链接(与 Firebase 函数相同的底层后端)


推荐阅读