Updrading Stripe Payment method To V3 version

150 Views Asked by At

Hi guys I have created a stipe payment method a it works fine but I want to upgrade to the stripe v3 but I don't how tp convert my old method to the new one I look at the doc be they are not so clear here is my old code I am using Altorouter and illuminate for database and blade template engine Vue for front end and axious for HTTP

My route

$router->map('POST', '/cart/payment', 'App\Controllers\CartController@Payment', 'Payment');

my cart.blade.php file

<button @click.prevent='checkout' class="btn btn-outline-success">Checkout - <i class="fa fa-credit-card"
 aria-hidden="true"></i></button>
  <span id="proporities" class="hide" data-customer-email='{{ user()->email
  data-stripe-api='{{ \App\Classes\Session::get('stripe_key') }}'>
</span>  

after this button get click I take the information to cart.js here is the cart.js code

 var stripe = StripeCheckout.configure({
            key: $('#proporities').data('stripe-api'),
            locale: 'auto',
            token: function(token) {
                var data = $.param({
                    stripeToken: token.id,
                    stripeEmail: token.email
                });
                axios.post('/cart/payment', data).then(function(response) {
                    $('.notifty').css('display', 'block').delay(2000).slideUp(300).html(response.data.success);
                    app.showcart(10);
                }).catch(function(error) {
                    console.log(error)
                });
            }

        });
 var app = new Vue({

            el: '#cart',
            data: {
                amount: 0
            },
            methods: {

    checkout: function() {
                    stripe.open({
                        name: 'Cartzilla Store Inc.',
                        description: 'Your Shopping Cart',
                        zipCode: true,
                        amount: app.amount,
                        email: $('#proporities').data('customer-email')
                    });
    }

in the cart.js axious take to information to PHP controller which I have defined in the top route

here is the controller code

 public function payment()
  {
    $array = [];
    if (Request::has('post')) {
      $request = Request::get('post');
      $email = $request->stripeEmail;
      $token = $request->stripeToken;

      try {


        $customer = Customer::create([
          'email' => $email,
          'source' => $token
        ]);

        $amount = convertMoney(Session::get('carttotal'));
        $charge = Charge::create([
          'customer' => $customer->id,
          'amount' => $amount,
          'description' => user()->fullname . 'Cart Purchase',
          'currency' => 'usd'
        ]);

        $orderId = strtoupper(uniqid());

        foreach ($_SESSION['user_cart'] as $items) {
          $productId = $items['product_id'];
          $quantity = $items['quantity'];
          $product = Product::where('id', $productId)->first();

          if (!$product) {
            continue;
          }
          $totalPrice = $product->price * $quantity;
          $totalPrice = number_format($totalPrice, 2);
          Order::create([
            'user_id' => user()->id,
            'product_id' => $productId,
            'unit_price' => $product->price,
            'status' => 'pending',
            'total_price' => $amount,
            'order_no' => $orderId,
            'quantity' => $quantity
          ]);

          $product->quantity = $product->quantity - $quantity;
          $product->save();

          array_push($array, [
            'name' => $product->name,
            'price' => $product->price,
            'quantity' => $quantity,
            'total' => $totalPrice,
          ]);

          Payment::create([
            'user_id' => user()->id,
            'amount' => $charge->amount,
            'status' => $charge->status,
            'order_no' => $orderId
          ]);
        }

        Session::remove('user_cart');
        echo json_encode(['success' => 'Thank You we Have Recevied Your Payment And Now Processing Your Order']);
      } catch (\Throwable $th) {
        //throw $th;
      }
    }
  }

apart from this, I have to model to for order table and payment table

and here are the model

Order.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Order extends Model
{
  use SoftDeletes;
  public $timestamp = true;
  public $fillable = ['user_id', 'product_id', 'unit_price', 'qunatity', 'total_price', 'status', 'order_no'];
  public $date = ['deleted_at'];
}

and Payment .php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Payment extends Model
{
  use SoftDeletes;
  public $timestamp = true;
  public $fillable = ['user_id', 'amount', 'status', 'order_no'];
  public $date = ['deleted_at'];
}
                
0

There are 0 best solutions below