PayStack.php 5.1 KB

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