PayStack.php 4.7 KB

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