Getting customer data
If you need the email and/or name of the payer, Yandex Pay can pass such data. For example, you might need it to register the order, send receipts, or payment details.
This way, you can skip requesting this data on your site and immediately let the user pay for the order, which will improve your conversion rate.
Requesting payer's email
You can add the requiredFields.billingContact
section to PaymentData
, specifying the applicable data.
const paymentData = {
// ... Other parameters
requiredFields: {
billingContact: {
email: true, // Return the email, false by default
name: true, // Return the Name, false by default
},
},
}
Example
let activeSession;
function onYaPayLoad(requestEmail, requestName) {
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,
],
},
],
// Notify that we need to get the payer's email
requiredFields: {
billingContact: {
email: requestEmail,
name: requestName,
},
},
};
// Handler to obtain a payment token
function onPaymentProcess(event) {
// Payer email and name
console.log(`Email — ${event.billingContact.email}\nName — ${event.billingContact.name}`);
}
// 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.
});
}