Recurring payments Yandex Pay
Yandex Pay enables merchants to accept recurring payments.
For this purpose, Yandex Pay provides a payment token that gives an option to make multiple payments for a given amount.
Recurring payments can be mandatory or optional. On a Yandex Pay form, the user is asked to agree to their account being periodically debited by a given amount.
Mandatory consent to a recurring payment
In that case the user will see a message on the form and will need to provide a mandatory consent to recurring payments to the merchant.
paymentData.recurringOptions = {
type: YaPay.RecurringType.Recurring,
optional: false,
};
Optional consent to a recurring payment
In this case, the user will provide a preferable payment option in the form.
In the response, along with the token, you'll get a pointer to the user's consent to recurring payments. The payment token will be either recurring or one-time.
paymentData.recurringOptions = {
type: YaPay.RecurringType.Recurring,
optional: true,
};
function onPaymentProcess(event) {
// A pointer to the user's consent to recurring payments
// The payment token type: recurring or regular
alert(`Recurring payment approve — ${event.recurringInfo.recurring}`);
}
Example
let activeSession;
function onYaPayLoad(type, optional) {
const YaPay = window.YaPay;
if (activeSession) {
activeSession.destroy();
activeSession = null;
}
// Payment details
const paymentData = {
env: YaPay.PaymentEnv.Sandbox,
version: 2,
countryCode: YaPay.CountryCode.Ru,
currencyCode: YaPay.CurrencyCode.Rub,
merchant: {
id: '<YOUR_MERCHANT_ID>',
name: 'test-merchant-name',
url: 'https://test-merchant-url.ru',
},
order: {
id: 'order-id',
total: { amount: '43980.00' },
items: [
{ label: 'Yandex Station, black', amount: '26990.00' },
{ label: 'Yandex Station 2, antracite', amount: '16990.00' },
],
},
paymentMethods: [
{
type: YaPay.PaymentMethodType.Card,
gateway: 'test-gateway',
gatewayMerchantId: 'test-gateway-merchant-id',
allowedAuthMethods: [YaPay.AllowedAuthMethod.PanOnly],
allowedCardNetworks: [
YaPay.AllowedCardNetwork.Visa,
YaPay.AllowedCardNetwork.Mastercard,
YaPay.AllowedCardNetwork.Mir,
YaPay.AllowedCardNetwork.Maestro,
YaPay.AllowedCardNetwork.VisaElectron,
],
},
],
};
// Specify a recurring payment
if (type === 'RECURRING') {
paymentData.recurringOptions = {
type: YaPay.RecurringType.Recurring,
optional: optional,
};
}
// Handler to obtain a payment token
function onPaymentProcess(event) {
// A pointer to user consent to recurring payments
// And, accordingly, the type of the payment token: recurring or one-time
console.log(`Recurring payment approve — ${event.recurringInfo.recurring}`);
}
// Payment error handler
function onPaymentError(event) {
console.log(`Payment error — ${event.reason}`);
}
// Payment cancellation handler
function onPaymentAbort(event) {}
// Create a payment session
YaPay.createSession(paymentData, {
onProcess: onPaymentProcess,
onAbort: onPaymentAbort,
onError: onPaymentError,
})
.then(function (paymentSession) {
activeSession = paymentSession;
paymentSession.mountButton(document.querySelector('#button_container'), {
type: YaPay.ButtonType.Pay,
theme: YaPay.ButtonTheme.Black,
width: YaPay.ButtonWidth.Auto,
});
})
.catch(function (err) {
// Couldn't create a payment session.
});
}