提问者:小点点

PayPal订阅和webhooks


我做了一个网站的服务,我正在收费。我想创建一个贝宝订阅。我需要将订阅与我的后端(firebase Functions-node.js)连接起来,这样我就可以更改数据库中的一些数据,以便根据用户是否付费来为他们提供不同的内容。我想使用贝宝按钮为我的订阅,但我无法找到一种方式连接按钮与我的后端,所以似乎贝宝按钮不是最佳的我的问题。我不能使用Stripe,因为我的国家不支持它。你能给我一个不同的解决方案为我的订阅付款或显示如何我可以使用贝宝?


共1个答案

匿名用户

你可以为你的用例使用贝宝节点SDK,而不是依赖于可嵌入的贝宝订阅按钮。SDK将为您提供与NodeJS更好的集成。

主要有两个步骤:1.)定义Billing Plan对象Billing Plan对象定义订阅计划,包括周期数,支付频率,任何设置费用等等。

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的开发者部分。

这里还有一个使用NodeJs(与Firebase函数相同的底层后端)的github示例的链接