Created
May 19, 2024 02:42
-
-
Save willnode/6481c826868e4abdb90c625294d428ea to your computer and use it in GitHub Desktop.
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
<?php | |
namespace App\Libraries; | |
use App\Entities\User; | |
use App\Entities\Purchase; | |
use Config\Services; | |
use WpOrg\Requests\Requests; | |
/** | |
* @codeCoverageIgnore | |
*/ | |
class IpaymuGate | |
{ | |
/** | |
* @param $config | |
* @param $params | |
* | |
* @throws Unauthorized | |
* | |
* @return mixed | |
*/ | |
function genSignature($body, $va, $secret) | |
{ | |
$requestBody = strtolower(hash('sha256', $body)); | |
$stringToSign = 'POST:' . $va . ':' . $requestBody . ':' . $secret; | |
$signature = hash_hmac('sha256', $stringToSign, $secret); | |
return $signature; | |
} | |
public function request($params) | |
{ | |
$va = Services::request()->config->ipaymuVa; | |
$secret = Services::request()->config->ipaymuKey; | |
$body = json_encode($params, JSON_UNESCAPED_SLASHES); | |
$signature = $this->genSignature($body, $va, $secret); | |
$timestamp = Date('YmdHis'); | |
$head = [ | |
'Accept' => 'application/json', | |
'Content-Type' => 'application/json', | |
'va' => $va, | |
'signature' => $signature, | |
'timestamp' => $timestamp, | |
]; | |
$url = ENVIRONMENT === 'production' ? 'https://my.ipaymu.com/api/v2/payment' : 'https://sandbox.ipaymu.com/api/v2/payment'; | |
$response = Requests::post($url, $head, $body, ['timeout' => 30]); | |
log_message('notice', 'PAYMENT IPAYMU :::: ' . json_encode($head) . '::::' . $body . '::::' . $response->body); | |
return json_decode($response->body, true); | |
} | |
/** | |
* @param Purchase[] $purchases | |
* @param User $user | |
* @return mixed | |
*/ | |
public function createPayment(array $purchases, User $user) | |
{ | |
$va = Services::request()->config->ipaymuVa; | |
$secret = Services::request()->config->ipaymuSecret; | |
$carts = []; | |
$ids = []; | |
$chals = []; | |
foreach ($purchases as $item) { | |
$carts['product'][] = "" . $item->nice_message; | |
$carts['price'][] = "" . $item->metadata->price; | |
$carts['description'][] = "" . $item->nice_message; | |
$carts['qty'][] = "1"; | |
$ids[] = $item->id; | |
$chals[] = $item->metadata->_challenge; | |
} | |
$ids = implode(',', $ids); | |
$chals = implode(',', $chals); | |
$body = array_merge([ | |
'account' => $va, | |
'buyerName' => $user->name, | |
'buyerEmail' => $user->email, | |
'returnUrl' => base_url("user/host/?message=payment-return"), | |
'cancelUrl' => base_url("user/host/?message=payment-cancel"), | |
'notifyUrl' => base_url("api/notify?id={$ids}&challenge={$chals}&secret=$secret"), | |
'referenceId' => strval($purchases[0]->id), | |
], $carts); | |
return $this->request($body); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment