PayStack.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 $reference
  56. * @param int $amount
  57. * @return array
  58. */
  59. public static function transactionRefund(string $reference, int $amount)
  60. {
  61. $secretKey = \Yii::$app->Paystack->testSecretKey;
  62. $url = "https://api.paystack.co/refund";
  63. $fields = [
  64. 'transaction' => $reference,
  65. 'amount' => $amount,
  66. ];
  67. $fields_string = http_build_query($fields);
  68. //open connection
  69. $ch = curl_init();
  70. //set the url, number of POST vars, POST data
  71. curl_setopt($ch, CURLOPT_URL, $url);
  72. curl_setopt($ch, CURLOPT_POST, true);
  73. curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
  74. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  75. "Authorization: Bearer {$secretKey}",
  76. "Cache-Control: no-cache",
  77. ));
  78. //So that curl_exec returns the contents of the cURL; rather than echoing it
  79. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  80. //execute post
  81. $response = curl_exec($ch);
  82. $response = json_decode($response, true);
  83. return [
  84. 'status' => $response['status'],
  85. 'message' => $response['message'],
  86. 'data' => $response['data'],
  87. ];
  88. }
  89. }