PayStack.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace common\helpers;
  3. class PayStack
  4. {
  5. /**
  6. * 根据运行环境加载secretKey
  7. */
  8. public static function getSecretKey(): string
  9. {
  10. return YII_ENV == YII_ENV_PROD ? \Yii::$app->Paystack->liveSecretKey : \Yii::$app->Paystack->testSecretKey;
  11. // return \Yii::$app->Paystack->testSecretKey;
  12. }
  13. /**
  14. * 支付.
  15. */
  16. public static function transactionInit($currency, $amount, $email)
  17. {
  18. $secretKey = self::getSecretKey();
  19. LoggerTool::info('env: ' . YII_ENV);
  20. if (YII_ENV != YII_ENV_PROD) {
  21. return [
  22. 'status' => true,
  23. 'data' => [
  24. 'amount' => $amount
  25. ]
  26. ];
  27. }
  28. try {
  29. $url = "https://api.paystack.co/transaction/initialize";
  30. $fields = [
  31. 'email' => $email,
  32. 'amount' => $amount * 100
  33. ];
  34. $fields_string = http_build_query($fields);
  35. //open connection
  36. $curl = curl_init();
  37. //set the url, number of POST vars, POST data
  38. curl_setopt($curl,CURLOPT_URL, $url);
  39. curl_setopt($curl,CURLOPT_POST, true);
  40. curl_setopt($curl,CURLOPT_POSTFIELDS, $fields_string);
  41. curl_setopt($curl, CURLOPT_HTTPHEADER, array(
  42. "Authorization: Bearer {$secretKey}",
  43. "Cache-Control: no-cache",
  44. ));
  45. //So that curl_exec returns the contents of the cURL; rather than echoing it
  46. curl_setopt($curl,CURLOPT_RETURNTRANSFER, true);
  47. //execute post
  48. $result = curl_exec($curl);
  49. $err = curl_error($curl);
  50. curl_close($curl);
  51. if ($err) {
  52. return [
  53. 'status' => false,
  54. 'message' => $err,
  55. ];
  56. }
  57. return json_decode($result, true);
  58. } catch (\Exception $e) {
  59. return [
  60. 'status' => false,
  61. 'message' => $e->getMessage(),
  62. ];
  63. }
  64. }
  65. /**
  66. * 交易结果校验.
  67. * @param $ref
  68. * @return array
  69. */
  70. public static function transactionVerify($ref): array
  71. {
  72. $secretKey = self::getSecretKey();
  73. if (YII_ENV != YII_ENV_PROD) {
  74. return [
  75. 'status' => true,
  76. 'data' => ['amount' => $ref['amount']],
  77. ];
  78. }
  79. try {
  80. $curl = curl_init();
  81. curl_setopt_array($curl, [
  82. CURLOPT_URL => "https://api.paystack.co/transaction/verify/{$ref}",
  83. CURLOPT_RETURNTRANSFER => true,
  84. CURLOPT_ENCODING => "",
  85. CURLOPT_MAXREDIRS => 10,
  86. CURLOPT_TIMEOUT => 30,
  87. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  88. CURLOPT_CUSTOMREQUEST => "GET",
  89. CURLOPT_HTTPHEADER => array(
  90. "Authorization: Bearer {$secretKey}",
  91. "Cache-Control: no-cache",
  92. ),
  93. ]);
  94. $response = curl_exec($curl);
  95. $err = curl_error($curl);
  96. curl_close($curl);
  97. if ($err) {
  98. return [
  99. 'status' => false,
  100. 'message' => $err,
  101. ];
  102. }
  103. return json_decode($response, true);
  104. } catch (\Exception $e) {
  105. return [
  106. 'status' => false,
  107. 'message' => $e->getMessage(),
  108. ];
  109. }
  110. }
  111. /**
  112. * 交易退款.
  113. * @param string $reference
  114. * @param int $amount
  115. * @return array
  116. */
  117. public static function transactionRefund(string $reference, int $amount)
  118. {
  119. $secretKey = self::getSecretKey();
  120. try {
  121. $url = "https://api.paystack.co/refund";
  122. $fields = [
  123. 'transaction' => $reference,
  124. 'amount' => $amount,
  125. ];
  126. $fields_string = http_build_query($fields);
  127. //open connection
  128. $curl = curl_init();
  129. //set the url, number of POST vars, POST data
  130. curl_setopt($curl, CURLOPT_URL, $url);
  131. curl_setopt($curl, CURLOPT_POST, true);
  132. curl_setopt($curl, CURLOPT_POSTFIELDS, $fields_string);
  133. curl_setopt($curl, CURLOPT_HTTPHEADER, array(
  134. "Authorization: Bearer {$secretKey}",
  135. "Cache-Control: no-cache",
  136. ));
  137. //So that curl_exec returns the contents of the cURL; rather than echoing it
  138. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  139. //execute post
  140. $response = curl_exec($curl);
  141. $err = curl_error($curl);
  142. curl_close($curl);
  143. if ($err) {
  144. return [
  145. 'status' => false,
  146. 'message' => $err,
  147. ];
  148. }
  149. return json_decode($response, true);
  150. } catch (\Exception $e) {
  151. return [
  152. 'status' => false,
  153. 'message' => $e->getMessage(),
  154. ];
  155. }
  156. }
  157. }