'Stripe Gpay element - Show custom message if I don't deliver to buyer address

When a user change their shipping address, an ajax request fetch the delivery charges on basis of user's postcode like this and its working fine -

paymentRequest.on('shippingaddresschange', function(ev) {       
  if (ev.shippingAddress.country !== 'GB') {
    ev.updateWith({status: 'invalid_shipping_address'});
   } else {
    // Perform server-side request to fetch shipping options
    fetch(ajaxurl, {
      ...
    }).then(function(response) {
      return response.json();
    }).then(function(result) {
      ev.updateWith({
        status: result.status,
        shippingOptions: result.supportedShippingOptions,
        total: {
              label: result.cart_label,
              amount: result.cart_total,
          }
      });
      
    })        
  }
});

The response from server is {"supportedShippingOptions":[{"id":"standard","label":"Standard Delivery","detail":"Standard delivery","amount":100}],"cart_label":"Total Amount","cart_total":305,"status":"success"}

The above things works fine. But there are few postcodes where I do not deliver so in that case I replace the ajax response "status":"success" with "status":"invalid_shipping_address"

After doing this, my user sees message "Invalid Shipping Address" and they are prompt to choose another address.

My question is - The error message "Invalid Shipping Address" causes confusion to my user because there address is correct and they get confused. So how can I show custom message for e.g. "We don't deliver to your postcode". I searched through Stripe's documentation but found nowhere. Please suggest if there is any workaround.



Solution 1:[1]

There isn't a way to display a custom message in the Google Pay display, but you can use your own code to achieve this. Something like this would work:

if (ev.shippingAddress.country !== 'GB') {
    alert('We do not ship outside GB!');
    ev.updateWith({status: 'invalid_shipping_address'});
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1