PHP 8.2.30
Preview: PricingPlanRepository.php Size: 6.38 KB
/home/byroehnu/easetack.com/app/Repositories/Frontend/PricingPlanRepository.php

<?php

namespace App\Repositories\Frontend;

use App\Models\PaymentHistory;
use App\Models\PricingPlan;
use App\Services\PaymentGateway\FlutterWave;
use App\Services\PaymentGateway\Paypal;
use App\Services\PaymentGateway\Razorpay;
use App\Services\PaymentGateway\SSLCommerz;
use App\Services\PaymentGateway\Stripe;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
use Inertia\Inertia;

class PricingPlanRepository
{
    public function makePayment(Request $request, PricingPlan $pricingPlan)
    {
        $payment_method = $request->payment_method;

        $paymentHistory = PaymentHistory::create([
            'name' => $request->input('name'),
            'plan_id' => $pricingPlan->id,
            'amount' => $pricingPlan->price,
            'email' => $request->input('email'),
            'mobile' => $request->input('mobile'),
            'whatsapp_or_skype' => $request->input('whatsapp_skype'),
            'method' => $request->input('payment_method'),
            'status' => 'awaiting_payment',
            'note' => $request->input('note'),
        ]);

        switch ($payment_method) {
            case 'paypal':
                $paypal = new Paypal;
                $body = [
                    'intent' => 'CAPTURE',
                    'purchase_units' => [[
                        'reference_id' => rand(000000, 999999),
                        'amount' => [
                            'value' => number_format($pricingPlan->price, 2, '.', ''),
                            'currency_code' => $pricingPlan->currency->code,
                        ],
                    ]],
                    'application_context' => [
                        'cancel_url' => route('payment.cancel', ['method' => 'paypal', 'identifier' => $paymentHistory]),
                        'return_url' => route('payment.success', ['method' => 'paypal', 'identifier' => $paymentHistory]),
                    ],
                ];

                $response = $paypal->initializePayment($body);

                return Inertia::location($response->links[1]->href);
            case 'stripe':
                $stripe = new Stripe;
                $body = [
                    'line_items' => [[
                        'price_data' => [
                            'currency' => $pricingPlan->currency->code,
                            'product_data' => [
                                'name' => $pricingPlan->content->name,
                            ],
                            'unit_amount' => 100 * $pricingPlan->price, // Amount in cents, adjust accordingly
                        ],
                        'quantity' => 1,
                    ]],
                    'mode' => 'payment',
                    'success_url' => route('payment.success', ['method' => 'stripe', 'identifier' => $paymentHistory]),
                    'cancel_url' => route('payment.cancel', ['method' => 'stripe', 'identifier' => $paymentHistory]),
                ];
                try {
                    $response = $stripe->initializePayment($body);
                    Session::put('paymentId', $response->id);
                    // save payment id to db
                    $paymentHistory->update(['payment_identifier' => $response->id]);

                    return Inertia::location($response->url);
                } catch (\Exception $exception) {
                    throw new Exception($exception->getMessage());
                }

            case 'sslcmz':
                $sslcmz = new SSLCommerz($paymentHistory->id);
                $body = [
                    'total_amount' => $pricingPlan->price,
                    'currency' => $pricingPlan->currency->code,
                    'tran_id' => $paymentHistory->id,

                    'cus_name' => $request->name,
                    'cus_email' => $request->email,
                    'cus_add1' => '',
                    'cus_add2' => '',
                    'cus_city' => '',
                    'cus_postcode' => '',
                    'cus_country' => 'Bangladesh',
                    'cus_phone' => $request->mobile,

                    'ship_name' => '',
                    'ship_add1' => '',
                    'ship_add2' => '',
                    'ship_city' => '',
                    'ship_state' => '',
                    'ship_postcode' => '',
                    'ship_phone' => $request->mobile,
                    'ship_country' => 'Bangladesh',

                    'shipping_method' => 'NO',
                    'product_name' => [$pricingPlan->content->name],
                    'product_category' => [$pricingPlan->content->name],
                    'product_profile' => 'general',
                ];
                try {
                    $response = $sslcmz->initilizePatment($body);
                    $result = json_decode($response);

                    return Inertia::location($result->data);
                } catch (\Exception $exception) {
                    dd($exception->getMessage());
                    throw new Exception($exception->getMessage());
                }

            case 'flutterwave':
                try {
                    $flutterwave = new FlutterWave;
                    $data = [
                        'amount' => $pricingPlan->price,
                        'currency' => $pricingPlan->currency->code,
                        'customer' => [
                            'name' => $request->name,
                            'email' => $request->email,
                            'phonenumber' => $request->mobile,
                        ],
                        'tx_ref' => $paymentHistory->id,
                        'redirect_url' => route('payment.success', ['method' => 'flutterwave', 'identifier' => $paymentHistory]),
                    ];
                    $response = $flutterwave->initializePayment($data);

                    return Inertia::location($response['data']['link']);
                } catch (\Exception $exception) {
                }

            case 'razorpay':
                $razorpay = new Razorpay;
                $data = ['receipt' => 'R-' . rand(000000, 999999), 'amount' => (int) $pricingPlan->price * 100, 'currency' => $pricingPlan->currency->code];
                $order_id = $razorpay->initilizePatment($data);
                $url = route('payment.razorpay.pay', ['order_id' => $order_id, 'payment_id' => $paymentHistory->id]);

                return Inertia::location($url);
        }
    }
}

Directory Contents

Dirs: 0 × Files: 9

Name Size Perms Modified Actions
2.62 KB lrw-rw-rw- 2025-04-19 04:22:15
Edit Download
8.26 KB lrw-rw-rw- 2025-05-15 11:24:11
Edit Download
1.45 KB lrw-rw-rw- 2025-04-19 04:22:15
Edit Download
6.38 KB lrw-rw-rw- 2025-05-08 12:05:13
Edit Download
2.52 KB lrw-rw-rw- 2025-04-26 11:30:31
Edit Download
1.77 KB lrw-rw-rw- 2025-04-15 10:33:57
Edit Download
829 B lrw-rw-rw- 2025-04-19 04:22:16
Edit Download
5.46 KB lrw-rw-rw- 2025-04-24 11:21:09
Edit Download
3.50 KB lrw-rw-rw- 2025-04-13 07:02:31
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).