-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from XShaan/master
close #35; Nextpay gateway added
- Loading branch information
Showing
10 changed files
with
350 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Tartan\Larapay\Adapter; | ||
|
||
use Tartan\Larapay\Adapter\Nextpay\Exception; | ||
use Tartan\Larapay\Adapter\Nextpay\Helper; | ||
use Tartan\Log\Facades\XLog; | ||
|
||
/** | ||
* Class Nextpay | ||
* @package Tartan\Larapay\Adapter | ||
*/ | ||
class Nextpay extends AdapterAbstract implements AdapterInterface | ||
{ | ||
|
||
public $endPoint = 'https://nextpay.org/nx/gateway/token'; | ||
public $endPointForm = 'https://nextpay.org/nx/gateway/payment/{trans_id}'; | ||
public $endPointVerify = 'https://nextpay.org/nx/gateway/verify'; | ||
|
||
public $reverseSupport = false; | ||
|
||
/** | ||
* @return string | ||
* @throws Exception | ||
* @throws \Tartan\Larapay\Adapter\Exception | ||
*/ | ||
protected function requestToken(): string | ||
{ | ||
if ($this->getTransaction()->checkForRequestToken() == false) { | ||
throw new Exception('larapay::larapay.could_not_request_payment'); | ||
} | ||
|
||
$this->checkRequiredParameters([ | ||
'api_key', | ||
'amount', | ||
'redirect_url', | ||
'order_id', | ||
]); | ||
|
||
$sendParams = [ | ||
'api_key' => $this->api_key, | ||
'amount' => intval($this->amount), | ||
'order_id' => ($this->order_id), | ||
'payer_desc' => $this->description ? $this->description : '', | ||
'customer_phone' => $this->mobile ? $this->mobile : '', | ||
'callback_uri' => $this->redirect_url, | ||
]; | ||
|
||
try { | ||
XLog::debug('PaymentRequest call', $sendParams); | ||
$result = Helper::post2https($sendParams, $this->endPoint); | ||
$resultObj = json_decode($result); | ||
|
||
XLog::info('PaymentRequest response', $this->obj2array($resultObj)); | ||
|
||
if (isset($resultObj->code)) { | ||
|
||
if ($resultObj->code == -1) { | ||
$this->getTransaction()->setGatewayToken(strval($resultObj->trans_id)); // update transaction reference id | ||
return $resultObj->trans_id; | ||
} else { | ||
throw new Exception('larapay::larapay.nextpay.errors.error_'.$resultObj->code); | ||
} | ||
} else { | ||
throw new Exception('larapay::larapay.invalid_response'); | ||
} | ||
} catch (\Exception $e) { | ||
throw new Exception('Nextpay Fault: ' . $e->getMessage() . ' #' . $e->getCode(), $e->getCode()); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* @return string | ||
* @throws Exception | ||
* @throws \Tartan\Larapay\Adapter\Exception | ||
*/ | ||
protected function generateForm(): string | ||
{ | ||
$authority = $this->requestToken(); | ||
|
||
$form = view('larapay::nextpay-form', [ | ||
'endPoint' => strtr($this->endPointForm, ['{trans_id}' => $authority]), | ||
'submitLabel' => !empty($this->submit_label) ? $this->submit_label : trans("larapay::larapay.goto_gate"), | ||
'autoSubmit' => true, | ||
]); | ||
return $form->__toString(); | ||
} | ||
|
||
/** | ||
* @return array | ||
* @throws Exception | ||
* @throws \Tartan\Larapay\Adapter\Exception | ||
*/ | ||
public function formParams(): array | ||
{ | ||
$authority = $this->requestToken(); | ||
|
||
return [ | ||
'endPoint' => strtr($this->endPointForm, ['{trans_id}' => $authority]), | ||
]; | ||
} | ||
|
||
/** | ||
* @return bool | ||
* @throws Exception | ||
* @throws \Tartan\Larapay\Adapter\Exception | ||
*/ | ||
protected function verifyTransaction(): bool | ||
{ | ||
|
||
if ($this->getTransaction()->checkForVerify() == false) { | ||
throw new Exception('larapay::larapay.could_not_verify_payment'); | ||
} | ||
|
||
$this->checkRequiredParameters([ | ||
'api_key', | ||
'trans_id', | ||
'amount' | ||
]); | ||
|
||
$sendParams = [ | ||
'api_key' => $this->api_key, | ||
'trans_id' => $this->trans_id, | ||
'amount' => $this->amount, | ||
]; | ||
|
||
try { | ||
XLog::debug('PaymentVerification call', $sendParams); | ||
$result = Helper::post2https($sendParams, $this->endPointVerify); | ||
$response = json_decode($result); | ||
XLog::info('PaymentVerification response', $this->obj2array($response)); | ||
if (isset($response->code , $response->Shaparak_Ref_Id)) { | ||
if ($response->code == 0) { | ||
$this->getTransaction()->setVerified(); | ||
$this->getTransaction()->setReferenceId(strval($response->Shaparak_Ref_Id)); // update transaction reference id | ||
return true; | ||
} else { | ||
throw new Exception('larapay::larapay.nextpay.errors.error_'.$response->code); | ||
} | ||
} else { | ||
throw new Exception('larapay::larapay.invalid_response'); | ||
} | ||
} catch (\Exception $e) { | ||
throw new Exception('Nextpay Fault: ' . $e->getMessage() . ' #' . $e->getCode(), $e->getCode()); | ||
} | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function canContinueWithCallbackParameters(): bool | ||
{ | ||
if (!empty($this->parameters['trans_id'])) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* @return string | ||
* @throws \Tartan\Larapay\Adapter\Exception | ||
*/ | ||
public function getGatewayReferenceId(): string | ||
{ | ||
$this->checkRequiredParameters([ | ||
'trans_id', | ||
]); | ||
|
||
return strval($this->trans_id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace Tartan\Larapay\Adapter\Nextpay; | ||
|
||
class Exception extends \Tartan\Larapay\Adapter\Exception { | ||
protected $adapter = 'nextpay'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
namespace Tartan\Larapay\Adapter\Nextpay; | ||
|
||
use Tartan\Log\Facades\XLog; | ||
|
||
class Helper | ||
{ | ||
/** | ||
* CURL POST TO HTTPS | ||
* | ||
* @param $fields_arr | ||
* @param $url | ||
* @return mixed | ||
*/ | ||
public static function post2https($fields_arr, $url) | ||
{ | ||
//open connection | ||
$ch = curl_init(); | ||
|
||
//set the url, number of POST vars, POST data | ||
curl_setopt($ch, CURLOPT_URL, $url); | ||
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); | ||
curl_setopt($ch, CURLOPT_POST, count($fields_arr)); | ||
curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($fields_arr)); | ||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); | ||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | ||
|
||
|
||
//execute post | ||
$res = curl_exec($ch); | ||
|
||
//close connection | ||
curl_close($ch); | ||
|
||
XLog::debug('Nextpay call result: '. $res); | ||
return $res; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.