Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
A1Gard committed Aug 26, 2024
2 parents bc3b4f4 + bdf3ef0 commit 266d38f
Show file tree
Hide file tree
Showing 15 changed files with 656 additions and 22 deletions.
15 changes: 5 additions & 10 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ APP_KEY=
APP_DEBUG=true
APP_DEPLOYED=false
APP_TIMEZONE=UTC
APP_URL=http://localhost:8000
APP_URL=http://xshop.test

APP_LOCALE=en
APP_FALLBACK_LOCALE=en
Expand All @@ -20,12 +20,7 @@ LOG_STACK=single
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=
DB_CONNECTION=sqlite

SESSION_DRIVER=database
SESSION_LIFETIME=9999999
Expand All @@ -45,7 +40,6 @@ MEMCACHED_HOST=127.0.0.1
PANEL_PREFIX=dashboard
PANEL_PAGE_COUNT=30


REDIS_CLIENT=phpredis
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
Expand All @@ -60,7 +54,6 @@ MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS="[email protected]"
MAIL_FROM_NAME="${APP_NAME}"


MEDIA_WATERMARK_SIZE=15
MEDIA_WATERMARK_OPACITY=50

Expand All @@ -72,7 +65,6 @@ AWS_USE_PATH_STYLE_ENDPOINT=false

VITE_APP_NAME="${APP_NAME}"


XLANG_ACTIVE=false
XLANG_MAIN=en
XLANG_API_URL="http://5.255.98.77:3001"
Expand All @@ -83,3 +75,6 @@ CURRENCY_CODE=USD

SIGN_SMS=true
SIGN_DRIVER=Kavenegar

ZARINPAL_MERCHANT=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
PAY_GATEWAY=zarinpal
67 changes: 67 additions & 0 deletions app/Contracts/Payment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace App\Contracts;

interface Payment
{

/**
* Register Payment Service Provider
*
* @return self
*/
public static function registerService();

/**
* Get Payment name
*
* @return string
*/
public static function getName(): string;

/**
* Get payment type must be one of: ONLINE, CHEQUE, CARD, CASH, CASH_ON_DELIVERY
*
* @return string
*/
public static function getType(): string;

/**
* Is Active To Show user
*
* @return bool
*/
public static function isActive(): bool;

/**
* Gateway Logo
*
* @return string
*/
public static function getLogo();

/**
* Request online payment
*
* @param int $amount transaction amount
* @param string $callbackUrl a url that callback user after transaction
* @param array $additionalData additional data to send back
*
* @return array request data like token and order id
* @throws \Throwable
*/
public function request(int $amount, string $callbackUrl, array $additionalData = []): array;

/**
* Redirect customer to bank payment page
*/
public function goToBank();

/**
* Verify payment
*
* @return array successful payment have two keys: reference_id , card_number
* @throws \Throwable if payment fail
*/
public function verify(): array;
}
40 changes: 40 additions & 0 deletions app/Contracts/PaymentStore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php


namespace App\Contracts;


interface PaymentStore
{
/**
* Store payment request
*
* @param int $orderId Payment unique order id
* @param null $token
* @param string $type One of 'ONLINE', 'CHEQUE', 'CASH', 'CARD', 'CASH_ON_DELIVERY'
*
* @return \App\Models\Payment
*/
public function storePaymentRequest($orderId,$amount, $token = null, $type = 'ONLINE',$bank=null): \App\Models\Payment;

/**
* Store success payment and update invoice status
*
* @param int $paymentId Payment unique order id
* @param string|int $referenceId Transaction reference id
* @param null $cardNumber
*
* @return \App\Models\Payment
*/
public function storeSuccessPayment($paymentId, $referenceId, $cardNumber = null): \App\Models\Payment;

/**
* Store failed payment and update invoice status
*
* @param int $orderId Payment unique order id
* @param null $message Fail reason text to store
*
* @return \App\Models\Payment
*/
public function storeFailPayment($orderId, $message = null): \App\Models\Payment;
}
21 changes: 21 additions & 0 deletions app/Events/InvoiceCompleted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Events;

use App\Models\Invoice;
use Illuminate\Queue\SerializesModels;

class InvoiceCompleted
{
use SerializesModels;

/**
* @var Invoice
*/
public $invoice;

public function __construct(Invoice $invoice)
{
$this->invoice = $invoice;
}
}
27 changes: 27 additions & 0 deletions app/Events/InvoiceFailed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace App\Events;

use App\Models\Invoice;
use App\Models\Payment;
use Illuminate\Queue\SerializesModels;

class InvoiceFailed
{
use SerializesModels;

/**
* @var Invoice
*/
public $invoice;
/**
* @var Payment
*/
public $payment;

public function __construct(Invoice $invoice,Payment $payment)
{
$this->invoice = $invoice;
$this->payment = $payment;
}
}
27 changes: 27 additions & 0 deletions app/Events/InvoiceSucceed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace App\Events;

use App\Models\Invoice;
use App\Models\Payment;
use Illuminate\Queue\SerializesModels;

