huafu($config, $merchant, $info); if ($result == 'ok') { //$this->fenzhang($config, $merchant, $info); } return $result; } # 划付 protected 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' => $cash, ]; Dever::log($content, 'yspay_huafu'); $result = $this->curl('202002', $content); if ($result == 'ok') { $data['status'] = 1; } else { $data['status'] = 3; $data['error'] = $result; } $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 'ok'; } # 分账 protected function fenzhang($config, $merchant, $info, $auto = 1) { $this->config = $config; $mid = $merchant['mid']; $cash = $info['fz_cash']; if (!$cash || $cash <= 0) { return false; } $order_num = $this->getOrderNum($info['order_num']); //整理内容信息 $content = [ 'merNo' => $merchant['merno'], 'merOrderNo' => $order_num, 'payAmt' => $cash, 'cardNo' => hash("sha256",$this->config['card']), 'ps' => $merchant['name'] . '分账', ]; Dever::log($content, 'yspay_fenzhang'); $result = $this->curl('202003', $content); if ($result == 'ok') { $data['status'] = 1; } else { $data['status'] = 3; $data['error'] = $result; } $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); } 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 header($code) { $header = array( 'transCode' => $code, 'verNo' => '100', 'srcReqDate' => date("Ymd"), 'srcReqTime' => date("His"), 'srcReqId' => Dever::uuid(), 'channelId' => '043', 'groupId' => $this->config['cash_groupid'], ); return $header; } protected function curl($code, $param, $state = true) { $url = self::$host; $url .= $code; $header = $this->header($code); $header['signature'] = $this->sign($param); echo $url; print_r($header); print_r($param); $curl = Curl::getInstance($url, $param, 'post', true, $header)->setResultHeader(true); $header = $curl->header(); $body = $curl->result(); print_r($header); print_r($body);die; if (strstr($body, '')) { return 'error'; } $body = Dever::json_decode($body); if (isset($header['respCode'])) { if ($header['respCode'] == '99999999') { return $state ? 'ok' : $body; } elseif ($header['respCode'] == 'FAN00012') { return $this->curl($code, $param, $state); } else { return $header['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; } }