huafu($config, $merchant, $info); if ($result == 1) { $this->fenzhang($config, $merchant, $info); } return $result; }*/ # 划付 public function huafu($config, $merchant, $info, $auto = 1) { $this->config = $config; $mid = $merchant['mid']; $cash = $info['hf_cash']; $order_num = $this->getOrderNum($info['order_num']); //整理内容信息 $content = [ 'merNo' => $merchant['merno'], //'merOrderNo' => $order_num, 'payAmt' => (string) $cash, 'ps' => $info['order_num'], ]; $result = $this->curl('202002', $content); Dever::log(array('request' => $content, 'result' => $result), 'yspay_huafu'); //$result = 'ok'; if ($result == 'ok') { $data['status'] = 1; } else { $data['status'] = 3; $data['error'] = $result; } $data['yspay_main_id'] = $merchant['yspay_main_id']; $data['merchant_id'] = $merchant['id']; $data['mid'] = $mid; $data['order_num'] = $order_num; $data['type'] = 1; $data['cash'] = $cash; $data['tdate'] = time(); $data['desc'] = $content['ps']; $data['auto'] = $auto; Dever::db('pay/yspay_cash_log')->insert($data); return $data['status']; } # 分账 public function fenzhang($config, $yspay_main, $merchant, $info, $auto = 1) { $this->config = $config; $mid = $yspay_main['cash_mid']; $cash = $info['fz_cash'] + $info['pt_cash']; if (!$cash || $cash <= 0) { return false; } $order_num = $this->getOrderNum($info['order_num']); //整理内容信息 $content = [ 'merNo' => $merchant['merno'], //'merOrderNo' => $order_num, 'payAmt' => (string) $cash, 'cardNo' => hash("sha256", $yspay_main['cash_card']), 'ps' => $info['order_num'], ]; $result = $this->curl('202004', $content); Dever::log(array('request' => $content, 'result' => $result), 'yspay_fenzhang'); //$result = 'ok'; if ($result == 'ok') { $data['status'] = 1; } else { $data['status'] = 3; $data['error'] = $result; } $data['yspay_main_id'] = $merchant['yspay_main_id']; $data['merchant_id'] = $merchant['id']; $data['mid'] = $mid; $data['order_num'] = $order_num; $data['type'] = 2; $data['cash'] = $cash; $data['tdate'] = time(); $data['desc'] = $content['ps']; $data['auto'] = $auto; Dever::db('pay/yspay_cash_log')->insert($data); return $data['status']; } # 查询余额 public function yue($config, $merchant) { $this->config = $config; //整理内容信息 $content = [ 'merNo' => $merchant['merno'], ]; Dever::log($content, 'yspay_yue'); $result = $this->curl('202006', $content, false); if (isset($result['canPayAmt'])) { return $result['canPayAmt']; } return 0; } # 查询订单 public function query($config, $merchant, $orderType, $transDate, $page = 1) { $this->config = $config; //整理内容信息 $content = [ 'merNo' => $merchant['merno'], 'orderType' => $orderType, 'transDate' => $transDate, 'pageIdx' => $page ]; Dever::log($content, 'yspay_query'); $result = $this->curl('202012', $content, false); return $result; } protected function getOrderNum($order_num) { $where['order_num'] = $order_num . '_' . Dever::rand(8, 0); $state = Dever::db('pay/yspay_cash_log')->one($where); if (!$state) { return $where['order_num']; } else { return $this->getOrderNum($order_num); } } protected function common($param, $code) { $param += array( 'transCode' => $code, 'verNo' => '100', 'srcReqDate' => date("Ymd"), 'srcReqTime' => date("His"), 'srcReqId' => Dever::uuid(), 'channelId' => '043', 'groupId' => $this->config['cash_groupid'], ); return $param; } protected function curl($code, $param, $state = true) { $url = self::$host; $url .= $code; $param = $this->common($param, $code); $param['signature'] = $this->sign($param); $curl = Curl::getInstance($url, $param, 'post', true); $body = $curl->result(); if (strstr($body, '')) { return 'error'; } $body = Dever::json_decode($body); if ($code == '202012') { return $body; } if (isset($body['respCode'])) { if ($body['respCode'] == '99999999') { return $state ? 'ok' : $body; } elseif ($body['respCode'] == 'FAN00012') { return $this->curl($code, $param, $state); } else { return $body['respMsg']; } } else { return 'error'; } } protected function sign($data) { $data = $this->getParams($data); $file = $this->config['cash_private_file']; if (!strstr($file, 'http')) { $file = Dever::local($file); } $pkcs12 = file_get_contents($file); openssl_pkcs12_read($pkcs12, $certs, $this->config['cash_private_file_password']); if (!$certs) { Dever::alert('private_key error'); } $privateKey = $certs['pkey']; if (openssl_sign(utf8_encode($data), $binarySignature, $privateKey, OPENSSL_ALGO_SHA256)) { return bin2hex($binarySignature); } else { return ''; } } protected function checkSign($data, $signature) { $file = $this->config['cash_public_file']; if (!strstr($file, 'http')) { $file = Dever::local($file); } $cert = file_get_contents($file); $cert = '-----BEGIN CERTIFICATE-----' . PHP_EOL . chunk_split(base64_encode($cert), 64, PHP_EOL) . '-----END CERTIFICATE-----' . PHP_EOL; $pubKeyId = openssl_get_publickey($cert); $signature = hex2bin($signature); $ok = openssl_verify($data, $signature, $pubKeyId, OPENSSL_ALGO_SHA256); if ($ok == 1) { openssl_free_key($pubKeyId); return true; } return false; } protected function getParams($param) { ksort($param); $result = array(); foreach ($param as $k => $v) { if (is_array($v)) { $v = json_encode($v, JSON_UNESCAPED_UNICODE); } elseif(trim($v) == ""){ continue; } if (is_bool($v)) { $result[] = $v ? "$k=true" : "$k=false"; } else { $result[] = $k . '=' . $v; } } $result = implode('&', $result); return $result; } }