/home/kueuepay/public_html/app/Traits/PaymentGateway/Stripe.php
<?php
namespace App\Traits\PaymentGateway;

use Exception;
use Stripe\StripeClient;
use Illuminate\Support\Str;
use App\Models\TemporaryData;
use App\Http\Helpers\PaymentGateway;
use App\Constants\PaymentGatewayConst;

trait Stripe {

    private $stripe_gateway_credentials;
    private $request_credentials;
 
    public function stripeInit($output = null) {
        if(!$output) $output = $this->output;
        return $this->createStripeCheckout($output);
    }

    public function createStripeCheckout($output) {

        $request_credentials = $this->getStripeRequestCredentials($output);
        $stripe_client = new StripeClient($request_credentials->token);

        $temp_record_token = generate_unique_string('temporary_datas','identifier',60);
        $this->setUrlParams("token=" . $temp_record_token); // set Parameter to URL for identifying when return success/cancel

        $redirection = $this->getRedirection();
        $url_parameter = $this->getUrlParams();

        $user = auth()->guard(get_auth_guard())->user();

        try{
            $checkout = $stripe_client->checkout->sessions->create([
                'mode'              => 'payment',
                'success_url'       => $this->setGatewayRoute($redirection['return_url'],PaymentGatewayConst::STRIPE,$url_parameter),
                'cancel_url'        => $this->setGatewayRoute($redirection['cancel_url'],PaymentGatewayConst::STRIPE,$url_parameter),
                'customer_email'    => $user->email,
                'line_items'        => [
                    [
                        'price_data'    => [
                            'product_data'      => [
                                'name'          => "Add Money",
                                'description'   => "Add Money To Wallet (" . $output['wallet']->currency->code . "). Payment Currency " . $output['currency']->currency_code,
                                'images'        => [
                                    [
                                        get_logo()
                                    ]
                                ]
                            ],
                            'unit_amount_decimal'   => get_amount(($output['amount']->total_amount * 100),null,2), // as per stripe policy,
                            'currency'              => $output['currency']->currency_code,
                        ],
                        'quantity'                  => 1,
                    ]
                ],
            ]);

            $response_array = json_decode(json_encode($checkout->getLastResponse()->json), true);

            $this->stripeJunkInsert($response_array, $temp_record_token);
        }catch(Exception $e) {
            throw new Exception($e->getMessage());
        }

        $redirect_url = $response_array['url'] ?? null;
        if(!$redirect_url) throw new Exception("Something went wrong! Please try again");

        if(request()->expectsJson()) { // API Response
            $this->output['redirection_response']   = $response_array;
            $this->output['redirect_links']         = [];
            $this->output['redirect_url']           = $redirect_url;
            return $this->get();
        }

        return redirect()->away($response_array['url']);
    }

    public function stripeJunkInsert($response, $temp_identifier) {
        $output = $this->output;

        $data = [
            'gateway'       => $output['gateway']->id,
            'currency'      => $output['currency']->id,
            'amount'        => json_decode(json_encode($output['amount']),true),
            'response'      => $response,
            'wallet_table'  => $output['wallet']->getTable(),
            'wallet_id'     => $output['wallet']->id,
            'creator_table' => auth()->guard(get_auth_guard())->user()->getTable(),
            'creator_id'    => auth()->guard(get_auth_guard())->user()->id,
            'creator_guard' => get_auth_guard(),
        ];

        return TemporaryData::create([
            'type'          => PaymentGatewayConst::TYPEADDMONEY,
            'identifier'    => $temp_identifier,
            'data'          => $data,
        ]);
    }

