1

I am trying to integrate the payment request api, but I am missing something here.. How to validate payments that were made using the api? When the user pays my callback is executed, but how do I know the payment is complete? Here is my code.

paymentRequest.show()
    .then((paymentResponse) => {
        fetch('http://validate-payment/api')
        .then((response) => {
            return response.json();
        })
        .then((json) => {
            return paymentResponse.complete('fail'); // Hardcode fail
        })
        .catch((error) => {
            reject();
        })
    })
    .catch((error) =>{
        console.log(error.message)
    });
sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
nikksan
  • 3,341
  • 3
  • 22
  • 27
  • Check for `response.ok` and if that’s true, then call `response.json()`; else call `.complete('fail')`. – sideshowbarker Oct 15 '17 at 03:49
  • Please note that this API is the "Payment Request API" and it's not related to Google. Please do not call this "Google Payment Request API". https://medium.com/dev-channel/addressing-common-misconceptions-about-the-payment-request-api-4d0db51dae75 – agektmr Oct 16 '17 at 05:45

1 Answers1

2

When you receive paymentResponse object, this doesn't mean the payment is done. You must post the information to a payment gateway like you do now to process the payment.

Obtain the payment detail with paymentResponse.details and POST it to a payment gateway (in your code it could be "validate-payment/api").

The response from the payment gateway will indicate if the payment was successful or not.

Mind PCI compliance when you work with this API (especially if you handle raw credit card information). Stripe for example does this on behalf of you but not a lot of payment gateways do similar just yet.

paymentRequest.show()
    .then((paymentResponse) => {
        var details = paymentResponse.details;
        fetch('https://validate-payment/api', {
            method: 'POST',
            body: JSON.stringify(details)
        })...
agektmr
  • 2,144
  • 15
  • 14