class InvoiceSucceed
{
use SerializesModels;

/**
* @var Invoice
*/
public $invoice;
/**
* @var Payment
*/
public $payment;

public function __construct(Invoice $invoice,Payment $payment)
{
$this->invoice = $invoice;
$this->payment = $payment;
}
}
56 changes: 44 additions & 12 deletions app/Http/Controllers/CardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Http\Controllers;

use App\Contracts\Payment;
use App\Models\Customer;
use App\Models\Discount;
use App\Models\Invoice;
use App\Models\Order;
Expand Down Expand Up @@ -80,6 +82,7 @@ public function productCardToggle(Product $product)

public function index()
{
auth('customer')->login(Customer::first());
$area = 'card';
$title = __("Shopping card");
$subtitle = '';
Expand All @@ -97,27 +100,28 @@ public function check(Request $request)
]);
$total = 0;
// return $request->all();
$inv = new Invoice();
$inv->customer_id = auth('customer')->user()->id;
$inv->count = array_sum($request->count);
$inv->address_id = $request->address_id;
$inv->desc = $request->desc;
$invoice = new Invoice();
$invoice->customer_id = auth('customer')->user()->id;
$invoice->count = array_sum($request->count);
$invoice->address_id = $request->address_id;
$invoice->desc = $request->desc;

if ($request->has('transport_id')) {
$request->transport_id = $request->input('transport_id');
$t = Transport::find($request->input('transport_id'));
$inv->transport_price = $t->price;
$invoice->transport_price = $t->price;
$total += $t->price;
}
if ($request->has('discount_id')) {
$request->discount_id = $request->input('discount_id');
}

$inv->save();
$invoice->save();

foreach ($request->product_id as $i => $product) {
$order = new Order();
$order->product_id = $product;
$order->invoice_id = $inv->id;
$order->invoice_id = $invoice->id;
$order->count = $request->count[$i];
if ($request->quantity_id[$i] != '') {
$order->quantity_id = $request->quantity_id[$i];
Expand All @@ -133,17 +137,45 @@ public function check(Request $request)
$order->save();
}

$inv->total_price = $total;
$inv->save();
$invoice->total_price = $total;
$invoice->save();
// clear shopping card
// self::clear();
return [$inv, $inv->orders];
//prepare to redirect to bank gateway
$activeGateway = config('xshop.payment.active_gateway');
/** @var Payment $gateway */
$gateway = app($activeGateway . '-gateway');
logger()->info('pay controller', ["active_gateway" => $activeGateway, "invoice" => $invoice->toArray(),]);

if ($invoice->isCompleted()) {
return redirect()->back()->with('message', __('Invoice payed.'));
}

$callbackUrl = route('pay.check', ['invoice_hash' => $invoice->hash, 'gateway' => $gateway->getName()]);
$payment = null;
try {
$response = $gateway->request(($invoice->total_price - $invoice->credit_price), $callbackUrl);
$payment = $invoice->storePaymentRequest($response['order_id'], ($invoice->total_price - $invoice->credit_price), $response['token'] ?? null, null, $gateway->getName());
session(["payment_id" => $payment->id]);
\Session::save();

return $gateway->goToBank();
} catch (\Throwable $exception) {
$invoice->status = 'FAILED';
$invoice->save();
\Log::error("Payment REQUEST exception: " . $exception->getMessage());
\Log::warning($exception->getTraceAsString());
$result = false;
$message = __('error in payment. contact admin.');
return redirect()->back()->withErrors($message);
}

}


public static function clear()
{
if (auth('customer')->check()){
if (auth('customer')->check()) {
$customer = auth('customer')->user();
$customer->card = null;
$customer->save();
Expand Down
50 changes: 50 additions & 0 deletions app/Http/Controllers/Payment/GatewayVerifyController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace App\Http\Controllers\Payment;

use App\Contracts\Payment;
use App\Models\Invoice;

class GatewayVerifyController
{
/**
* @param Invoice $invoice
* @param Payment $gateway
*/
public function __invoke($invoice_hash, $gateway)
{
try {
$invoice = Invoice::whereHash($invoice_hash)->firstOrFail();
$payment = null;
$message = null;
$result = true;
$paymentId = self::getPayment($invoice);
$response = $gateway->verify();
$payment = $invoice->storeSuccessPayment($paymentId, $response['reference_id'], $response['card_number']);
session(['card'=>serialize([])]);
} catch (\Throwable $exception) {
$result = false;
$invoice->storeFailPayment($paymentId, $exception->getMessage());
$message = $exception->getMessage();
\Log::debug("Payment RESPONSE Fail For Gateway {$gateway->getName()} :" . $exception->getMessage() . " On Line {$exception->getLine()} Of File {$exception->getFile()}", ['request' => request()->all(), 'session' => request()->session()->all(), 'user' => request()->user(), 'payment_id' => $paymentId]);
\Log::warning($exception->getTraceAsString());
return redirect()->route('client.card')->withErrors(__("error in payment.").$message);
}

return redirect()->route('client.profile')->with('message' , __("payment success"));

}

/**
* @param Invoice $invoice
* @return integer
*/
public static function getPayment($invoice)
{
$paymentId = session('payment_id');
if (empty($paymentId)) {
$paymentId = $invoice->payments->last()->id;
}
return $paymentId;
}
}
Loading

0 comments on commit 266d38f

Please sign in to comment.