Wed Oct 11 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
<template>

    <main class="container">
        <header>
          <h3>&mdash; Secure Checkout &mdash;</h3>
          <!-- <p></p>   -->      
        </header>      
     
        <v-layout row wrap>

          
          <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">
                        <span class="step-number">1</span>
                        <span class='step-title'>
                          Guest's name (optional)
                          <a v-if="steps.one.mode == 'completed-step'">edit</a>
                        </span>
                      </div>
                      <div class="step-content">
                        <v-text-field   
                          placeholder=""
                          hint-persistent
                          hint="Optional. If entered, guest's name will be engraved on crate"
                          class="input-group"
                          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-number">2</span>
                        <span class='step-title'>
                          Shipping Address
                          <a v-if="steps.two.mode == 'completed-step'">edit</a>
                        </span>
                      </div>
                      <div class="step-content">
                        <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-text-field    
                          label="Name"
                          v-model='$store.state.checkout.shipping.name'
                          class="input-group">            
                        </v-text-field>
                        
                        <v-text-field    
                          label="Address"
                          v-model='$store.state.checkout.shipping.address'
                          :rules="rules.address"
                          required
                          class="input-group">            
                        </v-text-field>

                        <v-text-field    
                          label="City"
                          v-model='$store.state.checkout.shipping.city'
                          :rules="rules.city"
                          required
                          class="input-group">            
                        </v-text-field>

                        <v-select
                          :items="states"
                          :rules='rules.state'
                          v-model="$store.state.checkout.shipping.state"
                          label="Select State"
                          single-line
                          required
                          autocomplete
                          auto
                          hide-details
                        ></v-select>

                        <v-text-field    
                          label="Zip"
                          v-model='$store.state.checkout.shipping.zip'
                          :rules="rules.zip"
                          required
                          class="input-group">            
                        </v-text-field>
                        <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-number">3</span>
                        <span class='step-title'>
                          Payment
                          <a v-if="steps.three.mode == 'completed-step'">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='$store.state.email'
                            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>

          <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
                              </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>
                            {{$store.state.checkout.shipping.name}}<br>
                            {{$store.state.checkout.shipping.address}}<br>
                            {{$store.state.checkout.shipping.city}}
                            <span v-if='$store.state.checkout.shipping.state'>,</span>&nbsp;
                            {{$store.state.checkout.shipping.state}}&nbsp;
                            {{$store.state.checkout.shipping.zip}}
                          </div>



              </v-card-text>
            </v-card>
          </v-flex>

        </v-layout>
          
             
    </main>

</template>

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

export default {
  layout: 'default',
  data ()
  {
    return {
      steps: {
        "one": {
         mode: 'active-step' 
        },
        "two": {
         mode: 'inactive-step' 
        },
        "three": {
         mode: 'inactive-step' 
        },
      },
      isSubmitting: false,
      guestName: '',
      
      // 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',
        ],
        address: [
          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 => isLength(v, { min: 2, max: 2 }) || 'Use 2 letters'
        ],
        ccNumber: [
          v => !!v || 'This field is required',
          v => isCreditCard(v) || 'Invalid credit card number'
        ],
        ccExp: [
          v => !!v || 'This field is required',
          v => isLength(v, { min:4, max:4 }) && isInt(v) 
            || 'Invalid expiration date'
        ],
        ccSecurityCode: [
          v => !!v || 'This field is required',
          v => isLength(v, { min:3, max:3 }) && isInt(v) 
            || 'Invalid security code'
        ]

      },
      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: ''
    }
  },

  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 = ''
      }
    });
  },

  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: {
    /*
     * When user is in Step 2 (shipping address),
     * only go to Step 3 if the form validates
     * @param {String} formNumber - number of form ref that called this
     * @param {String} stepNumber - number of step to go to
     */
    goToNextStep(formNumber, stepNumber)
    {
      // First validate the form
      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('/api/checkout', {
            clientState: this.$store.state,
            stripeToken: result.token
          })
          .then( response => {
            if (response.data.result == 'success')
            {
              this.$router.replace('/checkout/complete')
            }
            this.isSubmitting = false
          })
          .catch(error => {
            console.log(error);
            this.isSubmitting = false
          });

          //this.isSubmitting = false
          
        }

      })


      
      
      
 
      // axios.post('/api/checkout', {
      //   clientState: this.$store.state,
      // })
      // .then( response => {
      //   if (response.data.result == 'success')
      //   {
      //     this.$router.replace('/checkout/complete')
      //   }
      //   this.isSubmitting = false
      // })
      // .catch(error => {
      //   console.log(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
  background alpha(white, 50%) !important
  .step-content
    display none

.completed-step  
  background rgba(195, 215, 236, 0.5)!important
  .step-number
    background rgba(139, 238, 161, 0.5)!important
    border-color transparent


.steps
  font-size 1.2em
  
  & > div
    margin-bottom 1em
  .step-number  
  .step-title
    display inline-block
    a
      font-size 80%
  .step-number
    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
      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>