payment-request-api

Validate Google Payment Request API


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

Solution

  • 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)
            })...