dblock/slack-gamebot

View on GitHub
public/subscribe.html.erb

Summary

Maintainability
Test Coverage
<html>
  <head>
    <title>PlayPlay.io - Subscribe</title>
    <%= partial 'public/partials/_head.html' %>
    <script src='https://checkout.stripe.com/checkout.js'></script>
    <link rel='stylesheet' href='https://checkout.stripe.com/v3/checkout/button.css'></link>
    <%
      game = Game.where(name: request['game']).first
      team = game && game.teams.where(team_id: request['team_id']).first
      coupon = Stripe::Coupon.retrieve(request[:coupon]) if request[:coupon]
      subscription_cents = 2999
      subscription_dollars = subscription_cents.to_f / 100
      amount_cents = subscription_cents
      amount_cents -= coupon.amount_off if coupon
      amount_dollars = amount_cents.to_f / 100
    %>
  </head>
  <body style='text-align: center'>
    <p style='margin: 50px;'>
      <a href='/'><img src='img/<%= game ? game.name : 'pong' %>.png' width='120px'></a>
    </p>
    <p>
      <h3>PlayPlay.io: Subscribe</h3>
    </p>
    <p id='messages' />
    <p id='subscribe'>
      <button id='subscribeButton' class='stripe-button-el'>
          <span style='display: block; min-height: 30px;'>Pay $<%= amount_dollars %> with Card</span>
      </button>
      <p>
        <img src='/img/stripe.png' width='119' height='26'></img>
        <div class='small'>
          Questions? Contact dblock[at]dblock[dot]org or DM <a href='https://twitter.com/playplayio'>@playplayio</a>.
        </div>
      </p>
      <%= partial 'public/partials/_scripts.html' %>
      <script>
        $(document).ready(function() {

          var team = {
            id: <%= team ? "'#{team._id}'" : 'null' %>,
            game: <%= game ? "'#{game.name}'" : 'null' %>,
            name: <%= team ? "'#{team.name}'" : 'null' %>,
            subscribed: <%= !!(team && team.subscribed) %>
          };

          var coupon = {
            code: <%= coupon ? "'#{coupon.id}'" : 'null' %>
          };

          var subscription = {
            dollars: <%= subscription_dollars %>,
            cents: <%= subscription_cents %>
          };

          var pay = {
            dollars: <%= amount_dollars %>,
            cents: <%= amount_cents %>
          };

          if (team.subscribed) {
            PlayPlay.message('Team <b>' + team.name + '</b> is already subscribed to <b>' + team.game + '</b>, thank you.');
            $('#subscribeButton').remove();
          } else if (team.id && team.name && team.game && coupon.code) {
            PlayPlay.message('Subscribe team <b>' + team.name + '</b> to <b>' + team.game + '</b> for $' + pay.dollars + ' for the first year and $' + subscription.dollars + ' thereafter with coupon <b>' + coupon.code + '!');
          } else if (team.id && team.name && team.game) {
            PlayPlay.message('Subscribe team <b>' + team.name + '</b> to <b>' + team.game + '</b> for $29.99 a year!');
          } else {
            $('#subscribeButton').remove();
            PlayPlay.message('Missing or invalid team ID and/or game.');
          }

          var handler = StripeCheckout.configure({
            key: '<%= ENV['STRIPE_API_PUBLISHABLE_KEY'] %>',
            image: '/img/<%= game ? game.name : 'pong' %>.png',
            locale: 'auto',
            token: function(token) {
              data = {
                stripe_email: token.email,
                stripe_token: token.id,
                stripe_token_type: token.type,
                team_id: team.id
              };

              if (coupon.code) {
                data.stripe_coupon = coupon.code;
              };

              $.ajax({
                type: 'POST',
                url: '/api/subscriptions',
                data: data,
                success: function(data) {
                  PlayPlay.message('Team <b>' + team.name + '</b> successfully subscribed to <b>' + team.game + '</b>.<br>Thank you!');
                  $('#subscribeButton').remove();
                },
                error: PlayPlay.error
              });
            }
          });

          $('#subscribeButton').on('click', function(e) {
            handler.open({
              name: 'PlayPlay.io',
              description: '1 Year Subscription',
              amount: pay.cents
            });
            e.preventDefault();
          });

          $(window).on('popstate', function() {
            handler.close();
          });
        });
      </script>
    </p>
  </body>
</html>