public function config($config = null) { if (empty($_SERVER['HTTP_USER_AGENT']) || strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') === false && strpos($_SERVER['HTTP_USER_AGENT'], 'Windows Phone') === false) { header('Location: ' . WEB_PATH . '/pay/wxpay_web_url/payinfo/nowechat'); die; } include_once dirname(__FILE__) . "/wxpay/WxPayPubHelper.php"; if (empty($config['pay_type_data'])) { $this->db = System::load_sys_class('model'); $pay = $this->db->GetOne("SELECT * from `@#_pay` where `pay_class` = 'wxpay_web'"); $config['pay_type_data'] = unserialize($pay['pay_key']); } WxPayConf_pub::$APPID = $config['pay_type_data']['APPID']['val']; WxPayConf_pub::$MCHID = $config['pay_type_data']['MCHID']['val']; WxPayConf_pub::$KEY = $config['pay_type_data']['KEY']['val']; WxPayConf_pub::$APPSECRET = $config['pay_type_data']['APPSECRET']['val']; $jsApi = new JsApi_pub(); if (!isset($_GET['code'])) { $url = G_WEB_PATH . '/index.php/pay/wxpay_web_url/?money=' . $config['money'] . '&out_trade_no=' . $config['code']; $url = $jsApi->createOauthUrlForCode(urlencode($url)); header("Location: {$url}"); die; } else { $jsApi->setCode($_GET['code']); $openid = $jsApi->getOpenId(); } // var_dump($_GET); // echo $openid;die; WxPayConf_pub::$SSLCERT_PATH = dirname(__FILE__) . '/cacert/apiclient_cert.pem'; WxPayConf_pub::$SSLKEY_PATH = dirname(__FILE__) . '/cacert/apiclient_key.pem'; //=========步骤2:使用统一支付接口,获取prepay_id============ //使用统一支付接口 $unifiedOrder = new UnifiedOrder_pub(); //设置统一支付接口参数 //设置必填参数 //appid已填,商户无需重复填写 //mch_id已填,商户无需重复填写 //noncestr已填,商户无需重复填写 //spbill_create_ip已填,商户无需重复填写 //sign已填,商户无需重复填写 $unifiedOrder->setParameter("openid", $openid); $unifiedOrder->setParameter("body", "购买商品"); //商品描述 $unifiedOrder->setParameter("out_trade_no", $config['code']); //商户订单号 $unifiedOrder->setParameter("total_fee", $config['money'] * 100); //总金额 $unifiedOrder->setParameter("notify_url", $config['NotifyUrl']); //通知地址 $unifiedOrder->setParameter("trade_type", "JSAPI"); //交易类型 $prepay_id = $unifiedOrder->getPrepayId(); //=========步骤3:使用jsapi调起支付============ $jsApi->setPrepayId($prepay_id); $jsApiParameters = $jsApi->getParameters(); include 'wxpay_web.html.php'; }
public function __construct($wxpayconfig = array()) { self::$APPID = $wxpayconfig['appid']; self::$APPSECRET = $wxpayconfig['appsecret']; self::$MCHID = $wxpayconfig['mchid']; self::$KEY = $wxpayconfig['key']; self::$JS_API_CALL_URL = $wxpayconfig['js_api_call_url']; self::$CURL_TIMEOUT = $wxpayconfig['CURL_TIMEOUT']; self::$SSLCERT_PATH = $wxpayconfig['SSLCERT_PATH']; self::$SSLKEY_PATH = $wxpayconfig['SSLKEY_PATH']; self::$NOTIFY_URL = $wxpayconfig['notifyurl']; self::$RETURN_URL = $wxpayconfig['returnurl']; }
/** * 作用:使用证书,以post方式提交xml到对应的接口url */ function postXmlSSLCurl($xml, $url, $second = 30) { $ch = curl_init(); //超时时间 curl_setopt($ch, CURLOPT_TIMEOUT, $second); //这里设置代理,如果有的话 //curl_setopt($ch,CURLOPT_PROXY, '8.8.8.8'); //curl_setopt($ch,CURLOPT_PROXYPORT, 8080); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); //设置header curl_setopt($ch, CURLOPT_HEADER, FALSE); //要求结果为字符串且输出到屏幕上 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); //设置证书 //使用证书:cert 与 key 分别属于两个.pem文件 //默认格式为PEM,可以注释 curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM'); curl_setopt($ch, CURLOPT_SSLCERT, WxPayConf_pub::SSLCERT_PATH()); //默认格式为PEM,可以注释 curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM'); curl_setopt($ch, CURLOPT_SSLKEY, WxPayConf_pub::SSLKEY_PATH()); //post提交方式 curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); $data = curl_exec($ch); //返回结果 if ($data) { curl_close($ch); return $data; } else { $error = curl_errno($ch); echo "curl出错,错误码:{$error}" . "<br>"; echo "<a href='http://curl.haxx.se/libcurl/c/libcurl-errors.html'>错误原因查询</a></br>"; curl_close($ch); return false; } }