summaryrefslogtreecommitdiff
path: root/moneyjsx/src/upgrade.tsx
blob: ab29e34d08a78851afaee2d1eda9dee1eb861a3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import * as JSX from './jsx';
import * as user from './user';
import * as api from './api';
import $ from 'jquery';

import { Page, GroupBox, Spinner } from './components';

let stripe = null;
let card = null;
let selected_product = 2;

async function loadStripe() {
  return new Promise( function (resolve, reject ) {
    const s = document.createElement( 'script' );
    s.src = 'https://js.stripe.com/v3/';
    s.onload = resolve;
    s.onerror = reject;
    document.head.appendChild( s );
  } );
}

function showError( text: string ) {
  $( "#payment-error" ).show();
  $( "#payment-error" ).text( text );
}

async function onFormSubmit( e: Event ) {
  e.preventDefault();
  const { paymentMethod, error } = await stripe.createPaymentMethod( {
    type: 'card',
    card
  } );

  if( error )
    return showError( error.message );

  const res = await api.post( 'create-payment-intent', {
    token: localStorage.getItem( 'session' ),
    paymentMethodId: paymentMethod.id,
    subLength: selected_product
  } );

  if( res.status == 'ok' ) {
    const intent = res.paymentIntent;
    if( intent.status == 'succeeded' )
      return JSX.navigateParams( '/payment-success', {} );
    else
      return showError( "Payment failed. Contact support if the issue persists." );
  }
  else if( res.msg )
    return showError( "Error: " + res.msg );
  else
    return showError( "Payment failed. Contact support if the issue persists." );
}

function waitForStripe() {
  const iframes = $( 'iframe' );
  if( !iframes.length )
    return setTimeout( waitForStripe, 50 );

  for( let iframe of iframes ) {
    if( iframe.attributes.getNamedItem( 'name' ).value == 'hcaptcha-invisible' ) {
      $( '#upgrade-loading' ).remove();
      $( '#upgrade-inner' ).show();

      return;
    }
  }

  setTimeout( waitForStripe, 50 );
}

async function initStripe() {
  stripe = window['Stripe']( 'pk_live_51Q9JG602rmv2yeiGYNNfwFkqp5ntpXIDIMIoiwDoEOrB5IsC75WDy3XOqekvuwY6PecviSSo5ERt0umrdBdUtqhd00FyYZe5p3' );
  const font = user.settings.site_prefs.font;

  const params = {
    mode: 'billing',
    style: {
      base: {
        color: '#fff',
        fontFamily: font,
        fontSize: '15px',
        '::placeholder': {
          color: '#ccc',
        },
      },
    },
  }

  const el = getComputedStyle( document.body );
  const back = el.getPropertyValue( '--back' );
  const front = el.getPropertyValue( '--front' );
  const appearance = {
    theme: 'stripe',
    disableAnimations: true,

    variables: {
      colorPrimary: front,
      colorBackground: back,
      colorText: '#fff',
      fontFamily: font,
      fontSizeBase: '15px',
      fontSizeSm: '13px',
      spacingUnit: '2px',
      borderRadius: '0px',
    },
    rules: {
      ".Input": {
        border: 'none'
      }
    }
  }

  const elements = stripe.elements( { appearance } );
  const name = elements.create( 'address', { mode: 'billing', appearance } );
  name.mount( '#name-element' );

  card = elements.create( 'card', params );
  card.mount( '#card-element' );
}

function onProductChange( e: Event ) {
  selected_product = parseInt( (e.target as HTMLInputElement).value );
}

function SelectionRadio( params: any ) {
  return <div style="display: flex">
    { params.checked &&
      <input type="radio"
        name="product"
        value={ params.value }
        onChange={ onProductChange }
        checked="checked"
      />
    }
    { !params.checked &&
      <input type="radio"
        name="product"
        value={ params.value }
        onChange={ onProductChange }
      />
    }
    <label class="radio-label" style="margin-left: 5px">
      { params.text } <span style="color: var(--front)">-- ${ params.price }</span>
    </label>
  </div>
}

function UpgradeLoaded() {
  return <GroupBox
    title="Account upgrade"
    style="width: 90%; max-width: 550px; display: none"
    innerStyle="width: calc( 100% - 10px )"
    id="upgrade-inner"
  >
    <div style="border-bottom: 1px solid var(--front);">
      <h2 style="padding: 8px 0px; margin: 0px; font-size: 28px">Summary</h2>
      <div style="width: 60%">
        <h3 style="margin: 5px 0;">
          Product:
        </h3>
        <h4 style="margin: 5px 0">
          <span id="product-name">Axonbox premium account upgrade</span><br />
          <SelectionRadio text="1 week" price="2.70" value="1"/>
          <SelectionRadio text="1 month" price="10" value="2" checked="checked"/>
          <SelectionRadio text="1 year" price="100" value="3"/>
        </h4>
      </div>
    </div>
    <div>
      <h3 style="margin: 5px 0">
        Payment information
      </h3>
    </div>
    <form id="upgrade-form" method="POST" action="" onsubmit={ onFormSubmit }>
      <div>
        <label for="name-element">Contact information</label>
        <div id="name-element"></div>
        <label for="card-element">Credit or debit card</label>
        <div id="card-element" style="padding: 10px 0"></div>
        <div id="card-errors" role="alert"></div>
      </div>
      <div>
        <span style="display:none" id='payment-error'></span>
      </div>
      <button type="submit">Submit</button>
    </form>
  </GroupBox>
}

export default function Upgrade() {
  setTimeout( () => {
    loadStripe().then( () => {
      $( <UpgradeLoaded /> ).insertAfter( "#upgrade-loading" );
      initStripe();
      waitForStripe();
    } );
  } );

  return <Page>
    <GroupBox title="Account upgrade" id="upgrade-loading">
      <span>Loading... &nbsp;<Spinner style="display: inline" /></span>
    </GroupBox>
  </Page>
}