Re: [webpayments] What is the appropriate conversational pattern for the API? (#55)

@zkoch,

Here's a first quick attempt at combining the approaches:

```js
var checkout = new Checkout();
checkout
  .send('paymentItem', paymentItems)  // send line item estimate to UA
  .request('shippingAddress')         // request shippingAddress from UA
  .addEventListener('shippingAddressChange', shippingAddressChanged)
  .addEventListener('shippingOptionChange', shippingOptionChanged)
  .start()                            // start the checkout UI
  .then(finishCheckout);              // checkout UI has collected the info

function shippingAddressChanged(event) {
  var checkoutDetails = event.checkoutDetails;

  if(!isAddressAcceptable(checkoutDetails.address)) {
    return checkout.cancel('We cannot ship to your address.');
  }
  
  // send updated payment items and request a shipping option selection;
  // each function here can return a Promise
  event
    .send('paymentItem', getPaymentItems(checkoutDetails))
    .request('shippingOption', getShippingOptions(checkoutDetails.address));
}

function getPaymentItems(checkoutDetails) {
  // post checkout details to shipping calculation endpoint and return
  // updated payment items in a Promise
  return fetch('/calc-shipping', {
    method: 'POST',
    body: JSON.stringify(checkoutDetails)
  }).then(function(res) {
    return res.json();
  });
}

function getShippingOptions(checkoutDetails) {
  // ... determine shipping options from checkoutDetails.shippingAddress ...
  return Promise.resolve(shippingOptions);
}

function finishCheckout(checkoutDetails) {
  // asynchronously create a payment request from the checkout details
  customCreatePaymentRequest(checkoutDetails).then(function(paymentRequest) {
    return checkout.finish(paymentRequest);
  }).then(function(acknowledgement) {
    // handle acknowledgement from payment app
  });
}
```

---
Reply to this email directly or view it on GitHub:
https://github.com/w3c/webpayments/issues/55#issuecomment-183484374

Received on Friday, 12 February 2016 20:52:16 UTC