Sun Dec 24 2017
Copied to clipboard! Copy reply
  • 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
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426
  • 427
  • 428
  • 429
  • 430
  • 431
  • 432
  • 433
  • 434
  • 435
  • 436
  • 437
  • 438
  • 439
  • 440
  • 441
  • 442
  • 443
  • 444
  • 445
  • 446
  • 447
  • 448
  • 449
  • 450
  • 451
  • 452
  • 453
  • 454
  • 455
  • 456
  • 457
  • 458
  • 459
  • 460
  • 461
  • 462
  • 463
  • 464
  • 465
  • 466
  • 467
  • 468
  • 469
  • 470
  • 471
  • 472
  • 473
  • 474
  • 475
  • 476
  • 477
  • 478
  • 479
  • 480
  • 481
  • 482
  • 483
  • 484
  • 485
  • 486
  • 487
  • 488
  • 489
  • 490
  • 491
  • 492
  • 493
  • 494
  • 495
  • 496
  • 497
  • 498
  • 499
  • 500
  • 501
  • 502
  • 503
  • 504
  • 505
  • 506
  • 507
  • 508
  • 509
  • 510
  • 511
  • 512
  • 513
  • 514
  • 515
  • 516
  • 517
  • 518
  • 519
  • 520
  • 521
  • 522
  • 523
  • 524
  • 525
  • 526
  • 527
  • 528
  • 529
  • 530
  • 531
  • 532
  • 533
  • 534
  • 535
  • 536
  • 537
  • 538
  • 539
  • 540
  • 541
  • 542
  • 543
  • 544
  • 545
  • 546
  • 547
  • 548
  • 549
  • 550
  • 551
  • 552
  • 553
  • 554
  • 555
  • 556
  • 557
  • 558
  • 559
  • 560
  • 561
  • 562
  • 563
  • 564
  • 565
  • 566
  • 567
  • 568
  • 569
  • 570
  • 571
  • 572
  • 573
  • 574
  • 575
  • 576
  • 577
  • 578
  • 579
  • 580
  • 581
  • 582
  • 583
  • 584
  • 585
  • 586
  • 587
  • 588
  • 589
  • 590
  • 591
  • 592
  • 593
  • 594
  • 595
  • 596
  • 597
  • 598
  • 599
  • 600
  • 601
  • 602
  • 603
  • 604
  • 605
  • 606
  • 607
  • 608
  • 609
  • 610
  • 611
  • 612
  • 613
  • 614
  • 615
  • 616
  • 617
  • 618
  • 619
  • 620
  • 621
  • 622
  • 623
  • 624
  • 625
  • 626
  • 627
  • 628
  • 629
  • 630
  • 631
  • 632
  • 633
  • 634
  • 635
  • 636
  • 637
  • 638
  • 639
  • 640
  • 641
  • 642
  • 643
  • 644
  • 645
  • 646
  • 647
  • 648
  • 649
  • 650
  • 651
  • 652
  • 653
  • 654
  • 655
  • 656
  • 657
  • 658
  • 659
  • 660
  • 661
  • 662
  • 663
  • 664
  • 665
  • 666
  • 667
  • 668
  • 669
  • 670
  • 671
  • 672
  • 673
  • 674
  • 675
  • 676
  • 677
  • 678
  • 679
  • 680
  • 681
  • 682
  • 683
  • 684
  • 685
  • 686
  • 687
  • 688
  • 689
  • 690
  • 691
  • 692
  • 693
  • 694
  • 695
  • 696
  • 697
  • 698
  • 699
  • 700
  • 701
  • 702
  • 703
  • 704
  • 705
  • 706
  • 707
  • 708
  • 709
  • 710
  • 711
  • 712
  • 713
  • 714
  • 715
  • 716
  • 717
  • 718
  • 719
  • 720
  • 721
  • 722
  • 723
  • 724
  • 725
  • 726
  • 727
  • 728
  • 729
  • 730
  • 731
  • 732
  • 733
  • 734
  • 735
  • 736
  • 737
  • 738
  • 739
  • 740
  • 741
  • 742
  • 743
  • 744
  • 745
  • 746
  • 747
  • 748
  • 749
  • 750
  • 751
  • 752
  • 753
  • 754
  • 755
  • 756
  • 757
  • 758
  • 759
  • 760
  • 761
  • 762
  • 763
  • 764
  • 765
  • 766
  • 767
  • 768
  • 769
  • 770
  • 771
  • 772
  • 773
  • 774
  • 775
  • 776
  • 777
  • 778
  • 779
  • 780
  • 781
  • 782
  • 783
  • 784
  • 785
  • 786
  • 787
  • 788
  • 789
  • 790
  • 791
  • 792
  • 793
  • 794
  • 795
  • 796
  • 797
  • 798
  • 799
  • 800
  • 801
  • 802
  • 803
  • 804
  • 805
  • 806
  • 807
  • 808
  • 809
  • 810
  • 811
  • 812
