PayStack.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. try {
  74. $curl = curl_init();
  75. curl_setopt_array($curl, [
  76. CURLOPT_URL => "https://api.paystack.co/transaction/verify/{$ref}",
  77. CURLOPT_RETURNTRANSFER => true,
  78. CURLOPT_ENCODING => "",
  79. CURLOPT_MAXREDIRS => 10,
  80. CURLOPT_TIMEOUT => 30,
  81. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  82. CURLOPT_CUSTOMREQUEST => "GET",
  83. CURLOPT_HTTPHEADER => array(
  84. "Authorization: Bearer {$secretKey}",
  85. "Cache-Control: no-cache",
  86. ),
  87. ]);
  88. $response = curl_exec($curl);
  89. $err = curl_error($curl);
  90. curl_close($curl);
  91. if ($err) {
  92. return [
  93. 'status' => false,
  94. 'message' => $err,
  95. ];
  96. }
  97. return json_decode($response, true);
  98. } catch (\Exception $e) {
  99. return [
  100. 'status' => false,
  101. 'message' => $e->getMessage(),
  102. ];
  103. }
  104. }
  105. /**
  106. * 交易退款.
  107. * @param string $reference
  108. * @param int $amount
  109. * @return array
  110. */
  111. public static function transactionRefund(string $reference, int $amount)
  112. {
  113. $secretKey = self::getSecretKey();
  114. try {
  115. $url = "https://api.paystack.co/refund";
  116. $fields = [
  117. 'transaction' => $reference,
  118. 'amount' => $amount,
  119. ];
  120. $fields_string = http_build_query($fields);
  121. //open connection
  122. $curl = curl_init();
  123. //set the url, number of POST vars, POST data
  124. curl_setopt($curl, CURLOPT_URL, $url);
  125. curl_setopt($curl, CURLOPT_POST, true);
  126. curl_setopt($curl, CURLOPT_POSTFIELDS, $fields_string);
  127. curl_setopt($curl, CURLOPT_HTTPHEADER, array(
  128. "Authorization: Bearer {$secretKey}",
  129. "Cache-Control: no-cache",
  130. ));
  131. //So that curl_exec returns the contents of the cURL; rather than echoing it
  132. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  133. //execute post
  134. $response = curl_exec($curl);
  135. $err = curl_error($curl);
  136. curl_close($curl);
  137. if ($err) {
  138. return [
  139. 'status' => false,
  140. 'message' => $err,
  141. ];
  142. }
  143. return json_decode($response, true);
  144. } catch (\Exception $e) {
  145. return [
  146. 'status' => false,
  147. 'message' => $e->getMessage(),
  148. ];
  149. }
  150. }
  151. }