<?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;
}
}
At NFC Pay, your privacy is of utmost importance to us. This Privacy Policy outlines how we collect, use, share, and protect your personal information when you use our services, including our website and mobile applications.
1. Information We Collect
2. How We Use Your Information
We use the information we collect for the following purposes:
3. Sharing Your Information
We may share your personal information in the following circumstances:
4. Security of Your Information
We take the security of your personal information seriously and implement a variety of security measures, including encryption, secure servers, and access controls, to protect your data from unauthorized access, disclosure, alteration, or destruction. However, no method of transmission over the internet or electronic storage is completely secure, and we cannot guarantee its absolute security.
5. Your Privacy Rights
Depending on your location, you may have certain rights regarding your personal information, such as:
6. Third-Party Links
Our services may contain links to third-party websites or services. We are not responsible for the privacy practices or the content of these third-party sites. We encourage you to review the privacy policies of those third parties.
7. Children’s Privacy
Our services are not intended for individuals under the age of 13. We do not knowingly collect personal information from children under 13. If we become aware that we have collected personal information from a child under 13, we will take steps to delete that information.
8. Changes to This Privacy Policy
We may update this Privacy Policy from time to time to reflect changes in our practices or for other operational, legal, or regulatory reasons. We will notify you of any significant changes by posting the new Privacy Policy on our website and updating the effective date.
9. Contact Us
If you have any questions or concerns about this Privacy Policy or our data practices, please contact us at:
Email: support@nfcpay.com