PayStack.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace common\helpers;
  3. class PayStack
  4. {
  5. /**
  6. *
  7. */
  8. public static function transactionInit($currency, $amount, $email){
  9. $secretKey = \Yii::$app->Paystack->testSecretKey;
  10. $url = "https://api.paystack.co/transaction/initialize";
  11. $fields = [
  12. 'email' => $email,
  13. 'amount' => $amount * 100
  14. ];
  15. $fields_string = http_build_query($fields);
  16. //open connection
  17. $ch = curl_init();
  18. //set the url, number of POST vars, POST data
  19. curl_setopt($ch,CURLOPT_URL, $url);
  20. curl_setopt($ch,CURLOPT_POST, true);
  21. curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
  22. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  23. "Authorization: Bearer {$secretKey}",
  24. "Cache-Control: no-cache",
  25. ));
  26. //So that curl_exec returns the contents of the cURL; rather than echoing it
  27. curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
  28. //execute post
  29. $result = curl_exec($ch);
  30. $result = json_decode($result, true);
  31. return [
  32. 'status' => $result['status'],
  33. 'message' => $result['message'],
  34. 'data' => $result['data'],
  35. ];
  36. }
  37. /**
  38. * 交易支付.
  39. * @param $ref
  40. * @return array
  41. */
  42. public static function transactionVerify($ref): array
  43. {
  44. $payStack = \Yii::$app->Paystack;
  45. $transaction = $payStack->transaction()->setRequestOptions($ref);
  46. $transaction->verify();
  47. return [
  48. 'status' => $transaction->status,
  49. 'message' => $transaction->message,
  50. 'data' => $transaction->data,
  51. ];
  52. }
  53. /**
  54. * 交易退款.
  55. * @param string $transaction
  56. * @param int $amount
  57. */
  58. public static function transactionRefund(string $reference, int $amount)
  59. {
  60. $secretKey = \Yii::$app->Paystack->testSecretKey;
  61. $url = "https://api.paystack.co/refund";
  62. $fields = [
  63. 'transaction' => $reference,
  64. 'amount' => $amount,
  65. ];
  66. $fields_string = http_build_query($fields);
  67. //open connection
  68. $ch = curl_init();
  69. //set the url, number of POST vars, POST data
  70. curl_setopt($ch, CURLOPT_URL, $url);
  71. curl_setopt($ch, CURLOPT_POST, true);
  72. curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
  73. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  74. "Authorization: Bearer {$secretKey}",
  75. "Cache-Control: no-cache",
  76. ));
  77. //So that curl_exec returns the contents of the cURL; rather than echoing it
  78. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  79. //execute post
  80. $response = curl_exec($ch);
  81. $response = json_decode($response, true);
  82. return [
  83. 'status' => $response['status'],
  84. 'message' => $response['message'],
  85. 'data' => $response['data'],
  86. ];
  87. }
  88. }