Error when creating subscription with paypal checkout advanced nodeJs

un-sam-sauvage
Contributor
Contributor

Hello

I'm trying to integrate the advanced checkout from paypal and I'm trying to make a subscription.

I'm getting an error in the client console 

Uncaught Error: Expected an subscription id to be passed to createSubscription

but I console.log() the result of the request and I got a subscription Id returned.

 

client-side-code :

		createSubscription: async function (data, actions) {
			await fetch("/api/subscription", {
			method: "POST",
			headers: {
				"Content-Type": "application/json"
			},
			body : JSON.stringify({
				code_duree : "6",
				produits : "Action coup de poing",
				montantttc : "44.44",
				prenom : myFirstName,
				nom : myName,
				email : myEmailAdress,
				tel : "0733254789",
				ap : "3 rue des jardins",
				ville : "Cholet",
				cp : "49300",
				code_pays : "FR",
				bc : "prenom=myFirstName\nnom=myName",
				url_ok : myURLOK,
				url_ko : myURLOKO
			})
		}).then(data =>{
			console.log(data);
			return data.json();
		}).then(dataJson =>{
			console.log(dataJson.id);
			return dataJson.id;
		})

 

paypal-api.js

export async function createSubscription(pa_data) {
	const accessToken = await generateAccessToken();
	const url = `${base}/v1/billing/subscriptions`;
	const planID = await createPlan(pa_data);
	const response = await fetch(url, {
		method: "post",
		headers: {
			"Content-Type": "application/json",
			Authorization: `Bearer ${accessToken}`,
		},
		body: JSON.stringify({
			plan_id : planID,
			subscriber: {
				name: {
					given_name: pa_data["prenom"],
					surname: pa_data["nom"]
				},
				email_address: pa_data["email"],
				phone: {
					phone_number: {
						national_number: pa_data["tel"]
					}
				}, 
				shipping_address: {
					name: {
						full_name: pa_data["prenom"] + " " + pa_data["nom"]
					},
					address: {
						address_line_1: pa_data["ap"],
						admin_area_2: pa_data["ville"],
						postal_code: pa_data["cp"],
						country_code: pa_data["code_pays"]
					}
				}
			}
		}),
	});
	
	return handleResponse(response);
}

 

 server-side-code :

app.post("/api/subscription", async (req, res) => {
	try {
		const sub = await paypal.createSubscription(req.body);
		console.log(sub);
		res.json(sub);
	} catch (err) {
		console.log("error" + err);
		res.status(500).send(err.message);
	}
});
Login to Me Too
0 REPLIES 0

Haven't Found your Answer?

It happens. Hit the "Login to Ask the community" button to create a question for the PayPal community.