From b091829fc91c49c09f1f24169bb20485b505d726 Mon Sep 17 00:00:00 2001 From: saparatayev Date: Sat, 24 Jul 2021 13:45:31 +0500 Subject: [PATCH] online payment --- plugins/tps/birzha/Plugin.php | 3 +- plugins/tps/birzha/components/OfferForm.php | 55 ++++++++++++++++++- plugins/tps/birzha/components/PaymentApi.php | 25 +++++++++ .../birzha/components/offerForm/redirect.htm | 6 ++ plugins/tps/birzha/routes.php | 3 + themes/birzha/pages/bank_result.htm | 17 ++++++ 6 files changed, 105 insertions(+), 4 deletions(-) create mode 100644 plugins/tps/birzha/components/PaymentApi.php create mode 100644 plugins/tps/birzha/components/offerForm/redirect.htm create mode 100644 themes/birzha/pages/bank_result.htm diff --git a/plugins/tps/birzha/Plugin.php b/plugins/tps/birzha/Plugin.php index b4be1ce63..4e0faea41 100644 --- a/plugins/tps/birzha/Plugin.php +++ b/plugins/tps/birzha/Plugin.php @@ -175,7 +175,8 @@ class Plugin extends PluginBase 'TPS\Birzha\Components\Offers' => 'offers', 'TPS\Birzha\Components\Singleoffer' => 'singleoffer', 'TPS\Birzha\Components\OfferForm' => 'offerform', - 'TPS\Birzha\Components\Messages' => 'messages' + 'TPS\Birzha\Components\Messages' => 'messages', + 'TPS\Birzha\Components\PaymentApi' => 'paymentapi' ]; } diff --git a/plugins/tps/birzha/components/OfferForm.php b/plugins/tps/birzha/components/OfferForm.php index 53aa0ba9f..759808898 100644 --- a/plugins/tps/birzha/components/OfferForm.php +++ b/plugins/tps/birzha/components/OfferForm.php @@ -34,7 +34,11 @@ class OfferForm extends ComponentBase */ public $countries; - + const REGISTRATION_URI = 'register.do'; + + const STATUS_URI = 'getOrderStatus.do'; + + const API_URL = 'https://mpi.gov.tm/payment/rest/'; public function componentDetails() { return [ @@ -244,7 +248,26 @@ class OfferForm extends ComponentBase } } - + protected function payOnline() { + $payment = $this->createNewPayment(false, 'online'); + + $response = $this->registerOrder($payment); + + $result = json_decode($response->body,true); + + if($result['errorCode'] == 0){ + $payment->order_id = $result['orderId']; + + $payment->save(); + + return $result['formUrl']; + } + else{ + throw new AjaxException( + $result + ); + } + } public function onPayByBankTransfer() { $data = input(); @@ -374,5 +397,31 @@ class OfferForm extends ComponentBase } // payment - + protected function registerOrder($payment) { + $client = self::getClient(self::REGISTRATION_URI); + + $url = $this->controller->pageUrl('bank_result.htm', ['payment_id' => $payment->id]); + + $client->data([ + 'amount' => $payment->amount * 100,//multiply by 100 to obtain tenge + 'currency' => 934, + 'description' => 'Kart üçin döwlet pajy.', + 'orderNumber' => strtoupper(str_random(5)) . date('jn'), + + 'failUrl' => $url . '?status=success', + 'returnUrl' => $url . '?status=failed', + + ]); + + $client->setOption(CURLOPT_POSTFIELDS,$client->getRequestData()); +// dd($client); + return $client->send(); + } + + 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); + } } \ No newline at end of file diff --git a/plugins/tps/birzha/components/PaymentApi.php b/plugins/tps/birzha/components/PaymentApi.php new file mode 100644 index 000000000..0fc809f66 --- /dev/null +++ b/plugins/tps/birzha/components/PaymentApi.php @@ -0,0 +1,25 @@ + 'Payment API', + 'description' => 'Payment API' + ]; + } + + public function defineProperties() + { + return [ + 'payment_id' => [ + 'title' => 'Payment ID', + 'description' => 'Payment ID', + 'default' => '{{ :payment_id }}', + 'type' => 'string', + ] + ]; + } +} \ No newline at end of file diff --git a/plugins/tps/birzha/components/offerForm/redirect.htm b/plugins/tps/birzha/components/offerForm/redirect.htm new file mode 100644 index 000000000..0d802b0ff --- /dev/null +++ b/plugins/tps/birzha/components/offerForm/redirect.htm @@ -0,0 +1,6 @@ + + \ No newline at end of file diff --git a/plugins/tps/birzha/routes.php b/plugins/tps/birzha/routes.php index dcb7770bb..4f726576a 100644 --- a/plugins/tps/birzha/routes.php +++ b/plugins/tps/birzha/routes.php @@ -7,3 +7,6 @@ Route::namespace('TPS\Birzha\Controllers')->group(function () { Route::get('version', 'Settings@version')->name('version'); }); }); + + +// Route::get('payment-result', ['as'=>'paymentReturn','uses'=>'...@checkPayment'] ); diff --git a/themes/birzha/pages/bank_result.htm b/themes/birzha/pages/bank_result.htm new file mode 100644 index 000000000..c0b13e4f9 --- /dev/null +++ b/themes/birzha/pages/bank_result.htm @@ -0,0 +1,17 @@ +title = "bank_result" +url = "/bank_result/:payment_id" +layout = "default" +is_hidden = 0 + +[viewBag] +localeTitle[en] = "bank_result" +localeTitle[ru] = "bank_result" +localeUrl[en] = "/bank_result/:payment_id/:is_payment_cancelled/:is_payment_successful" +localeUrl[ru] = "/bank_result/:payment_id/:is_payment_cancelled/:is_payment_successful" + +[paymentapi] +payment_id = "{{ :payment_id }}" +is_payment_cancelled = "{{ :is_payment_cancelled }}" +is_payment_successful = "{{ :is_payment_successful }}" +== +{% component 'paymentapi' %} \ No newline at end of file