sarga/packages/Sarga/Payment/Methods/Rysgal.php

155 lines
4.6 KiB
PHP
Raw Normal View History

2022-02-28 09:19:57 +00:00
<?php
/**
* Created by PhpStorm.
* User: merdan
* Date: 7/24/2019
* Time: 16:48
*/
namespace Sarga\Payment\Methods;
use Carbon\Carbon;
use GuzzleHttp\Client;
2023-02-23 10:35:44 +00:00
use Illuminate\Support\Facades\Log;
2022-02-28 09:19:57 +00:00
use Webkul\Payment\Payment\Payment;
2023-02-24 11:08:19 +00:00
class Rysgal extends Payment
2022-02-28 09:19:57 +00:00
{
protected $code = 'altynasyr';
public function getRedirectUrl():String
{
return route('paymentmethod.altynasyr.redirect');
}
private function getApiClient():Client{
return new Client([
'base_uri' => $this->getConfigData('api_url'),
2023-02-23 10:52:09 +00:00
'connect_timeout' => 20,//sec
'timeout' => 20,//sec
2023-02-23 10:23:55 +00:00
'verify' => false,
2022-02-28 09:19:57 +00:00
]);
}
public function isRegistered(){
$payment = $this->getCart()->payment;
return (!empty($payment) && !empty($payment->orderId));
}
public function registerOrder(){
$cart = $this->getCart();
$lifeTime = config('session.lifetime',10);//10 minutes
$client = $this->getApiClient();
$params =[
'form_params' => [
'userName' => $this->getConfigData('business_account'),//'103161020074',
'password' => $this->getConfigData('account_password'),//'E12wKp7a7vD8',
'sessionTimeoutSecs' => $lifeTime * 30, //(600 sec)
'orderNumber' =>$cart->id . Carbon::now()->timestamp,
'currency' => 934,
'language' => 'ru',
2023-02-23 10:55:35 +00:00
'description'=> "Sarga online sowda jemi: {$cart->grand_total}m.",
2023-02-23 09:48:56 +00:00
'amount' => $cart->grand_total * 100,// amount w kopeykah
'returnUrl' => route('paymentmethod.altynasyr.success',['cart_id'=>$cart->id]),
2022-02-28 09:19:57 +00:00
'failUrl' => route('paymentmethod.altynasyr.cancel')
],
];
2023-02-23 09:48:56 +00:00
try {
$result = json_decode($client->post('register.do',$params)->getBody(),true);
2023-02-23 10:49:59 +00:00
return [
2023-02-23 10:42:00 +00:00
'data' => [
'url' => $result['formUrl'],
'status' => true
],
'message' => 'Redirect to payment gateway',
2023-02-23 10:49:59 +00:00
];
2023-02-23 10:42:00 +00:00
// Log::info($result);
// if($result['errorCode'] == 0){
// return [
// "success" => $this->registerOrderId($result['orderId']),
// "url" => $result['formUrl'],
// "message" => "unable to save order id"
// ];
// }
// else{//if already registered or otkazana w dostupe
// return [
// "success" => false,
// "message" => $result['errorMessage']
// ];
// }
2023-02-23 09:48:56 +00:00
}catch(\Exception $e){
// Log::info($result);
2023-02-23 10:42:00 +00:00
// Log::error(c);
2023-02-23 10:49:59 +00:00
return ['data' => [
'status' => false
],
2023-02-23 10:42:00 +00:00
'message' =>$e->getMessage(),
2023-02-23 10:49:59 +00:00
];
2023-02-23 09:48:56 +00:00
}
2022-02-28 09:19:57 +00:00
}
public function registerOrderId($orderId){
$payment = $this->getCart()->payment;
$payment->order_id = $orderId;
// dd($payment);
// $payment->paymentFormUrl = $formUrl;
$payment->save();
}
public function getOrderStatus(){
2023-02-23 09:48:56 +00:00
try{
$client = $this->getApiClient();
$payment = $this->getCart()->payment;
2023-02-24 11:35:11 +00:00
2023-02-23 09:48:56 +00:00
$orderId = request()->get('orderId');
2023-02-24 11:35:11 +00:00
Log::info($orderId,$payment);
2023-02-23 09:48:56 +00:00
if($payment && $payment->order_id === $orderId){
$params = [
'form_params' => [
'userName' => $this->getConfigData('business_account'),
'password' => $this->getConfigData('account_password'),
'orderId' => $payment->order_id,
]
];
$result = json_decode($client->post('getOrderStatus.do',$params)->getBody(),true);
2023-02-24 11:13:18 +00:00
Log::info($result);
2023-02-23 09:48:56 +00:00
2023-02-24 11:08:19 +00:00
if($result['errorCode'] == 0 && $result['orderStatus'] == 2){
2023-02-23 09:48:56 +00:00
return [
"success" => true,
"message" => "Payment successful"
];
}
return [
"success" => false,
2023-02-24 11:13:18 +00:00
"message" => $result['errorMessage']
2023-02-23 09:48:56 +00:00
];
}
return [
"success" => false,
"message" => "Malformed request"
];
}catch(\Exception $e){
return [
"success" => false,
"message" => $e->getMessage()
];
}
2022-02-28 09:19:57 +00:00
}
}