add more banks in online payment

This commit is contained in:
saparatayev 2022-04-23 12:26:09 +05:00
parent 31666b594a
commit 57b1ef42c5
13 changed files with 341 additions and 74 deletions

16
config/bank.php Normal file
View File

@ -0,0 +1,16 @@
<?php
return [
'halkbank' => [
'bank_name' => 'Halkbank',
'class' => 'TPS\Birzha\Classes\Halkbank'
],
'rysgal' => [
'bank_name' => 'Rysgal bank',
'class' => 'TPS\Birzha\Classes\Rysgal'
],
'senagat' => [
'bank_name' => 'Senagat PTB',
'class' => 'TPS\Birzha\Classes\Senagat'
],
];

View File

@ -0,0 +1,64 @@
<?php namespace TPS\Birzha\Classes;
use TPS\Birzha\Models\Settings;
class Halkbank extends Payment
{
protected function getApiCredentials()
{
return [
'userName' => Settings::getValue('api_login'),
'password' => Settings::getValue('api_password'),
];
}
protected function getApiUrl()
{
return 'https://mpi.gov.tm/payment/rest/';
}
protected function getRegistrationUri()
{
return 'register.do';
}
protected function getStatusUri()
{
return 'getOrderStatus.do';
}
protected function registerOrderParams($payment, $url)
{
return [
'amount' => $payment->amount * 100, //multiply by 100 to obtain tenge
'currency' => 934,
'description' => 'Balansa pul goşmak - Birža şahsy otag',
'orderNumber' => strtoupper(str_random(5)) . date('jn'),
'failUrl' => $url . '?status=failed',
'returnUrl' => $url . '?status=success',
];
}
public function checkRegisterOrderErrorCode($registerOrderResponse)
{
return $registerOrderResponse['errorCode'] == 0;
}
public function checkReturnUrlParams($returnUrlParams, $payment)
{
if($payment && $returnUrlParams['status'] === 'success' && $returnUrlParams['orderId'] === $payment->order_id) {
return true;
} else {
return false;
}
}
public function checkOrderStatusResponseCodes($orderStatusResponse)
{
return $orderStatusResponse['ErrorCode'] == 0 && $orderStatusResponse['OrderStatus'] == 2;
}
}

View File

