| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- <?php
- namespace common\helpers;
- class PayStack
- {
- /**
- *
- */
- public static function transactionInit($currency, $amount, $email){
- $secretKey = \Yii::$app->Paystack->testSecretKey;
- $url = "https://api.paystack.co/transaction/initialize";
- $fields = [
- 'email' => $email,
- 'amount' => $amount * 100
- ];
- $fields_string = http_build_query($fields);
- //open connection
- $ch = curl_init();
- //set the url, number of POST vars, POST data
- curl_setopt($ch,CURLOPT_URL, $url);
- curl_setopt($ch,CURLOPT_POST, true);
- curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
- curl_setopt($ch, 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($ch,CURLOPT_RETURNTRANSFER, true);
- //execute post
- $result = curl_exec($ch);
- $result = json_decode($result, true);
- return [
- 'status' => $result['status'],
- 'message' => $result['message'],
- 'data' => $result['data'],
- ];
- }
- /**
- * 交易支付.
- * @param $ref
- * @return array
- */
- public static function transactionVerify($ref): array
- {
- $payStack = \Yii::$app->Paystack;
- $transaction = $payStack->transaction()->setRequestOptions($ref);
- $transaction->verify();
- return [
- 'status' => $transaction->status,
- 'message' => $transaction->message,
- 'data' => $transaction->data,
- ];
- }
- /**
- * 交易退款.
- * @param string $transaction
- * @param int $amount
- */
- public static function transactionRefund(string $reference, int $amount)
- {
- $secretKey = \Yii::$app->Paystack->testSecretKey;
- $url = "https://api.paystack.co/refund";
- $fields = [
- 'transaction' => $reference,
- 'amount' => $amount,
- ];
- $fields_string = http_build_query($fields);
- //open connection
- $ch = curl_init();
- //set the url, number of POST vars, POST data
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
- curl_setopt($ch, 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($ch, CURLOPT_RETURNTRANSFER, true);
- //execute post
- $response = curl_exec($ch);
- $response = json_decode($response, true);
- return [
- 'status' => $response['status'],
- 'message' => $response['message'],
- 'data' => $response['data'],
- ];
- }
- }
|