    public function getStripeCredentials($output) 
    {
        $gateway = $output['gateway'] ?? null;
        if(!$gateway) throw new Exception("Payment gateway not available");

        $test_publishable_key_sample = ['test publishable','test publishable key','test public key','sandbox public key', 'test public','public key test','public test', 'stripe test public key','stripe test sandbox key'];
        $test_secret_key_sample = ['test secret','test secret key','test private','test private key','test live key','test production key','test production','test live','stripe test secret key','stripe test production key'];

        $live_publishable_key_sample    = ['live publishable','live publishable key','live public key','live public key', 'live public','public key live','public live', 'stripe live public key','stripe live sandbox key'];
        $live_secret_key_sample         = ['live secret','live secret key','live private','live private key','live live key','live production key','live production','live live','stripe live secret key','stripe live production key'];

        $test_publishable_key    = PaymentGateway::getValueFromGatewayCredentials($gateway,$test_publishable_key_sample);
        $test_secret_key         = PaymentGateway::getValueFromGatewayCredentials($gateway,$test_secret_key_sample);

        $live_publishable_key    = PaymentGateway::getValueFromGatewayCredentials($gateway,$live_publishable_key_sample);
        $live_secret_key         = PaymentGateway::getValueFromGatewayCredentials($gateway,$live_secret_key_sample);
        
        $mode = $gateway->env;
        
        $gateway_register_mode = [
            PaymentGatewayConst::ENV_SANDBOX => PaymentGatewayConst::ENV_SANDBOX,
            PaymentGatewayConst::ENV_PRODUCTION => PaymentGatewayConst::ENV_PRODUCTION,
        ];

        if(array_key_exists($mode,$gateway_register_mode)) {
            $mode = $gateway_register_mode[$mode];
        }else {
            $mode = PaymentGatewayConst::ENV_SANDBOX;
        }

        $credentials = (object) [
            'test_publishable_key'          => $test_publishable_key,
            'test_secret_key'               => $test_secret_key,
            'live_publishable_key'          => $live_publishable_key,
            'live_secret_key'               => $live_secret_key,
            'mode'                          => $mode
        ];

        $this->stripe_gateway_credentials = $credentials;

        return $credentials;
    }

    public function getStripeRequestCredentials($output = null) 
    {
        if(!$this->stripe_gateway_credentials) $this->getStripeCredentials($output);
        $credentials = $this->stripe_gateway_credentials;
        if(!$output) $output = $this->output;

        $request_credentials = [];
        if($output['gateway']->env == PaymentGatewayConst::ENV_PRODUCTION) {
            $request_credentials['token']   = $credentials->live_secret_key;
        }else {
            $request_credentials['token']   = $credentials->test_secret_key;
        }

        $this->request_credentials = (object) $request_credentials;
        return (object) $request_credentials;
    }

    public function stripeSuccess($output) {
        $output['capture']      = $output['tempData']['data']->response ?? "";
        // need to insert new transaction in database
        try{
            $this->createTransaction($output);
        }catch(Exception $e) {
            throw new Exception($e->getMessage());
        }
    }

    public function isStripe($gateway) 
    {
        $search_keyword = ['stripe','stripe gateway','gateway stripe','stripe payment gateway'];
        $gateway_name = $gateway->name;

        $search_text = Str::lower($gateway_name);
        $search_text = preg_replace("/[^A-Za-z0-9]/","",$search_text);
        foreach($search_keyword as $keyword) {
            $keyword = Str::lower($keyword);
            $keyword = preg_replace("/[^A-Za-z0-9]/","",$keyword);
            if($keyword == $search_text) {
                return true;
                break;
            }
        }
        return false;
    }
    
}
About
top

About NFC Pay: Our Story and Mission

NFC Pay was founded with a vision to transform the way people handle transactions. Our journey is defined by a commitment to innovation, security, and convenience. We strive to deliver seamless, user-friendly payment solutions that make everyday transactions effortless and secure. Our mission is to empower you to pay with ease and confidence, anytime, anywhere.

  • Simplifying Payments, One Tap at a Time.
  • Reinventing Your Wallet for Modern Convenience.
  • Smart Payments for a Effortless Lifestyle.
  • Experience the Ease of Tap and Pay.
  • Innovative Solutions for Your Daily Transactions.

Frequently Asked Questions About NFC Pay

Here are answers to some common questions about NFC Pay. We aim to provide clear and concise information to help you understand how our platform works and how it can benefit you. If you have any further inquiries, please don’t hesitate to contact our support team.

faq-img

How do I register for NFC Pay?

Download the app and sign up using your email or phone number, then complete the verification process.

Is my payment information secure?

Yes, we use advanced encryption and security protocols to protect your payment details.

Can I add multiple cards to my NFC Pay wallet?

Absolutely, you can link multiple debit or credit cards to your wallet.

How do I transfer money to another user?

Go to the transfer section, select the recipient, enter the amount, and authorize the transfer.

What should I do if I forget my PIN?

Use the “Forgot PIN” feature in the app to reset it following the provided instructions.

How can I activate my merchant account?

Sign up for a merchant account through the app and follow the setup instructions to start accepting payments.

Can I track my payment status?

Yes, you can view and track your payment status in the account dashboard