retry() method

Jump to:

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 related to the payer details.
shippingAddress
A AddressErrorFields object, describing validation errors with the shipping address.

Example

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();

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

Document Tags and Contributors

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