Skip to content

Commit

Permalink
editorial: add example of .toJSON()+fetch POST (#583)
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcos Cáceres authored Aug 16, 2017
1 parent a5e82c6 commit 88d2836
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,39 @@ <h3>
};
</pre>
</section>
<section data-link-for="PaymentResponse">
<h3>
POSTing payment response back to a server
</h3>
<p>
It's expected that data in a <a>PaymentResponse</a> will be POSTed
back to a server for processing. To make this as easy as possible,
<a>PaymentResponse</a> provides a <a>toJSON()</a> method that
serializes the object directly into JSON. This makes it trivial to
POST the resulting JSON back to a server using the <a data-cite=
"fetch">Fetch API</a>:
</p>
<pre class="example">
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();
</pre>
</section>
</section>
<section data-dfn-for="PaymentRequest" data-link-for="PaymentRequest">
<h2>
Expand Down

0 comments on commit 88d2836

Please sign in to comment.