0
votes

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.

Things I've tried:

  1. Subscriptions: I've checkout subscriptions too but subscriptions seem to be having fixed amount.
  2. Paypal-rest-sdk: Paypal rest sdk won't let me change the payment_definitions most probably because it is just creating a standard subscription and not a variable one
(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));
                            }
                    });
            }
    });
})();

Above code throws

{ 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 }

Some links I've found interesting are:

  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/#

If some one could help me with some working code that creates a new recurring payment subscription / plan I'd really appreciate it.

1

1 Answers

0
votes

No, you are wrong subscriptions are not fixed price. There is an option like seat-based pricing in subscriptions. All you need to do is modify your JSON when you creating a plan. Here is an example plan JSON for 1 USD base price.

{
    "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

}

The most important part in here is quantity_supported": true, it indicates that you can give quantity property when creating a subscription. Lets say you want to create a subscription with 12 USD then all you need to do is give quantity as 12. When creating a subscription your JSON may like this

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

Hope it helps.