<template>

    <v-container class="container">
        <header>
          <h3>Secure Checkout</h3>
        </header>      
     
        <v-layout row wrap>

          
          <!-- Left Side -->
          <v-flex xs12 md6 >
            <v-layout class="steps" row wrap>

              <!-- Guest Name -->
              <v-flex d-flex xs12>
                <v-card v-bind:class="steps.one.mode">
                  <v-card-text>
                    <v-form ref='form-one'>
                      <div class="step-header">                        
                        <!-- title = number, title, edit -->
                        <span class='step-title'>                          
                          <div>
                            <span class="step-number">1</span>
                            Guest's name (optional)
                          </div>
                          <a v-if="steps.one.mode == 'completed-step'" @click.prevent="editStep('one')">edit</a>
                        </span>
                      </div>
                      <div class="step-content">
                        <p class="hint">
                          If given, the guest's name will be engraved on the crate.
                        </p>
                        <v-text-field   
                          placeholder="Guest's name"                          
                          class="input-group"
                          maxlength="30"
                          v-model="guestName">            
                        </v-text-field>
                        <v-btn
                          class="step-button"
                          block
                          large
                          secondary
                          @click="goToNextStep('one','two')">
                            continue
                          </v-btn>
                      </div>
                    </v-form>
                  </v-card-text>
                </v-card>
              </v-flex>


              <!-- Shipping -->
              <v-flex d-flex xs12>
                <v-card v-bind:class="steps.two.mode">
                  <v-card-text>
                    <v-form ref='form-two'>
                      <div class="step-header">                        
                        <span class='step-title'>
                          <div>
                            <span class="step-number">2</span>
                            Shipping Address
                          </div>
                          <a v-if="steps.two.mode == 'completed-step'" @click.prevent="editStep('two')">edit</a>
                        </span>                        
                      </div>
                      <div class="step-content">
                        <!-- check to see if addresses.length.if so, show dropdown.
                        if not, show form with checkbox to save addr (if logged in) -->
                        <small>
                          Tip: You could ship the crate to your property and let
                          the guest know that there is a package waiting for them
                        </small>

                        <v-card-text>
                          <!-- Receipient Name -->
                          <v-text-field
                            maxlength="25"
                            label="Name"
                            v-model='$store.state.checkout.shippingAddress.name'
                            class="input-group">            
                          </v-text-field>

                          <!-- Saved Address Dropdown -->
                          <div v-if='addresses'>                            
                            <v-select
                              :items="addresses"
                              @change='updateShippingAddress'
                              label="Select an address"
                              item-text='street'
                              item-value='id'
                              return-object                        
                            ></v-select>
                          </div>

                          <!-- Display new address form -->
                          <a href='#""' class="toggle-form" @click.prevent='showAddressForm = true'>
                            or click to enter a new address
                          </a>

                          <!--  New Address form -->
                          <div v-if='showAddressForm' class="new-address-form">

                            <v-text-field    
                              label="Address"
                              v-model='$store.state.checkout.shippingAddress.street'
                              :rules="rules.street"
                              required
                              class="input-group">            
                            </v-text-field>
                          
                            <v-text-field   

                              label="City"
                              v-model='$store.state.checkout.shippingAddress.city'
                              :rules="rules.city"
                              required
                              class="input-group">            
                            </v-text-field>                      
                          
                            <v-select
                              :items="states"
                              :rules="rules.state"
                              v-model='$store.state.checkout.shippingAddress.state'
                              label="Select State"
                              single-line
                              required                          
                            ></v-select>                      

                            <v-text-field    
                              label="Zip"
                              v-model='$store.state.checkout.shippingAddress.zip'
                              :rules="rules.zip"
                              required
                              class="input-group">            
                            </v-text-field>

                            <v-checkbox label="Save this address for future use" v-model='saveAddress'></v-checkbox>
                          </div>
                        </v-card-text>
                          
                        <v-btn
                            class="step-button"
                            block
                            large
                            secondary
                            @click="goToNextStep('two','three')">
                              continue
                            </v-btn>
                      </div>
                    </v-form>
                  </v-card-text>
                </v-card>          
              </v-flex>


              <!-- Payment -->
              <v-flex d-flex xs12>
                <v-card v-bind:class="steps.three.mode">
                  <v-card-text>
                    <v-form ref='form-three'>
                      <div class="step-header">
                        <span class='step-title'>
                          <div>
                            <span class="step-number">3</span>
                            Payment
                          </div>
                          <a v-if="steps.three.mode == 'completed-step'" @click.prevent="editStep('three')">edit</a>
                        </span>                   
                      </div>
                      <div class="step-content">
                        <v-card-text>
                          <label>
                            <div>Credit card number</div>
                            <div id='cardNumber-mount'></div>
                            <span class="icon"><i class="material-icons">credit_card</i></span>
                          </label>
                          <div class="input-error" v-if='ccNumberError'>{{ccNumberError}}</div>
                        </v-card-text>

                        <v-card-text>
                          <div>Expiration</div>
                          <div id='cardExpiry-mount'></div>  
                          <div class="input-error" v-if="ccExpiryError">{{ccExpiryError}}</div>
                        </v-card-text>

                        <v-card-text>
                          <div>Security Code</div>
                          <div id='cardCvc-mount'></div>
                          <div class="input-error" v-if='ccCvcError'>{{ccCvcError}}</div>
                        </v-card-text>

                        <v-card-text>
                          <v-text-field
                            label='Your e-mail'
                            :rules="rules.email"
                            v-model='this.email'
                            required
                            persistent-hint
                            hint="Your receipt will be sent here">
                            </v-text-field>
                        </v-card-text>

                        <v-btn
                          class="step-button"
                          block
                          large
                          secondary
                          @click='handleCreditCardSubmit'>
                            <v-icon left dark>lock</v-icon>                            
                            PAY ${{$store.getters.total}}
                          </v-btn>
                      </div>
                    </v-form>
                  </v-card-text>
                </v-card>
              </v-flex>              

            </v-layout>         
          </v-flex>


          <!-- Right side -->
          <v-flex xs12 md5 offset-md1 class="summary">
            <v-card>
              <v-card-text>
                <h5>Order Summary</h5>

                <div class="summary-items">
                  <div v-for="(crate, key) in $store.state.crates" :key='key'>
                    <div>
                      <!--
                      <input type="text" v-model='$store.state.crates[key].quantity' :value='$store.state.crates[key].quantity'> {{key}} crate
                      -->
                      <input type="text" v-model='$store.state.crates[key].quantity'> {{key}} crate
                    </div>
                  </div>                          
                </div>


                <div class="summary-premium">
                  <v-switch label="Use Premium crates?"                               
                    v-model="$store.state.usePremium"
                    color="primary"
                    hide-details>  
                  </v-switch>
                </div>    


                <div class="summary-shipping">
                  <div>
                    <h6>Shipping to:</h6>
                  </div>
                  <div v-if='$store.state.checkout.shippingAddress.name'>
                    {{ $store.state.checkout.shippingAddress.checkout.name }}
                  </div>
                  <div v-if='$store.state.checkout.shippingAddress.street'>
                    {{ $store.state.checkout.shippingAddress.street }}
                  </div>
                  <div v-if='$store.state.checkout.shippingAddress.city'>
                    {{ $store.state.checkout.shippingAddress.city }}
                    <span v-if='$store.state.checkout.shippingAddress.state'>,</span>&nbsp;
                    {{ $store.state.checkout.shippingAddress.state }}&nbsp;
                    {{ $store.state.checkout.shippingAddress.zip }}
                  </div>
                </div>
              </v-card-text>
            </v-card>
          </v-flex>

        </v-layout>
          
             
    </v-container>

