| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- <?php
- namespace common\helpers;
- class PayStack
- {
- public static function verifyWebhookSignature(string $rawBody, string $signature, string $secretKey): bool
- {
- return $signature !== '' && hash_equals(hash_hmac('sha512', $rawBody, $secretKey), $signature);
- }
- /**
- * 根据运行环境加载secretKey
- */
- public static function getSecretKey(): string
- {
- return YII_ENV == YII_ENV_PROD ? \Yii::$app->Paystack->liveSecretKey : \Yii::$app->Paystack->testSecretKey;
- // return \Yii::$app->Paystack->testSecretKey;
- }
- /**
- * 支付.
- */
- public static function transactionInit($currency, $amount, $email, array $metadata = [], string $reference = '')
- {
- $secretKey = self::getSecretKey();
- LoggerTool::info('env: ' . YII_ENV);
- if (YII_ENV != YII_ENV_PROD) {
- if ($reference === '') {
- $reference = bin2hex(random_bytes(16));
- }
- return [
- 'status' => true,
- 'data' => [
- 'amount' => (int) round($amount * 100),
- 'currency' => strtoupper((string) $currency),
- 'reference' => $reference,
- 'authorization_url' => 'https://checkout.paystack.com/' . $reference,
- 'access_code' => $reference,
- ]
- ];
- }
- try {
- $url = "https://api.paystack.co/transaction/initialize";
- $fields = [
- 'email' => $email,
- 'amount' => (int) round($amount * 100),
- 'currency' => strtoupper((string) $currency),
- ];
- if ($reference !== '') {
- $fields['reference'] = $reference;
- }
- if ($metadata) {
- $fields['metadata'] = json_encode($metadata, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
- }
- $fields_string = http_build_query($fields);
- //open connection
- $curl = curl_init();
- //set the url, number of POST vars, POST data
- curl_setopt($curl,CURLOPT_URL, $url);
- curl_setopt($curl,CURLOPT_POST, true);
- curl_setopt($curl,CURLOPT_POSTFIELDS, $fields_string);
- curl_setopt($curl, CURLOPT_HTTPHEADER, array(
- "Authorization: Bearer {$secretKey}",
- "Cache-Control: no-cache",
- ));
- //So that curl_exec returns the contents of the cURL; rather than echoing it
- curl_setopt($curl,CURLOPT_RETURNTRANSFER, true);
- curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
- curl_setopt($curl, CURLOPT_TIMEOUT, 30);
- //execute post
- $result = curl_exec($curl);
- $err = curl_error($curl);
- curl_close($curl);
- if ($err) {
- return [
- 'status' => false,
- 'message' => $err,
- ];
- }
- return json_decode($result, true);
- } catch (\Exception $e) {
- return [
- 'status' => false,
- 'message' => $e->getMessage(),
- ];
- }
- }
- /**
- * 交易结果校验.
- * @param $ref
- * @return array
- */
- public static function transactionVerify($ref): array
- {
- $secretKey = self::getSecretKey();
- try {
- $curl = curl_init();
- curl_setopt_array($curl, [
- CURLOPT_URL => "https://api.paystack.co/transaction/verify/{$ref}",
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_ENCODING => "",
- CURLOPT_MAXREDIRS => 10,
- CURLOPT_TIMEOUT => 30,
- CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
- CURLOPT_CUSTOMREQUEST => "GET",
- CURLOPT_HTTPHEADER => array(
- "Authorization: Bearer {$secretKey}",
- "Cache-Control: no-cache",
- ),
- ]);
- $response = curl_exec($curl);
- $err = curl_error($curl);
- curl_close($curl);
- if ($err) {
- return [
- 'status' => false,
- 'message' => $err,
- ];
- }
- return json_decode($response, true);
- } catch (\Exception $e) {
- return [
- 'status' => false,
- 'message' => $e->getMessage(),
- ];
- }
- }
- /**
- * 交易退款.
- * @param string $reference
- * @param int $amount
- * @return array
- */
- public static function transactionRefund(string $reference, int $amount)
- {
- $secretKey = self::getSecretKey();
- try {
- $url = "https://api.paystack.co/refund";
- $fields = [
- 'transaction' => $reference,
- 'amount' => $amount,
- ];
- $fields_string = http_build_query($fields);
- //open connection
- $curl = curl_init();
- //set the url, number of POST vars, POST data
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_POST, true);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $fields_string);
- curl_setopt($curl, CURLOPT_HTTPHEADER, array(
- "Authorization: Bearer {$secretKey}",
- "Cache-Control: no-cache",
- ));
- //So that curl_exec returns the contents of the cURL; rather than echoing it
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- //execute post
- $response = curl_exec($curl);
- $err = curl_error($curl);
- curl_close($curl);
- if ($err) {
- return [
- 'status' => false,
- 'message' => $err,
- ];
- }
- return json_decode($response, true);
- } catch (\Exception $e) {
- return [
- 'status' => false,
- 'message' => $e->getMessage(),
- ];
- }
- }
- }
|