<?phpclass unionpay{ /** * 银联手机控件支付测试环境DEMO */ public static $unionpay_config = [ // 签名证书路径 'sign_cert_path' => 'certs/700000000000001_acp.pfx', // 签名证书密码 'sign_cert_pwd' => '000000', //商户号 'merid' => '777290058112628', // 验签证书 'verify_cert_path' => 'certs/verify_sign_acp.cer', // 密码加密证书 'encript_cert_path' =>'certs/RSA2048_PROD_index_22.cer', // 验签证书路径 'verify_cert_dir' => 'certs', // 前台请求地址 'front_trans_url' => 'https://101.231.204.80:5000/gateway/api/frontTransReq.do', // 后台请求地址 'back_trans_url' => 'https://101.231.204.80:5000/gateway/api/backTransReq.do', // 批量交易 'batch_trans_url' => 'https://101.231.204.80:5000/gateway/api/batchTrans.do', //单笔查询请求地址 'single_query_url' => 'https://101.231.204.80:5000/gateway/api/queryTrans.do', //文件传输请求地址 'file_query_url' => 'https://101.231.204.80:9080/', //有卡交易地址 'card_request_url' => 'https://101.231.204.80:5000/gateway/api/cardTransReq.do', //App交易地址 'app_requert_url' => 'https://101.231.204.80:5000/gateway/api/appTransReq.do' ]; /** * 银联支付 * @param $out_trade_no 交易号 * @param $total_fee 价格 * @param $subject 商品名 * @return bool */ public function unionpay($out_trade_no, $total_fee, $subject) { $api_url = 'https://www.iiitp.com'; $backUrl = 'https://www.iiitp.com/backurl'; $cert_id = $this->getCertId(static::$unionpay_config['sign_cert_path']); $params = array( 'version' => '5.0.0', //版本号 'encoding' => 'UTF-8', //编码方式 'certId' => $cert_id, //证书ID 'txnType' => '01', //交易类型 'txnSubType' => '01', //交易子类 'bizType' => '000201', //业务类型 'frontUrl' => $api_url, //前台通知地址 'backUrl' => $backUrl, //后台通知地址 'signMethod' => '01', //签名方法 'channelType' => '08', //渠道类型 'accessType' => '0', //接入类型 'merId' => static::$unionpay_config['merid'], //商户代码 'orderId' => $out_trade_no, //商户订单号 'txnTime' => date('YmdHis'), //订单发送时间 'txnAmt' => $total_fee * 100, //交易金额 'currencyCode' => '156', //交易币种 'defaultPayType' => '0001', //默认支付方式0001:认证支付,0004:储值卡支付,0005:IC 卡支付, //0201:网银支付,0404:互联网订单手机支付 'reqReserved' => $subject, //原样返回数据 ); $this->unionSign($params); $front_uri = static::$unionpay_config['app_requert_url']; $string = $this->sendHttpRequest($params, $front_uri); $result = $this->coverStringToArray($string); $tn = $result['tn']; echo json_encode(['tn' => $tn]); return false; } /** *银联回调 */ public function unotify() { error_log('银联回调返回数据:'.var_export($result, true)); $r = $this->verify($_POST); if ($r) { //验签成功 $sn = $result['orderId']; $txnAmt = $result['txnAmt']; $subject = $result['reqReserved']; if ($result['respCode'] == '00') { $price = $txnAmt / 100; $time = date('Y-m-d H:i:s'); //所有的更新操作 } else { error_log('银联支付失败!'); } } else { error_log('银联回调验签失败!' . var_export($result, true)); } return false; } //获取证书ID public function getCertId($cert_path) { $sign_cert_pwd = static::$unionpay_config['sign_cert_pwd']; $pkcs12certdata = file_get_contents($cert_path); openssl_pkcs12_read($pkcs12certdata, $certs, $sign_cert_pwd); $x509data = $certs ['cert']; openssl_x509_read($x509data); $certdata = openssl_x509_parse($x509data); $cert_id = $certdata ['serialNumber']; return $cert_id; } //银联签名 public function unionSign(&$params) { $sign_cert_path = static::$unionpay_config['sign_cert_path']; if (isset($params['transTempUrl'])) { unset($params['transTempUrl']); } // 转换成key=val&串 $params_str = $this->coverParamsToString($params); $params_sha1x16 = sha1($params_str, false); // 签名证书路径 $private_key = $this->getPrivateKey($sign_cert_path); // 签名 $sign_falg = openssl_sign($params_sha1x16, $signature, $private_key, OPENSSL_ALGO_SHA1); if ($sign_falg) { $signature_base64 = base64_encode($signature); $params ['signature'] = $signature_base64; } else { echo 'sign error'; error_log('签名失败');//记录到错误日志 error_log('签名串' . $params_str); } } /** * 获取私钥 */ public function getPrivateKey($cert_path) { $sign_cert_pwd = static::$unionpay_config['sign_cert_pwd']; $pkcs12 = file_get_contents($cert_path); openssl_pkcs12_read($pkcs12, $certs, $sign_cert_pwd); return $certs ['pkey']; } /** * 后台交易 HttpClient通信 * @param $params * @param $url * @return mixed */ public function sendHttpRequest($params, $url) { $opts = $this->getRequestParamString($params); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//不验证证书 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);//不验证HOST curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-type:application/x-www-form-urlencoded;charset=UTF-8', )); curl_setopt($ch, CURLOPT_POSTFIELDS, $opts); /* * 设置cURL 参数,要求结果保存到字符串中还是输出到屏幕上。 */ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 运行cURL,请求网页 $html = curl_exec($ch); // close cURL resource, and free up system resources curl_close($ch); return $html; } /** * 组装报文. * @param $params * @return string */ public function getRequestParamString($params) { $params_str = ''; foreach ($params as $key => $value) { $params_str .= ($key . '=' . (!isset($value) ? '' : urlencode($value)) . '&'); } return substr($params_str, 0, strlen($params_str) - 1); } /** * 字符串转换为 数组 * @param unknown_type $str * @return multitype:unknown */ public function coverStringToArray($str) { $result = array(); if (!empty($str)) { $temp = preg_split('/&/', $str); if (!empty($temp)) { foreach ($temp as $key => $val) { $arr = preg_split('/=/', $val, 2); if (!empty($arr)) { $k = $arr ['0']; $v = $arr ['1']; $result [$k] = $v; } } } } return $result; }}
https://www.iiitp.com