</template>

<script>
import axios from 'axios'
import { isEmail, isCreditCard, isInt, isLength, isAlpha } from 'validator'

export default {
  layout: 'default',
  data ()
  {
    return {
      email: this.$store.state.user.email || '',
      addresses: [],
      // If user checks checkbox to save address in list
      saveAddress: true,
      isSubmitting: false,
      guestName: '', 
      showAddressForm: false,
      // shippingAddress: {
      //   name: '',
      //   street: '',
      //   city: '',
      //   state: '',
      //   zip: '',
      //   country: 'us',
      // },
      
      // Initial step states. 
      steps: {
        "one": {
          mode: 'active-step'
        },
        "two": {
          mode: 'inactive-step'
        },
        "three": {
          mode: 'inactive-step'
        },
      },
      
      // Applied when user focus and leaves a field, or submits form.
      rules: {
        email: [
          v => !!v || 'E-mail is required',
          v => isEmail(v) || 'This e-mail is not valid'
        ],
        name: [
          v => !!v || 'Name is required',
        ],
        street: [
          v => !!v || 'City is required',
          v => isLength(v, { min: 2 }) || 'This field is required'
        ],
        zip: [
          v => !!v || 'Zip code is required',
          v => /[0-9a-zA-Z]+/i.test(v) || 'Enter a valid zip code'
        ],
        city: [
          v => !!v || 'City is required',
          v => isLength(v, { min: 1 }) || 'Enter a valid city'
        ],
        state: [
          v => !!v || 'This field is required',
          v => isAlpha(v) && isLength(v, { min: 2, max: 2 }) || 'Use 2 letters', 
        ]
        
      },
      states: [
        "AL", "AK", "AS", "AZ", "AR", "CA", "CO", "CT", 
        "DE", "DC", "FM", "FL", "GA", "GU", "HI", "ID", 
        "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MH", 
        "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", 
        "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "MP", 
        "OH", "OK", "OR", "PW", "PA", "PR", "RI", "SC", 
        "SD", "TN", "TX", "UT", "VT", "VI", "VA", "WA", 
        "WV", "WI", "WY"
      ],
      ccSubmitError: '', // Handles response from server
      ccNumberError: '',
      ccCvcError: '',
      ccExpiryError: ''
    }
  },

  async mounted()
  {
    // Style elements & styling
    window.stripe = Stripe('pk_test_0CoPbVlrcNnupYEd1fSRlrWU');
    var elements = stripe.elements();    
    var style = {
      base: {
        color: '#32325d',
        lineHeight: '24px',
        fontFamily: '"Helvetica Neue", Helvetica, sans-serif',
        fontSmoothing: 'antialiased',
        background: 'none',
        boxShadow: 'none',
        fontSize: '18px',
        '::placeholder': {
          color: '#aab7c4'
        }
      },
      invalid: {
        color: '#fa755a',
        iconColor: '#fa755a'
      }
    };

    /*
     * Mount each card element individually so 
     * that they can be responsive and stack in mobile
     */
    
    window.cardNumber = elements.create('cardNumber', {style: style})
    window.cardCvc = elements.create('cardCvc', {style: style})
    window.cardExpiry = elements.create('cardExpiry', {style: style})
    
    window.cardNumber.mount('#cardNumber-mount');
    window.cardCvc.mount('#cardCvc-mount');
    window.cardExpiry.mount('#cardExpiry-mount');
    
    window.cardNumber.addEventListener('change', ({error}) => {      
      if (error)
      {
        this.ccNumberError = error.message
      }
      else
      {
        this.ccNumberError = ''
      }
    });

    window.cardCvc.addEventListener('change', ({error}) => {      
      if (error)
      {
        this.ccCvcError = error.message
      }
      else
      {
        this.ccCvcError = ''
      }
    });

    window.cardExpiry.addEventListener('change', ({error}) => {      
      if (error)
      {
        this.ccExpiryError = error.message
      }
      else
      {
        this.ccExpiryError = ''
      }
    });

    // Fetch user's addresses if logged in
    if (this.$store.state.user.id)
    {
      try {
        const req = await axios({
          method: 'get',
          url: `${window.api}/user/addresses`,
          headers: {'Authorization': `Bearer ${this.$store.state.user.token}`}          
        })
        
        if (req.data.payload.addresses.length)
        {
          
          // const addresses = []
          // req.data.payload.addresses.forEach(addr => {
          //   addresses.push({
          //     text: `${addr.street}, ${addr.city}`,
          //     value : addr.id
          //   })
          // })         
          
          this.addresses = req.data.payload.addresses
          //const storeAddress = Object.assign({},this.$store.state.checkout.shipping)
          
          // this.$store.dispatch('updateShippingAddress', {
          //   name: ''
          // })
        }
      }
      catch (e)
      {
        
      }
    }
  },

  computed: {

    /*
     * Holds total price in user's basket.
     * This is the sum of all crates prices
     * combined. (crate price * crate count).
     * We use this number to display in the view.
     */
    total()
    {
      var crates = this.$store.state.crates
      return Object.keys(this.$store.state.crates)
        .map(e => crates[e].price * crates[e].quantity)
        .reduce((acc,cv) => acc + cv)
    }
  },

  methods: {
    
    /*
     * Makes a section (step) editable again when user
     * clicks the 'edit' link.
     * @param {String} stepNumber - number of the step we're editing ('one', 'two', etc)
     */

    editStep(stepNumber)
    {      
      this.steps[stepNumber].mode = 'active-step'
    },

    updateShippingAddress (obj)
    {
      this.$store.dispatch('UPDATE_SHIPPING_ADDRESS', {
        name: obj.name,
        street: obj.street,
        city: obj.city,
        state: obj.state,
        zip: obj.zip,
        country: obj.country,
      })

      // this.$store.state.checkout.shippingAddress = {
      //   name: obj.name,
      //   street: obj.street,
      //   city: obj.city,
      //   state: obj.state,
      //   zip: obj.zip
      // }      
    },

  

    /*
     * Goes to next step only if form validates.
     * @param {String} formNumber - number of form ref that called this
     * @param {String} stepNumber - number of step to go to
     */
    
    goToNextStep(formNumber, stepNumber)
    {
      // Because form-two is inside a v-if,
      // it won't get checked for validation, and
      // so will always pass. To fix, show
      // the form before going to next step
      if (formNumber == 'two')
      {
        this.showAddressForm = true
      }

      // We need this tick to give vue time to render
      // if we set this.showAddressForm to true in the
      // if condition above.
      this.$nextTick(() => {      
        if (this.$refs[`form-${formNumber}`].validate())
        {        
          this.steps[formNumber].mode = 'completed-step'
          this.steps[stepNumber].mode = 'active-step'
        }
      })
    
    },

    /*
     * Filters user input in real-time, preventing
     * user from entering invalid characters.
     * For instance, inputs that are numbers 
     * only (ie, cvv), will not let a user enter a letter.
     * They are called on keyUp event of each field
     * that requires it.
     *
     * @param {String} field we want to filter
     * @param {Object} e - input event
     */
    filter(field, e)
    {
      var fields = {

        // CVVs are 3 digits
        cvv(e)
        {
          if (e.key.length == 1 && !/[0-9]+$/i.test(e.key))
          {
            e.preventDefault()
            return false
          }
        },

        /*
         * We must check if e.key is 1 char long, and if so,
         * make sure it is 0-9.
         * We need the e.key.length == 1 because special e.key
         * like 'Backspace', 'Del' will fail the 0-9 regex
         * and thus not register.
         */
        expiry(e)
        {
          if (e.key.length == 1 && !/[0-9]+$/i.test(e.key))
          {
            e.preventDefault()
            return false
          }          
        }
      }

      fields[field](e)
    },

    /*
     * Handles credit card submition form.
     */
    handleCreditCardSubmit(e)
    {
      e.preventDefault()
    
      this.isSubmitting = true
      var error = false
      var self = this
      
      // if (!this.$refs['form-three'].validate())
      // {
      //   console.log('didnt validate form 3')
      //   this.isSubmitting = false
      //   error = true
      // }
      /* 
       * To remain PCI compliant, we pass the tokenized
       * creditcard info to the server, instead of the raw data.
       * We only tokenize the credit card info that
       * is the `card` element from Stripe. All other data
       * like email, crates, shipping, etc, remain raw.
       * NOTE that when using multiple Elements (like cardNumber,
       * cardCvc, etc) instead of just Card, we can pass any
       * of them to createToken, since they are linked.
       */
      stripe.createToken(cardNumber).then(result => {
        if (result.error)
        {          
          // self.ccSubmitError = result.error.message
          this.isSubmitting = false
          error = true
        }
        
        if (!error)
        {
          // Send tokenized data, along with all other raw information
          // (shipping addr, crates, etc) to the server.
          axios.post(`${window.api}/cart/checkout`, {
            orderData: {
              email: this.email,
              shippingAddress: this.$store.state.checkout.shippingAddress,
              crates: this.$store.state.crates,
              premium: this.$store.state.usePremium
            },
            stripeToken: result.token
          })
          .then( response => {
            if (response.data.result)
            {
              this.$router.replace('/checkout/complete')
            }
            this.isSubmitting = false
          })
          .catch(error => {
            this.isSubmitting = false
          });
        }
      })
      
    }
  }
}
</script>

