150 lines
4.4 KiB
PHP
150 lines
4.4 KiB
PHP
<?php
|
|
/**
|
|
* Created by PhpStorm.
|
|
* User: merdan
|
|
* Date: 7/24/2019
|
|
* Time: 16:48
|
|
*/
|
|
|
|
namespace Sarga\Payment\Methods;
|
|
|
|
use Carbon\Carbon;
|
|
use GuzzleHttp\Client;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Webkul\Payment\Payment\Payment;
|
|
|
|
|
|
class Rysgal extends Payment
|
|
{
|
|
protected $code = 'altynasyr';
|
|
|
|
public function getRedirectUrl():String
|
|
{
|
|
return route('paymentmethod.altynasyr.redirect');
|
|
}
|
|
|
|
private function getApiClient():Client{
|
|
$config = [
|
|
'base_uri' => $this->getConfigData('api_url'),
|
|
'connect_timeout' => 20,//sec
|
|
'timeout' => 20,//sec
|
|
'verify' => false,
|
|
];
|
|
|
|
return new Client($config);
|
|
}
|
|
|
|
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',
|
|
'description'=> "Sarga online sowda jemi: {$cart->grand_total}m.",
|
|
'amount' => $cart->grand_total * 100,// amount w kopeykah
|
|
'returnUrl' => route('paymentmethod.altynasyr.success',['cart_id'=>$cart->id]),
|
|
'failUrl' => route('paymentmethod.altynasyr.cancel')
|
|
],
|
|
];
|
|
try {
|
|
|
|
$result = json_decode($client->post('register.do',$params)->getBody(),true);
|
|
|
|
if((isset($result['errorCode']) && $result['errorCode'] == 0)||(!isset($result['errorCode']) && isset($result['formUrl']))){
|
|
return [
|
|
'data' => [
|
|
"status" => $this->registerOrderId($result['orderId']),
|
|
"url" => $result['formUrl'] ?? ''
|
|
],
|
|
"message" => "redirect to url"
|
|
];
|
|
}
|
|
else{//if already registered or otkazana w dostupe
|
|
Log::info($result);
|
|
return [
|
|
"data" => ["success" => false],
|
|
"message" => $result['errorMessage'] ?? "unable to save order id"
|
|
];
|
|
}
|
|
|
|
}catch(\Exception $e){
|
|
// Log::info($result);
|
|
Log::error($e);
|
|
return ['data' => [
|
|
'status' => false
|
|
],
|
|
'message' =>$e->getMessage(),
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public function registerOrderId($orderId){
|
|
$payment = $this->getCart()->payment;
|
|
$payment->order_id = $orderId;
|
|
// dd($payment);
|
|
// $payment->paymentFormUrl = $formUrl;
|
|
return $payment->save();
|
|
}
|
|
|
|
public function getOrderStatus(){
|
|
try{
|
|
$client = $this->getApiClient();
|
|
|
|
$payment = $this->getCart()->payment;
|
|
|
|
$orderId = request()->get('orderId');
|
|
|
|
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);
|
|
|
|
if($result['ErrorCode'] == 0 && $result['OrderStatus'] == 2){
|
|
return [
|
|
"success" => true,
|
|
"message" => "Payment successful"
|
|
];
|
|
}
|
|
|
|
return [
|
|
"success" => false,
|
|
"message" => $result['errorMessage']
|
|
];
|
|
}
|
|
|
|
return [
|
|
"success" => false,
|
|
"message" => "Malformed request"
|
|
];
|
|
}catch(\Exception $e){
|
|
return [
|
|
"success" => false,
|
|
"message" => $e->getMessage()
|
|
];
|
|
}
|
|
|
|
}
|
|
} |