diff --git a/index.html b/index.html index 100b6b47..f90d167b 100644 --- a/index.html +++ b/index.html @@ -444,6 +444,39 @@

}; +
+

+ POSTing payment response back to a server +

+

+ It's expected that data in a PaymentResponse will be POSTed + back to a server for processing. To make this as easy as possible, + PaymentResponse provides a toJSON() method that + serializes the object directly into JSON. This makes it trivial to + POST the resulting JSON back to a server using the Fetch API: +

+
+          async function doPaymentRequest() {
+            const payRequest = new PaymentRequest(methodData, details, options);
+            const payResponse = await payRequest.show();
+            let result = "";
+            try {
+              const httpResponse = await fetch("/process-payment", {
+                method: "POST",
+                headers: { "Content-Type": "application/json" },
+                body: payResponse.toJSON(),
+              });
+              result = httpResponse.ok ? "success" : "fail";
+            } catch (err) {
+              console.error(err);
+              result = "fail";
+            }
+            await payResponse.complete(result);
+          }
+          doPaymentRequest();
+        
+