How to use the SDK with Subscriptions? #203
-
How can I use the SDK for subscriptions (ideally Is there any minimal example I could see? I have been looking into the docs but I don't find any reference to this. Any example I use would be greatly appreciated! Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Some updates on my current implementation: I currently have the following code, which is not working: Front end form generator const mp = new MercadoPago('TEST-ABCDEF', {
locale: 'en'
});
const bricksBuilder = mp.bricks();
const renderCardPaymentBrick = async (bricksBuilder) => {
const settings = {
initialization: {
amount: 100, // total amount to be paid
payer: {
email: "",
},
},
customization: {
visual: {
style: {
customVariables: {
theme: 'default', // | 'dark' | 'bootstrap' | 'flat'
}
}
},
paymentMethods: {
maxInstallments: 1,
}
},
callbacks: {
onReady: () => {
console.log("ON READY!")
// callback called when the Brick is ready
},
onSubmit: (cardFormData) => {
console.log("CARD FORM DATA", cardFormData);
// callback called when the user clicks on the submit data button
// example of sending the data collected by our Brick to your server
return new Promise((resolve, reject) => {
fetch("/api/payment/", {
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(cardFormData)
})
.then((response) => {
// receive payment result
resolve();
})
.catch((error) => {
// handle error response when trying to create payment
reject();
})
});
},
onError: (error) => {
// callback called to all error cases related to the Brick
},
},
};
window.cardPaymentBrickController = await bricksBuilder.create('cardPayment', 'cardPaymentBrick_container', settings);
};
renderCardPaymentBrick(bricksBuilder);
}); Backend code const body: PaymentData = await request.json();
console.log("Received payment data", body);
// TODO: Set this value dynamically
const planId = "ABCDEF";
const mepaBody = {
preapproval_plan_id: planId,
reason: "Test subscription",
external_reference: "YG-1234",
payer_email: body.payer.email,
card_token_id: body.token,
auto_recurring: {
frequency: 1,
frequency_type: "months",
start_date: "2020-06-02T13:07:14.260Z",
transaction_amount: 20,
currency_id: "ARS"
},
back_url: "https://www.mercadopago.com.ar",
status: "authorized"
};
const postRequest = await axios.post("https://api.mercadopago.com/preapproval", mepaBody, { headers: { Authorization: "Bearer TEST-ABCDEFGH" } })
console.log(postRequest); I'm trying with a test card, which properly generates the token id, but it is failing when I call the backend code with Am I missing anything here? |
Beta Was this translation helpful? Give feedback.
-
Hello @Bullrich! We have an official channel that is super prepared to solve your case quickly. |
Beta Was this translation helpful? Give feedback.
Hello @Bullrich, we can't exactly show you how to create a subscription with
sdk-js
because this sdk is a frontend product so the subscription creation is not done using it.The flow is something like this:
I can see that you have already opened a new discussion at our NodeJS SDK, so my suggestion is to wait their response to understand what might be going wro…