Spamworldpro Mini Shell
Spamworldpro


Server : Apache
System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64
User : corals ( 1002)
PHP Version : 7.4.33
Disable Function : exec,passthru,shell_exec,system
Directory :  /home/corals/mcoil.corals.io/app/Shop/PaymentMethods/Stripe/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/corals/mcoil.corals.io/app/Shop/PaymentMethods/Stripe/StripeRepository.php
<?php

namespace App\Shop\PaymentMethods\Stripe;

use App\Shop\Checkout\CheckoutRepository;
use App\Shop\Couriers\Courier;
use App\Shop\Couriers\CourierRepository;
use App\Shop\Customers\Customer;
use App\Shop\Customers\Repositories\CustomerRepository;
use App\Shop\PaymentMethods\Stripe\Exceptions\StripeChargingErrorException;
use Gloudemans\Shoppingcart\Facades\Cart;
use Ramsey\Uuid\Uuid;
use Stripe\Charge;
use App\Shop\Orders\Order;

class StripeRepository
{
    /**
     * @var Customer
     */
    private $customer;

    /**
     * StripeRepository constructor.
     * @param Customer $customer
     */
    public function __construct(Customer $customer)
    {
        $this->customer = $customer;
    }

    /**
     * @param array $data Cart data
     * @param $total float Total items in the cart
     * @param $tax float The tax applied to the cart
     * @return Charge Stripe charge object
     * @throws StripeChargingErrorException
     */
    public function execute(array $data, $total, $tax) : Charge
    {
    	try {
            $shipping = 0;

            // Remove comma, if exist (1,250.00)
            $total = filter_var($total, FILTER_VALIDATE_FLOAT, FILTER_FLAG_ALLOW_THOUSAND);

            $totalComputed = $total + $shipping + $tax;

            // Deduct the discount before making the payment request
            $totalComputedAfterDiscount = $totalComputed - $data['discount_amount'];

            $customerRepo = new CustomerRepository($this->customer);
            $options['source'] = $data['stripeToken'];
            $options['currency'] = config('cart.currency');

            if ($charge = $customerRepo->charge($totalComputedAfterDiscount, $options)) {
                $checkoutRepo = new CheckoutRepository;
                $checkoutRepo->buildCheckoutItems([
                    'reference' => Uuid::uuid4()->toString(),
                    'courier_id' => 1,
                    'customer_id' => $this->customer->id,
                    'address_id' => $data['billing_address'],
                    'order_status_id' => 1,
                    'payment' => strtolower(config('stripe.name')),
                    'discounts' => $data['discount_amount'],

                    // For discount
                    'discount_coupon' 	=> $data['discount_coupon'],
                    'discount_type' 	=> $data['discount_type'],
                    'discount_value' 	=> $data['discount_value'],
                    'discount_amount' 	=> $data['discount_amount'],

                    'billing_address'   => $data['billing_address'],
                    'delivery_address'  => $data['delivery_address'],
                    'delivery_note'  	=> $data['delivery_note'],

                    'total_products' => $total,
                    'total' => $total,
                    // 'total_paid' => $totalComputed,
                    'total_paid' => ( $totalComputed - $data['discount_amount'] ),	// Deduct the discount
                    'tax' => $tax,
                    // Payment related
                    'transaction_id' 	=> $charge->id,
                    'stripe_customer_id'=> $charge->customer,
                    'transaction_status'=> $charge->status,
                ]);

                Cart::destroy();
            }

            return $charge;
        } catch (\Exception $e) {
            throw new StripeChargingErrorException($e);
        }
    }

    /**
     * @param array $data Cart data
     * @param $total float Total items in the cart
     * @param $tax float The tax applied to the cart
     * @return Charge Stripe charge object
     * @throws StripeChargingErrorException
     */
    public function executeShopOrderPayment(array $data, $total) : Charge
    {
    	try {
            // Remove comma, if exist (1,250.00)
            $total = filter_var($total, FILTER_VALIDATE_FLOAT, FILTER_FLAG_ALLOW_THOUSAND);

            $customerRepo = new CustomerRepository($this->customer);
            $options['source'] = $data['stripeToken'];
            $options['currency'] = config('cart.currency');

            if( $charge = $customerRepo->charge($total, $options) )
            {
            	// Update the order status
            	$orderId = $data['order_id'];

            	$order = Order::find($orderId);
            	$order->order_status_id 	= 1;		// paid
            	$order->transaction_id 		= $charge->id;
            	$order->stripe_customer_id 	= $charge->customer;
            	$order->transaction_status 	= $charge->status;
            	$order->save();
            }

            return $charge;
        } catch (\Exception $e) {
            throw new StripeChargingErrorException($e);
        }
    }
}

Spamworldpro Mini