首页 > 解决方案 > 如何在 node.js 中的 paypal 中创建可变的定期付款

问题描述

我已经注册了一个贝宝商业账户。我一直在尝试使用贝宝创建可变的定期付款。但没有运气。我找不到任何可以完成此操作的工作代码。

我尝试过的事情:

  1. 订阅:我也结帐订阅,但订阅似乎有固定数量。
  2. Paypal-rest-sdk:Paypal rest sdk 最有可能不会让我更改,payment_definitions因为它只是创建标准订阅而不是可变订阅
(async function() {

    const paypal = require("paypal-rest-sdk");

    paypal.configure({
        'mode': 'sandbox', //sandbox or live
        'client_id': process.env.PAYPAL_CLIENT_ID,
        'client_secret': process.env.PAYPAL_SECRET_KEY
    });

    var 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": "100"
                        },
                        "charge_models": [
                                {
                                        "amount": {
                                                "currency": "USD",
                                                "value": "10.60"
                                        },
                                        "type": "SHIPPING"
                                },
                                {
                                        "amount": {
                                                "currency": "USD",
                                                "value": "20"
                                        },
                                        "type": "TAX"
                                }
                        ],
                        "cycles": "0",
                        "frequency": "MONTH",
                        "frequency_interval": "1",
                        "name": "Regular 1",
                        "type": "REGULAR"
                }
        ],
        "type": "INFINITE"
    };

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

    // update
    var billing_plan_update_attributes = [
    {
            "op": "replace",
            "path": "/",
            "value": {
                    "payment_definitions": [
                        {
                            ...createdBillingPlan.payment_definitions[0],
                            "amount": {
                                "currency": "INR",
                                "value": 100
                            }
                        }
                ]
            }   
    }
    ];

    console.log(billing_plan_update_attributes);

    paypal.billingPlan.update(createdBillingPlan.id, billing_plan_update_attributes, function (error, response) {
            if (error) {
                    console.log(error.response);
                    throw error;
            } else {
                    paypal.billingPlan.get(createdBillingPlan.id, function (error, updatedBillingPlan) {
                            if (error) {
                                    console.log(error.response);
                                    throw error;
                            } else {
                                    console.log(JSON.stringify(updatedBillingPlan));
                            }
                    });
            }
    });
})();

上面的代码抛出

{ name: 'BUSINESS_VALIDATION_ERROR',
  details:
   [ { field: 'payment_definitions',
       issue: 'patch is not supported for this field.' } ],
  message: 'Validation Error.',
  information_link:
   'https://developer.paypal.com/docs/api/payments.billing-plans#errors',
  debug_id: 'someid',
  httpStatusCode: 400 }

我发现一些有趣的链接是:

  1. https://www.paypal-community.com/t5/Merchant-services-Archive/Subscription-with-variable-amount-per-month/td-p/49864
  2. https://developer.paypal.com/docs/classic/express-checkout/integration-guide/ECRecurringPayments/#

如果有人可以帮助我创建一些创建新的定期付款订阅/计划的工作代码,我将非常感激。

标签: paypalpaypal-subscriptions

解决方案


不,你错了订阅不是固定价格。订阅中有一个选项seat-based pricing。您需要做的就是在创建计划时修改 JSON。这是 1 美元基本价格的示例计划 JSON。

{
    "product_id": "PROD-60L70341GH758414Y",
    "name": "Basic Plan",
    "description": "Basic plan",
    "billing_cycles": [
        {
          "frequency": {
            "interval_unit": "MONTH",
            "interval_count": 1
          },
          "tenure_type": "REGULAR",
          "sequence": 1,
          "total_cycles": 12,
          "pricing_scheme": {
            "fixed_price": {
              "value": "1",
              "currency_code": "USD"
            }
          }
        }
      ],
    "payment_preferences": {
      "service_type": "PREPAID",
      "auto_bill_outstanding": true,
      "setup_fee_failure_action": "CONTINUE",
      "payment_failure_threshold": 3
    },
    "quantity_supported": true

}

这里最重要的部分是quantity_supported": true,它表示您可以quantity在创建订阅时赋予属性。假设您想创建一个 12 美元的订阅,那么您只需提供 12 美元quantity。创建订阅时,您的 JSON 可能会这样

   'plan_id': 'P-6AG14992W7150444HLUYGM5I',
   'quantity': "12"

希望能帮助到你。


推荐阅读