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
Need short example of serializing response #576
Comments
FYI, I have sample code for this on https://emerald-eon.appspot.com/. Here's a simplified snippet: let payment = new PaymentRequest(...);
payment.show()
.then(function(instrument) {
fetch('/buy', {
method: 'POST',
headers: new Headers({'Content-Type': 'application/json'}),
body: JSON.stringify(instrument, undefined, 2),
})
.then(function(buyResult) {
if (buyResult.ok) {
return buyResult.json();
}
complete(instrument, 'fail', 'Error sending instrument to server.');
}).then(function(buyResultJson) {
complete(instrument, buyResultJson.status, buyResultJson.message);
});
})
.catch(function(error) {
console.log('Could not charge user. ' + error);
});
function complete(instrument, result, msg) {
instrument.complete(result).then(function() {
console.log(msg);
}).catch(function(error) {
console.log(error);
});
} |
Note that the |
Yep, I should really have a separate catch() for my fetch() call. |
We could also just write the example with |
Thanks, @rsolomakhin! Can definitely use this. Will adapt it. |
The above beautifully distills down to: async function doPaymentRequest() {
const payResponse = await new PaymentRequest(methods, details).show();
let result = "unknown";
try {
const httpResponse = await fetch("/buy", {
method: "POST",
headers: new Headers({ "Content-Type": "application/json" }),
body: payResponse.toJSON(),
});
result = httpResponse.ok ? "success" : "fail";
} catch (err) {
console.error(err);
}
await payResponse.complete(result);
}
doPaymentRequest(); |
Be nice to have a short example of
response.toJSON()
andPOST
ing it withfetch()
to a server. That would "tell the complete story" of how to use the API.We can slot this into the existing example for payment response.
The text was updated successfully, but these errors were encountered: