PayStack.php 1.8 KB

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