@ -1,40 +1,33 @@
<?php namespace TPS\Birzha\Classes;
use October\Rain\Network\Http;
use TPS\Birzha\Models\Settings;
class Payment
abstract class Payment
{
abstract protected function getApiCredentials();
abstract protected function getApiUrl();
abstract protected function getRegistrationUri();
abstract protected function getStatusUri();
abstract protected function registerOrderParams($payment, $url);
abstract public function checkRegisterOrderErrorCode($registerOrderResponse);
abstract public function checkReturnUrlParams($returnUrlParams, $payment);
abstract public function checkOrderStatusResponseCodes($orderStatusResponse);
const REGISTRATION_URI = 'register.do';
const STATUS_URI = 'getOrderStatus.do';
const API_URL = 'https://mpi.gov.tm/payment/rest/';
private static function getClient($url){
return Http::make(self::API_URL.$url, Http::METHOD_POST)->data([
'userName' => Settings::getValue('api_login'),
'password' => Settings::getValue('api_password'),
])->timeout(3600);
private function getClient($url){
return Http::make($this->getApiUrl() . $url, Http::METHOD_POST)
->data($this->getApiCredentials())
->timeout(3600);
}
public static function registerOrder($payment, $url) {
$client = self::getClient(self::REGISTRATION_URI);
public function registerOrder($payment, $url) {
$client = $this->getClient($this->getRegistrationUri());
$client->data([
'amount' => $payment->amount * 100,//multiply by 100 to obtain tenge
'currency' => 934,
'description' => 'Balansa pul goşmak - Birža şahsy otag',
'orderNumber' => strtoupper(str_random(5)) . date('jn'),
'failUrl' => $url . '?status=failed',
'returnUrl' => $url . '?status=success',
]);
$client->data($this->registerOrderParams($payment, $url));
$client->setOption(CURLOPT_POSTFIELDS,$client->getRequestData());
return $client->send();
}
public static function getStatus($order_id) {
$client = self::getClient(self::STATUS_URI);
public function getStatus($order_id) {
$client = $this->getClient($this->getStatusUri());
$client->data([
'orderId' => $order_id

View File

@ -0,0 +1,62 @@
<?php namespace TPS\Birzha\Classes;
use TPS\Birzha\Models\Settings;
class Rysgal extends Payment
{
protected function getApiCredentials()
{
return [
'userName' => Settings::getValue('api_login_rysgal'),
'password' => Settings::getValue('api_password_rysgal'),
];
}
protected function getApiUrl()
{
return 'https://epg2.rysgalbank.tm/epg/rest/';
}
protected function getRegistrationUri()
{
return 'register.do';
}
protected function getStatusUri()
{
return 'getOrderStatusExtended.do';
}
protected function registerOrderParams($payment, $url)
{
return [
'amount' => $payment->amount * 100, //multiply by 100 to obtain tenge
'currency' => 934,
'description' => 'Balansa pul goşmak - Birža şahsy otag',
'orderNumber' => strtoupper(str_random(5)) . date('jn'),
'returnUrl' => $url,
];
}
public function checkRegisterOrderErrorCode($registerOrderResponse)
{
return !isset($registerOrderResponse['errorCode']);
}
public function checkReturnUrlParams($returnUrlParams, $payment)
{
if($payment) {
return true;
} else {
return false;
}
}
public function checkOrderStatusResponseCodes($orderStatusResponse)
{
return $orderStatusResponse['errorCode'] == 0 && $orderStatusResponse['orderStatus'] == 2;
}
}

View File

@ -0,0 +1,61 @@
<?php namespace TPS\Birzha\Classes;
use TPS\Birzha\Models\Settings;
class Senagat extends Payment
{
protected function getApiCredentials()
{
return [
'userName' => Settings::getValue('api_login_senagat'),
'password' => Settings::getValue('api_password_senagat'),
];
}
protected function getApiUrl()
{
return 'https://epg.senagatbank.com.tm/epg/rest/';
}
protected function getRegistrationUri()
{
return 'register.do';
}
protected function getStatusUri()
{
return 'getOrderStatus.do';
}
protected function registerOrderParams($payment, $url)
{
return [
'orderNumber' => strtoupper(str_random(5)) . date('jn'),
'amount' => $payment->amount * 100, //multiply by 100 to obtain tenge
'currency' => 934,
'description' => 'Balansa pul goşmak - Birža şahsy otag',
'returnUrl' => $url,
];
}
public function checkRegisterOrderErrorCode($registerOrderResponse)
{
return !isset($registerOrderResponse['errorCode']);
}
public function checkReturnUrlParams($returnUrlParams, $payment)
{
if($payment) {
return true;
} else {
return false;
}
}
public function checkOrderStatusResponseCodes($orderStatusResponse)
{
return $orderStatusResponse['ErrorCode'] == 0 && $orderStatusResponse['OrderStatus'] == 2;
}
}

View File

@ -1,13 +1,10 @@
<?php namespace TPS\Birzha\Components;
use Cms\Classes\ComponentBase;
use Cms\Classes\Page;
use Illuminate\Validation\Rule;
use Illuminate\Support\Facades\Config;
use October\Rain\Exception\AjaxException;
use October\Rain\Support\Facades\Event;
use TPS\Birzha\Models\Payment;
use October\Rain\Network\Http;
use TPS\Birzha\Models\Settings;
use TPS\Birzha\Classes\Payment as CardApi;
class Balance extends ComponentBase
@ -27,8 +24,9 @@ class Balance extends ComponentBase
$data = post();
$rules = [
// 'payment_type' => 'required',
'amount' => 'required|numeric'
'amount' => 'required|numeric',
'card_type' => 'required_if:payment_type,online',
'card_type' => 'in:halkbank,rysgal,senagat'
];
$this->validateForm($data, $rules);
@ -60,19 +58,27 @@ class Balance extends ComponentBase
$url = $this->controller->pageUrl('bank_result.htm', ['payment_id' => $payment->id]);
$response = CardApi::registerOrder($payment, $url);
// Halkbank or Rysgal or Senagat
$onlinePaymentClass = Config::get('bank.' . $formData['card_type'] . '.class');
$onlinePayment = new $onlinePaymentClass;
$response = $onlinePayment->registerOrder($payment, $url);
$result = json_decode($response->body,true);
if($result['errorCode'] == 0) {
$successfullyRegistered = $onlinePayment->checkRegisterOrderErrorCode($result);
if($successfullyRegistered) {
$payment->order_id = $result['orderId'];
$payment->save();
return $result['formUrl'];
} else {
return false;
}
}
/**
@ -91,8 +97,7 @@ class Balance extends ComponentBase
$newPayment->user_id = \Auth::user()->id;
$newPayment->amount = 0;
$newPayment->payment_type = "bank";
// $newPayment->status = "new";
// $newPayment->save();
// attach file to payment
$newPayment->bank_file = \Input::file('bank_file');
@ -120,7 +125,7 @@ class Balance extends ComponentBase
$newPayment->user_id = \Auth::user()->id;
$newPayment->amount = $formData['amount'];
$newPayment->payment_type = $formData['payment_type'];
// $newPayment->status = "new";
$newPayment->card_type = isset($formData['card_type']) ? $formData['card_type'] : null;
$newPayment->save();
// attach file to payment

View File

@ -1,12 +1,10 @@
<?php namespace Tps\Birzha\Components;
use Cms\Classes\ComponentBase;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Redirect;
use October\Rain\Support\Facades\Event;
use TPS\Birzha\Models\Payment;
use October\Rain\Network\Http;
use TPS\Birzha\Models\Settings;
use TPS\Birzha\Classes\Payment as CardApi;
class PaymentApi extends ComponentBase
{
@ -40,54 +38,37 @@ class PaymentApi extends ComponentBase
return Redirect::to('/404');
}
if($payment && \Input::get('status') === 'success' && \Input::get('orderId') === $payment->order_id) {
$responce = json_decode(CardApi::getStatus($payment->order_id), true);
$onlinePaymentClass = Config::get('bank.' . $payment->card_type . '.class');
$onlinePayment = new $onlinePaymentClass;
// if( $responce['ErrorCode'] == 0 && $responce['OrderStatus'] == 2) {
if( $responce['ErrorCode'] == 0) {
if($onlinePayment->checkReturnUrlParams(\Input::all(), $payment)) {
// todo check order Status
$responce = json_decode($onlinePayment->getStatus($payment->order_id), true);
if($onlinePayment->checkOrderStatusResponseCodes($responce)) {
// if page bank_result page is refreshed
if($payment->status === 'approved') {
return Redirect::to('/');
}
$this->orderStatusCode = $responce['OrderStatus'];
$payment->status = 'approved';
switch ($this->orderStatusCode) {
case 2: // OrderStatus 2 OK - the best scenario
$payment->status = 'approved';
if($payment->save()){
Event::fire('tps.payment.received',[$payment]);
$this->balance_message = trans('validation.balance.fill_up_succes');
}
break;
case 0:
$this->balance_message = trans('validation.balance.fill_up_succes_but_delayed');
break;
default:
$payment->status = 'failed';
if($payment->save()) {
$this->balance_message = trans('validation.balance.fill_up_fail');
}
break;
if($payment->save()){
Event::fire('tps.payment.received',[$payment]);
$this->balance_message = trans('validation.balance.fill_up_succes');
}
} else {
// if page bank_result page is refreshed
if($payment->status === 'approved') {
return Redirect::to('/');
}
$payment->status = 'failed';
if($payment->save()) {
$this->balance_message = trans('validation.balance.fill_up_fail');
}
}
} else {
// if page bank_result page is refreshed
@ -101,5 +82,6 @@ class PaymentApi extends ComponentBase
$this->balance_message = trans('validation.balance.fill_up_fail');
}
}
}
}

View File

@ -35,7 +35,21 @@
<span class="error_txt" data-validate-for="amount"></span>
</div>
<button type="submit" class="send_btn">
<!-- todo select box with banks -->
<div class="post_input p-b">
<label>Bank <span>*</span> </label>
<div class="my-select">
<select class="category-select" name="card_type">
<option value="halkbank">Halkbank</option>
<option value="halkbank">Halkbank</option>
<!-- <option value="rysgal">Rysgal Bank</option> -->
<option value="senagat">Senagat PBT</option>
</select>
</div>
</div>
<!-- end select box -->
<button type="submit" class="pay_btn">
{{'account.pay'|_}}
</button>
@ -132,7 +146,7 @@
</div>
</div>
<button class="send_btn" type="submit">
<button class="pay_btn" type="submit">
{{'account.send'|_}}
</button>

View File

@ -21,3 +21,6 @@ columns:
status:
label: Status
type: status
card_type:
label: 'Card (Bank)'
type: text

View File

@ -53,12 +53,32 @@ tabs:
api_login:
tab: Payment
label: Payment api Login
label: Payment api Login (Halkbank)
span: left
api_password:
tab: Payment
label: Payment api password
label: Payment api password (Halkbank)
span: right
api_login_rysgal:
tab: Payment
label: Payment api Login (Rysgal Bank)
span: left
api_password_rysgal:
tab: Payment
label: Payment api password (Rysgal Bank)
span: right
api_login_senagat:
tab: Payment
label: Payment api Login (Senagat PTB)
span: left
api_password_senagat:
tab: Payment
label: Payment api password (Senagat PTB)
span: right
fee:
tab: Payment
label: Fee per product

View File

@ -0,0 +1,23 @@
<?php namespace TPS\Birzha\Updates;
use Schema;
use October\Rain\Database\Updates\Migration;
class BuilderTableUpdateTpsBirzhaPayments5 extends Migration
{
public function up()
{
Schema::table('tps_birzha_payments', function($table)
{
$table->string('card_type')->nullable();
});
}
public function down()
{
Schema::table('tps_birzha_payments', function($table)
{
$table->dropColumn('card_type');
});
}
}

View File

@ -272,3 +272,6 @@
1.0.97:
- 'Updated table users'
- builder_table_update_users_table_04_02_2022_15_20.php
1.0.98:
- 'Updated table tps_birzha_payments'
- builder_table_update_tps_birzha_payments_5.php

View File

@ -815,6 +815,19 @@ li {
display: block;
}
.pay_btn {
background: var(--blue);
color: #fff;
border: none;
border-radius: 5px;
padding: 20px 50px;
cursor: pointer;
font-size: var(--text-18);
font-weight: 400;
line-height: 1;
display: block;
}
.post_footer-text {
color: #E2E2E2;
font-size: 20px;
@ -5706,6 +5719,10 @@ input::-webkit-calendar-picker-indicator {
-o-object-fit: contain;
}
.pay_btn {
margin: 20px auto 0;
}
/* Mobile User links end ================= */
.info_block:last-child .header_contact:last-child {
@ -6399,6 +6416,10 @@ input::-webkit-calendar-picker-indicator {
margin: 0;
}
.pay_btn {
font-size: 14px;
padding: 15px 50px;
}
/* --------------------------------- */