title: How to create a variable recurring payment in paypal in node.js
body: <p>I've signed up to a paypal business account. I've been trying to create a variable recurring payment using paypal. But with no luck. I could not find any working code that can accomplish this. </p>
  
  <p>Things I've tried:</p>
  
  <ol>
  <li>Subscriptions: I've checkout subscriptions too but subscriptions seem to be having fixed amount.</li>
  <li>Paypal-rest-sdk: Paypal rest sdk won't let me change the <code>payment_definitions</code> most probably because it is just creating a standard subscription and not a variable one</li>
  </ol>
  
  <pre><code>(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) =&gt; {
          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));
                              }
                      });
              }
      });
  })();
  
  </code></pre>
  
  <p>Above code throws </p>
  
  <pre><code>{ 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 }
  </code></pre>
  
  <p>Some links I've found interesting are:</p>
  
  <ol>
  <li><a href="https://www.paypal-community.com/t5/Merchant-services-Archive/Subscription-with-variable-amount-per-month/td-p/49864" rel="nofollow noreferrer">https://www.paypal-community.com/t5/Merchant-services-Archive/Subscription-with-variable-amount-per-month/td-p/49864</a></li>
  <li><a href="https://developer.paypal.com/docs/classic/express-checkout/integration-guide/ECRecurringPayments/#" rel="nofollow noreferrer">https://developer.paypal.com/docs/classic/express-checkout/integration-guide/ECRecurringPayments/#</a></li>
  </ol>
  
  <p>If some one could help me with some working code that creates a new recurring payment subscription / plan I'd really appreciate it. </p>