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

Need short example of serializing response #576

Closed
marcoscaceres opened this issue Aug 7, 2017 · 6 comments · Fixed by #583
Closed

Need short example of serializing response #576

marcoscaceres opened this issue Aug 7, 2017 · 6 comments · Fixed by #583

Comments

@marcoscaceres
Copy link
Member

Be nice to have a short example of response.toJSON() and POSTing it with fetch() 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.

@rsolomakhin
Copy link
Collaborator

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

@dlongley
Copy link

dlongley commented Aug 14, 2017

@rsolomakhin,

Note that the catch for show() won't catch any errors with fetch. Add return before fetch to make that happen if that's what's intended. Potentially the same problem with complete but maybe that is behaving how you want ... not really sure what the exact intent here is.

@rsolomakhin
Copy link
Collaborator

Yep, I should really have a separate catch() for my fetch() call.

@dlongley
Copy link

We could also just write the example with async/await :)

@marcoscaceres
Copy link
Member Author

Thanks, @rsolomakhin! Can definitely use this. Will adapt it.

@marcoscaceres
Copy link
Member Author

marcoscaceres commented Aug 15, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants