Checkout page

Add this page to setup Stripe payments:

module.exports = async function ($) {
  $.page.title = 'Checkout'

  async function handleCheckout(btn) {
    btn.disabled = true
    var values = serialize(btn.form)
    var data = await api({ action: 'session/create', values })
    if (data.error) {
      alert(data.error.message)
    } else {
      var result = await stripe.redirectToCheckout({
        sessionId: data.session_id
      })
      if (result.error) {
        alert(result.error.message)
      }
    }
    btn.disabled = false
  }

  return /* html */ `
    <div>
      <form onsubmit="return false">
        <input type="hidden" name="account_id" value="${$.app.config.env.account_id}">
        <p>
          <label for="email">Email</label>
          <input id="email" type="email" name="email" value="vidar@eldoy.com">
        </p>
        <p>
          <label>
            <input type="radio" name="price_id" value="price_2IZeyJKHin97Q2E7NQSrQOIe" checked>
            Premium
          </label>
          <label>
            <input type="radio" name="price_id" value="price_2IZeyiKHin97Q2E7kubhzcim">
            Basic
          </label>
        </p>
        <button onclick="handleCheckout(this)">Subscribe</button>
      </form>
    </div>
    <script>
      var stripe = Stripe('${$.app.config.payment.publishable_key}')
      ${handleCheckout}
    </script>
  `
}