PayStack.php 4.7 KB

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