|
|
@@ -5,54 +5,103 @@ namespace common\helpers;
|
|
|
class PayStack
|
|
|
{
|
|
|
/**
|
|
|
- *
|
|
|
+ * 加载secretKey
|
|
|
+ * @return string
|
|
|
*/
|
|
|
- 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'],
|
|
|
- ];
|
|
|
+ public static function getSecretKey(): string
|
|
|
+ {
|
|
|
+ return \Yii::$app->Paystack->environment == 'local' ? \Yii::$app->Paystack->testSecretKey : \Yii::$app->Paystack->liveSecretKey;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 支付.
|
|
|
+ */
|
|
|
+ public static function transactionInit($currency, $amount, $email)
|
|
|
+ {
|
|
|
+ $secretKey = self::getSecretKey();
|
|
|
+
|
|
|
+ try {
|
|
|
+ $url = "https://api.paystack.co/transaction/initialize";
|
|
|
+ $fields = [
|
|
|
+ 'email' => $email,
|
|
|
+ 'amount' => $amount * 100
|
|
|
+ ];
|
|
|
+ $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
|
|
|
+ $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
|
|
|
{
|
|
|
- $payStack = \Yii::$app->Paystack;
|
|
|
- $transaction = $payStack->transaction()->setRequestOptions($ref);
|
|
|
- $transaction->verify();
|
|
|
-
|
|
|
- return [
|
|
|
- 'status' => $transaction->status,
|
|
|
- 'message' => $transaction->message,
|
|
|
- 'data' => $transaction->data,
|
|
|
- ];
|
|
|
+ $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(),
|
|
|
+ ];
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -63,38 +112,48 @@ class PayStack
|
|
|
*/
|
|
|
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'],
|
|
|
- ];
|
|
|
- }
|
|
|
+ $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(),
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|