<style lang='stylus'>
  @import '../../assets/css/variables'

  $steps-color = #777
  $steps-inactive-color = #999!important


  .active-step  
    .step-content
      display block

  .inactive-step
  .completed-step
    color $steps-inactive-color  
    .step-content
      display none

  .inactive-step
    background transparent!important
    box-shadow none
    border 1px solid #ccc

  .completed-step    
    .step-number    
      border-color transparent


  .toggle-form
    //background #eee
    overflow auto
    //padding 0.5em 0
    text-align center
    margin 0 auto
    display block
    border-radius 3px
    text-decoration none
    margin-bottom 1em
    font-size 80%
  
  p.hint
    font-size 80%
    color #999
    padding 0.5em  4.2em
    
  .steps
    font-size 1.2em
    
    & > div
      margin-bottom 1em
    
    .step-title
      display flex
      align-items baseline
      justify-content space-between
      font-size 120%
      a
        font-size 80%
    .step-number
      display inline-block
      border 1px solid $steps-color
      margin 0 1em 0 0
      width 2em
      height 2em
      line-height 2em
      border-radius 20px
      text-align center
      font-size 80%
    .step-content
      //padding-top 1em
      small
        display none
        font-size 65%
        color lighten($steps-color, 10%)       

    button
      margin-top 2em
    
  #cc-errors
    display none

  .payment-icons
    text-align center
    display none
    img
      display inline-block
      margin-left 10px
    
  
  .pay-button
    div
      font-size 1.3em
      i
        font-size 20px
        margin-right 5px

  

  .summary
    //font-size 1em
    //background rgba(228, 227, 227, 0.2)
    //padding 1em 2em
    //border-radius 5px
    color #444
    .card__text > div
      margin-bottom 1.4em
    h5
      font-size 1.2em
      font-weight bold
      //border-bottom 1px solid #ccc
      text-align  center
      margin-bottom 1em
    h6
      font-size 1em
      margin-bottom 7px
      font-weight bold    
    
    .summary-items
      line-height 32px
    .input-group
      padding-top 0
    
    input
      border-bottom 1px solid #ccc
      padding 0 10px
      max-width 55px
      text-align center
      margin 0 5px
      color $blue
      line-height 15px
      font-size 1.2em



  .input-error
    color #ff5252


  label {
    position: relative;
    color: #8898AA;
    display: block;
    font-weight: 300; 
  }

  label span.icon {
    width: 30px;
    position: absolute;
    top: 34px;
    right: 12px;
  }

</style>