PaymentResponse.retry()

Secure context
This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

In cases where there is something wrong with the payment response's data (e.g., you find out that the shipping address is a PO box or the credit card was declined), the retry() method of a PaymentResponse instance allows you to ask a user to retry a payment.

Syntax

try {
  await paymentRequest.retry(errorFields);
} catch (DOMException err) {
  ...
}

Returns

A Promise.

Parameters

errorFields
A PaymentValidationErrors object, with the following properties:
payer
A PayerErrors object describing validation errors specific to the payer details.
shippingAddress
An AddressErrors object describing validation errors specific to the shipping address.
error
A DOMString that generally describes an error with the data provided.
paymentMethod
An object describing payment method specific errors

Examples

async function doPaymentRequest() {
  const request = new PaymentRequest(methodData, details, options);
  const response = await request.show();
  await recursiveValidate(request, response);
  await response.complete("success");
}

// Keep validating until the data looks good!
async function recursiveValidate(request, response) {
  const promisesToFixThings = [];
  const errors = await validate(request, response);
  if (!errors) {
    return;
  }
  if (errors.shippingAddress) {
    // "shippingaddresschange" fired at request object
    const promise = fixField(request, "shippingaddresschange", shippingValidator);
    promisesToFixThings.push(promise);
  }
  if (errors.payer) {
    // "payerdetailchange" fired at response object
    const promise = fixField(response, "payerdetailchange", payerValidator);
    promisesToFixThings.push(promise);
  }
  await Promise.all([response.retry(errors), ...promisesToFixThings]);
  await recursiveValidate(request, response);
}

function fixField(requestOrResponse, event, validator) {
  return new Promise(resolve => {
    // Browser keeps calling this until promise resolves.
    requestOrResponse.addEventListener(event, async function listener(ev) {
      const promiseToValidate = validator(requestOrResponse);
      ev.updateWith(promiseToValidate);
      const errors = await promiseToValidate;
      if (!errors) { // yay! fixed!
        event.removeEventListener(event, listener);
        resolve();
      }
    });
  });
}

doPaymentRequest();

Specifications

Specification Status Comment
Payment Request API
The definition of 'retry()' in that specification.
Candidate Recommendation Initial definition.

Browser Compatibility

No compatibility data found. Please contribute data for "api.PaymentRequest.retry" (depth: 1) to the MDN compatibility data repository.

See also

Document Tags and Contributors

Last updated by: marcoscaceres,