Viewing file: StripePayment.php (7.16 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php
namespace Modules\PaymentGateway\TopUpProvider;
use App\Models\BillingRequest; use App\Models\Customer; use App\Models\Number; use App\Models\Report; use App\Models\TopUpRequest; use App\Models\Transactions; use Carbon\Carbon; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Log; use Modules\PaymentGateway\Services\PaymentCredentials;
class StripePayment implements PaymentInterface { public $numberReq; public $redirect_url; public $error_message; public $return_view; public $will_redirect = false;
public function __construct() {
}
public function pay() { // TODO: Implement pay() method. }
public function number_request($numberReq) { $this->numberReq = $numberReq; return $this; }
public function getCredentials() { $credentials= PaymentCredentials::get();
if (!$credentials->stripe_pub_key || !$credentials->stripe_secret_key) { throw new \Exception('Credentials not found. Please contact with the administrator'); } return $credentials; }
public function topUpRequest($topup_request){ $this->topup_request=$topup_request; return $this; }
public function amount($amount) { $this->amount = $amount; return $this; } public function request($request) { $this->request = $request; return $this; }
public function will_redirect() { // TODO: Implement will_redirect() method. return $this->will_redirect; }
public function redirect_url() { // TODO: Implement redirect_url() method. return $this->redirect_url; }
public function return_view() { // TODO: Implement redirect_url() method. return $this->return_view; }
public function error_message() { // TODO: Implement error_message() method. return $this->error_message; }
public function process() { try { $amount = $this->amount; $request = $this->request; $credentials=$this->getCredentials(); $payment = $this->stripePayment($amount, $credentials, $this->request);
if (isset($payment->id)) { $this->stripeCompletePayment($this->topup_request, $payment->id); }
$this->redirect_url = null; $this->return_view = null;
} catch (\Exception $ex) { Log::error($ex->getMessage()); $this->error_message= $ex->getMessage(); } }
/*Custom Function*/ function stripePayment($amount, $credentials, $request) {
$stripe = new \Stripe\StripeClient($credentials->stripe_secret_key);
return $stripe->charges->create([ 'amount' => $amount * 100, 'currency' => "USD", 'source' => $request->stripeToken, 'description' => 'User:' . auth('customer')->user()->email, ]); }
function stripeCompletePayment($topup_request, $paymentId) {
DB::beginTransaction(); try {
$topup_request = TopUpRequest::where('id', $topup_request->id)->first();
if (!$topup_request) { throw new \Exception('Invalid Payment'); }
try { $customer = Customer::where('id', $topup_request->customer_id)->first(); if (!$customer) { throw new \Exception('Invalid Payment'); }
$transaction = Transactions::where('type', 'top_up')->where('customer_id', $customer->id)->where('ref_id', $topup_request->id)->first();
if ($transaction) { $transaction->transaction_id = $paymentId; $transaction->status = 'paid'; $transaction->save(); }
$wallet = $customer->wallet()->first();
if ($topup_request->credit > 0 && $customer->added_by != 'admin') { $seller = Customer::where('id', $topup_request->admin_id)->where('type', $customer->added_by)->first(); if (!$seller) { throw new \Exception('Invalid Payment'); } $sellerWallet = $seller->wallet()->first(); if ($sellerWallet->credit > $topup_request->credit) { $wallet->credit = $wallet->credit + $topup_request->credit; $wallet->save();
$sellerWallet->credit = $sellerWallet->credit - $topup_request->credit; $sellerWallet->save();
// For Customer //Report $report = new Report(); $report->customer_id = $customer->id; $report->ref_id = $topup_request->id; $report->type = 'topup'; $report->sub_type = 'topup'; $report->amount = '+' . $topup_request->credit; $report->save(); //For Seller //Report $report = new Report(); $report->customer_id = $sellerWallet->customer_id; $report->ref_id = $topup_request->id; $report->type = 'topup'; $report->sub_type = 'topop'; $report->amount = '-' . $topup_request->credit; $report->save(); } else { $topUpReq = new TopUpRequest(); $topUpReq->credit = $topup_request->credit; $topUpReq->credit_type = 'non_masking'; $topUpReq->customer_id = $customer->id; $topUpReq->admin_id = $customer->admin_id; $topUpReq->payment_status = 'unpaid'; $topUpReq->customer_type = $customer->type; $topUpReq->transaction_id = $paymentId; $topUpReq->save(); } } else { $wallet->credit = $wallet->credit + $topup_request->credit; $wallet->save(); $report = new Report(); $report->customer_id = $customer->id; $report->ref_id = $topup_request->id; $report->type = 'topup'; $report->sub_type = 'topup'; $report->amount = '+' . $topup_request->credit; $report->save(); }
$topup_request->payment_status = 'paid'; $topup_request->status = 'approved'; $topup_request->transaction_id = $paymentId; $topup_request->save();
DB::commit(); } catch (\Exception $ex) { DB::rollBack(); throw new \Exception('Invalid Payment'); }
DB::commit(); } catch (\Exception $ex) { DB::rollBack(); throw new \Exception('Invalid Payment'); } } }
|