| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- <?php
- namespace app\extra\jhfPay;
- use yzh52521\EasyHttp\Http;
- class Pay
- {
- protected string $gateway = "https://payapi.juhefu.com/";
- protected array $config = [];
- public function config(array $config = [])
- {
- $this->config = $config;
- return $this;
- }
- /**
- * 分账完之后立马提现
- * @param array $param
- * @return array
- */
- public function createBalanceWithdraw(array $param = []): array
- {
- return $this->paramData($param,"api/member/balance_withdraw_a");
- }
- /**
- * 基于余额分账
- * @param array $param
- * @return array
- */
- public function createBalancePay(array $param = []): array
- {
- return $this->paramData($param,"api/account/balance_pay");
- }
- /**
- * 绑定用户结算卡
- * @param array $param
- * @return array
- */
- public function createMember(array $param = []): array
- {
- return $this->paramData($param,"api/member/create_user_a");
- }
- /**
- * 更新结算卡信息
- * @param array $param
- * @return array
- */
- public function updateMember(array $param = []): array
- {
- return $this->paramData($param,"api/member/update_account_a");
- }
- /**
- * 账户可用余额查询
- * @param array $param
- * @return array [balance,freeze_balance,divide_balance,total]
- */
- public function getBalance(array $param = []): array
- {
- return $this->paramData($param,"api/member/balance_query_a");
- }
- /**
- * 创建支付
- * @param array $param
- * @return array
- */
- public function createPay(array $param = []): array
- {
- return $this->paramData($param,"api/payment/create_payment");
- }
- /**
- * 创建退款
- * @param array $param
- * @return array
- */
- public function createRefund(array $param = []): array
- {
- return $this->paramData($param,"api/payment/payment_refund");
- }
- protected function paramData(array $param = [],string $url = ""): array
- {
- //转为json格式业务报文
- $encryptData = json_encode($param, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
- //对业务报文进行aes加密
- $reqCipher = Utils::aes_encrypt($encryptData, $this->config['aeskey']);
- //报文拼接签名字符串并进行SHA256withRSA签名
- $param = $this->nestedSort($param);
- $signData = Json_encode($param,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
- $signData = Utils::rsa_sign($signData, $this->config['prikey']);
- $time = microtime(true);
- $timestamp = (int)$time; // 整数部分为时间戳
- $microseconds = ($time - $timestamp) * 1000; // 小数部分转换为毫秒
- $formattedTime = date("YmdHis", $timestamp) . sprintf('%03d', $microseconds);
- $post_data = [
- "merId" => $this->config['mch_id'],
- "sign" => $signData,
- "reqCipher" => $reqCipher,
- "reqTime" => $formattedTime
- ];
- $resp = Http::asJson()->post($this->gateway.$url,$post_data)->array();
- if (array_key_exists("error_msg", $resp)) //失败
- {
- return ['code' => 0,"error_code"=>$resp["error_code"], "error_msg"=>$resp["error_msg"]];
- }
- $resCipher = Utils::aes_decrypt($resp['resCipher'], $this->config['aeskey']);
- // 返回 [expend][pay_info]
- return json_decode($resCipher,true);
- }
- /**
- * @param $array
- * @return array
- */
- protected function ksort_recursive(&$array): array
- {
- // 先对当前层数组按键名排序
- ksort($array);
- // 遍历数组中的每个元素
- foreach ($array as &$value) {
- // 如果元素是数组,递归调用排序函数
- if (is_array($value)) {
- $this->ksort_recursive($value);
- }
- }
- unset($value); // 解除引用
- }
- /**
- * 对多维数组进行递归字母顺序排序(每层按键名排序)
- *
- * @param array $array 待排序的数组
- * @return array 排序后的数组
- */
- protected function nestedSort(array $array): array
- {
- // 对当前层的键进行排序
- ksort($array);
- // 递归处理子数组
- foreach ($array as $key => &$value) {
- if (is_array($value)) {
- $value = $this->nestedSort($value);
- }
- }
- return $array;
- }
- }
|