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:
- Subscriptions: I've checkout subscriptions too but subscriptions seem to be having fixed amount.
- 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:
- https://www.paypal-community.com/t5/Merchant-services-Archive/Subscription-with-variable-amount-per-month/td-p/49864
- 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.