Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

editorial: add example of .toJSON()+fetch POST #583

Merged
merged 3 commits into from
Aug 16, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this set result = "fail"?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably... but wanted to show the "unknown" state somehow.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe do a quick parse of response.json() ? I dunno. I have a hard time understanding "unknown" in real-world systems...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, me too :( Ok, will just add a comment about "unknown" that it's an option that people can use if they need it... however, we should try to come up with something real or we should mark it "at risk".

Chrome currently does nothing with "unknown" (behaves the same as "success", in that no error is shown to the user). I don't know if we (Firefox) will do anything with "unknown" at this point.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filed #583

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed mention of "unknown", as I have a feeling it's it won't survive CR.

result = "fail";
}
await payResponse.complete(result);
}
doPaymentRequest();
</pre>
</section>
</section>
<section data-dfn-for="PaymentRequest" data-link-for="PaymentRequest">
<h2>
Expand Down