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), the retry() method of a PaymentResponse instance allows you ask a user to retry a payment.

Syntax

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

Returns

A Promise.

Parameters

errorFields
An object, with the following properties:
payer
A PayerErrorFields object describing validation errors specific to the payer details.
shippingAddress
An AddressErrorFields object describing validation errors specific to the shipping address.
error
A DOMString that generally describes an error with the data provided.

Examples

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

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

function fixField(response, event, validator) {
  return new Promise(resolve => {
    // Browser keeps calling this until promise resolves.
    response.addEventListener(event, async function listener(ev) {
      const promiseToValidate = validator(response);
      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

Contributors to this page: chrisdavidmills, sideshowbarker, marcoscaceres
Last updated by: chrisdavidmills,