Pay.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace app\extra\jhfPay;
  3. use yzh52521\EasyHttp\Http;
  4. class Pay
  5. {
  6. protected string $gateway = "https://payapi.juhefu.com/";
  7. protected array $config = [];
  8. public function config(array $config = [])
  9. {
  10. $this->config = $config;
  11. return $this;
  12. }
  13. /**
  14. * 分账完之后立马提现
  15. * @param array $param
  16. * @return array
  17. */
  18. public function createBalanceWithdraw(array $param = []): array
  19. {
  20. return $this->paramData($param,"api/member/balance_withdraw_a");
  21. }
  22. /**
  23. * 基于余额分账
  24. * @param array $param
  25. * @return array
  26. */
  27. public function createBalancePay(array $param = []): array
  28. {
  29. return $this->paramData($param,"api/account/balance_pay");
  30. }
  31. /**
  32. * 绑定用户结算卡
  33. * @param array $param
  34. * @return array
  35. */
  36. public function createMember(array $param = []): array
  37. {
  38. return $this->paramData($param,"api/member/create_user_a");
  39. }
  40. /**
  41. * 更新结算卡信息
  42. * @param array $param
  43. * @return array
  44. */
  45. public function updateMember(array $param = []): array
  46. {
  47. return $this->paramData($param,"api/member/update_account_a");
  48. }
  49. /**
  50. * 账户可用余额查询
  51. * @param array $param
  52. * @return array [balance,freeze_balance,divide_balance,total]
  53. */
  54. public function getBalance(array $param = []): array
  55. {
  56. return $this->paramData($param,"api/member/balance_query_a");
  57. }
  58. /**
  59. * 创建支付
  60. * @param array $param
  61. * @return array
  62. */
  63. public function createPay(array $param = []): array
  64. {
  65. return $this->paramData($param,"api/payment/create_payment");
  66. }
  67. /**
  68. * 创建退款
  69. * @param array $param
  70. * @return array
  71. */
  72. public function createRefund(array $param = []): array
  73. {
  74. return $this->paramData($param,"api/payment/payment_refund");
  75. }
  76. protected function paramData(array $param = [],string $url = ""): array
  77. {
  78. //转为json格式业务报文
  79. $encryptData = json_encode($param, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
  80. //对业务报文进行aes加密
  81. $reqCipher = Utils::aes_encrypt($encryptData, $this->config['aeskey']);
  82. //报文拼接签名字符串并进行SHA256withRSA签名
  83. $param = $this->nestedSort($param);
  84. $signData = Json_encode($param,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
  85. $signData = Utils::rsa_sign($signData, $this->config['prikey']);
  86. $time = microtime(true);
  87. $timestamp = (int)$time; // 整数部分为时间戳
  88. $microseconds = ($time - $timestamp) * 1000; // 小数部分转换为毫秒
  89. $formattedTime = date("YmdHis", $timestamp) . sprintf('%03d', $microseconds);
  90. $post_data = [
  91. "merId" => $this->config['mch_id'],
  92. "sign" => $signData,
  93. "reqCipher" => $reqCipher,
  94. "reqTime" => $formattedTime
  95. ];
  96. $resp = Http::asJson()->post($this->gateway.$url,$post_data)->array();
  97. if (array_key_exists("error_msg", $resp)) //失败
  98. {
  99. return ['code' => 0,"error_code"=>$resp["error_code"], "error_msg"=>$resp["error_msg"]];
  100. }
  101. $resCipher = Utils::aes_decrypt($resp['resCipher'], $this->config['aeskey']);
  102. // 返回 [expend][pay_info]
  103. return json_decode($resCipher,true);
  104. }
  105. /**
  106. * @param $array
  107. * @return array
  108. */
  109. protected function ksort_recursive(&$array): array
  110. {
  111. // 先对当前层数组按键名排序
  112. ksort($array);
  113. // 遍历数组中的每个元素
  114. foreach ($array as &$value) {
  115. // 如果元素是数组,递归调用排序函数
  116. if (is_array($value)) {
  117. $this->ksort_recursive($value);
  118. }
  119. }
  120. unset($value); // 解除引用
  121. }
  122. /**
  123. * 对多维数组进行递归字母顺序排序(每层按键名排序)
  124. *
  125. * @param array $array 待排序的数组
  126. * @return array 排序后的数组
  127. */
  128. protected function nestedSort(array $array): array
  129. {
  130. // 对当前层的键进行排序
  131. ksort($array);
  132. // 递归处理子数组
  133. foreach ($array as $key => &$value) {
  134. if (is_array($value)) {
  135. $value = $this->nestedSort($value);
  136. }
  137. }
  138. return $array;
  139. }
  140. }