Skip to content

Commit

Permalink
unified AP htran using Postgres sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
samuraee committed Jun 29, 2023
1 parent 9410123 commit 28b4c2c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 15 deletions.
27 changes: 27 additions & 0 deletions migrations/2023_06_30_021642_create_sequence_for_ap_wallet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
DB::statement('CREATE SEQUENCE IF NOT EXISTS ap_trans_id START 1000000000000;');
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
DB::statement('DROP SEQUENCE IF EXISTS ap_trans_id;');
}
};
5 changes: 3 additions & 2 deletions src/LaraWalletServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public function boot(): void
protected function registerPublishing(): void
{
$this->publishes([
__DIR__.'/../config/wallet.php' => config_path('wallet.php'),
], 'config');
__DIR__.'/../config/wallet.php' => config_path('wallet.php')], 'larawallet-configs');
$this->publishes([
__DIR__.'/../migrations/' => database_path('migrations')], 'larawallet-migrations');
}
}
6 changes: 6 additions & 0 deletions src/Provider/AbstractProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Containers\AppSection\Transaction\Models\Transaction;
use App\Containers\AppSection\Wallet\Models\WalletProvider;
use App\Ship\Models\Serial;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
Expand Down Expand Up @@ -185,4 +186,9 @@ public static function generalViewErrorResponse($view, $withErrors): Factory|Vie
{
return view($view)->withErrors([$withErrors]);
}

protected function getWalletTransactionId(string $seq = 'ap_trans_id'): int
{
return Serial::getNextVal($seq);
}
}
25 changes: 12 additions & 13 deletions src/Provider/AsanPardakhtProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,25 @@ public function checkWalletBalance(): JsonResponse|array
'mo' => $this->getCellNumber(),
'hi' => $this->getParameters('host_id'),
'walet' => 5,
'htran' => $this->getWalletChargeTransactionId(),
'htran' => $this->getWalletTransactionId(),
'hop' => AsanpardakhtStatusEnum::WalletBalanceHop->value,
'htime' => time(),
'hkey' => $this->getParameters('api_key'),
]);

$hostRequestSign = $this->signRequest($hostRequest);
$rawResponse = $this->sendInfoToAp($hostRequest, $hostRequestSign, self::POST_METHOD, $this->getUrl());
$rawResponse = $this->sendInfoToAp(
$hostRequest,
$this->signRequest($hostRequest),
self::POST_METHOD,
$this->getUrl()
);
$responseJson = json_decode($rawResponse['hresp'], false, 512, JSON_THROW_ON_ERROR);

if ($responseJson->st !== 1100) {
$credit = 0;

if (property_exists($responseJson, 'wball')) {
$credit = $responseJson->wball / 10;
$credit = $responseJson->wball / 10; // this API returns fucking RIAL!!!
}

return self::generalResponse(
Expand Down Expand Up @@ -95,7 +99,7 @@ public function getBalanceWalletError(\Exception $exception): JsonResponse
{
if (method_exists($exception, 'getResponse') && ! empty($exception->getResponse())) {
$errorJson = json_decode($exception->getResponse()->getBody()->getContents());
$errorMsg = $errorJson != null && property_exists(
$errorMsg = ! is_null($errorJson) && property_exists(
$errorJson,
'description'
) ? $errorJson->description : $exception->getMessage();
Expand Down Expand Up @@ -126,7 +130,7 @@ public function payByWallet(): JsonResponse|array
'mo' => $this->getCellNumber(),
'hi' => $this->getParameters('host_id'),
'walet' => 5,
'htran' => $this->getTransaction()->getWalletTransactionId(),
'htran' => $this->getWalletTransactionId(),
'hop' => AsanpardakhtStatusEnum::PayByWalletHop->value,
'htime' => time(),
'stime' => time(),
Expand Down Expand Up @@ -189,7 +193,7 @@ public function reverseWalletPaymentResult(): mixed
'mo' => $this->getCellNumber(),
'hi' => $this->getParameters('host_id'),
'walet' => 5,
'htran' => $this->getTransaction()->getWalletTransactionId(),
'htran' => $this->getWalletTransactionId(),
'hop' => AsanpardakhtStatusEnum::ReverseRequestHop->value,
'htime' => $time,
'stime' => $time,
Expand Down Expand Up @@ -232,7 +236,7 @@ public function walletCharge(): JsonResponse|array
'mo' => $this->getCellNumber(),
'hi' => $this->getParameters('host_id'),
'walet' => 5,
'htran' => $this->getWalletChargeTransactionId(),
'htran' => $this->getWalletTransactionId(),
'hop' => AsanpardakhtStatusEnum::ChargeWallet->value,
'htime' => time(),
'stime' => time(),
Expand Down Expand Up @@ -400,9 +404,4 @@ public function refundWalletPaymentResult(): mixed

return $result;
}

private function getWalletChargeTransactionId(): int
{
return 900000000000 + time();
}
}

0 comments on commit 28b4c2c

Please sign in to comment.