/** * setWxPayConfig * 除 notify 方法外,其他方法的调用请先设置微信支付相关信息,既首先调用本方法 * @return void */ public static function setWxPayConfig($config) { WxPayConfig::$app_id = $config['app_id']; WxPayConfig::$mch_id = $config['mch_id']; WxPayConfig::$key = $config['key']; WxPayConfig::$app_secret = $config['app_secret']; WxPayConfig::$notify_url = $config['notify_url']; }
/** * * 构造获取open和access_toke的url地址 * @param string $code,微信跳转带回的code * * @return string 请求的url */ private function __CreateOauthUrlForOpenid($code) { $urlObj["appid"] = $this->config->getAppId(); $urlObj["secret"] = $this->config->getAppsecret(); $urlObj["code"] = $code; $urlObj["grant_type"] = "authorization_code"; $bizString = $this->ToUrlParams($urlObj); return "https://api.weixin.qq.com/sns/oauth2/access_token?" . $bizString; }
public static function init() { $aConf = Util_Common::getConf('wxpay', null, 'wxpay'); self::$APPID = $aConf['APPID']; self::$MCHID = $aConf['MCHID']; self::$KEY = $aConf['KEY']; self::$APPSECRET = $aConf['APPSECRET']; //self::$JS_API_CALL_URL = $aConf['JS_API_CALL_URL']; self::$SSLCERT_PATH = $aConf['SSLCERT_PATH']; self::$SSLKEY_PATH = $aConf['SSLKEY_PATH']; self::$NOTIFY_URL = $aConf['NOTIFY_URL']; }
/** * 生成签名 * @return string 签名,本函数不覆盖sign成员变量,如要设置签名需要调用SetSign方法赋值 */ public function MakeSign() { //签名步骤一:按字典序排序参数 ksort($this->values); $string = $this->ToUrlParams(); //签名步骤二:在string后加入KEY $string = $string . "&key=" . $this->config->getKey(); //签名步骤三:MD5加密 $string = md5($string); //签名步骤四:所有字符转为大写 $result = strtoupper($string); return $result; }
/** * 以post方式提交xml到对应的接口url * * @param string $xml 需要post的xml数据 * @param string $url url * @param bool $useCert 是否需要证书,默认不需要 * @param int $second url执行超时时间,默认30s * @return mixed * @throws WxPayException */ private function postXmlCurl($xml, $url, $useCert = false, $second = 30) { $ch = curl_init(); //设置超时 curl_setopt($ch, CURLOPT_TIMEOUT, $second); //如果有配置代理这里就设置代理 if ($this->config->getCurlProxyHost() != "0.0.0.0" && $this->config->getCurlProxyPort() != 0) { curl_setopt($ch, CURLOPT_PROXY, $this->config->getCurlProxyHost()); curl_setopt($ch, CURLOPT_PROXYPORT, $this->config->getCurlProxyPort()); } curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); //严格校验 //设置header curl_setopt($ch, CURLOPT_HEADER, false); //要求结果为字符串且输出到屏幕上 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); if ($useCert == true) { //设置证书 //使用证书:cert 与 key 分别属于两个.pem文件 curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM'); curl_setopt($ch, CURLOPT_SSLCERT, $this->config->getSslcertPath()); curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM'); curl_setopt($ch, CURLOPT_SSLKEY, $this->config->getSslkeyPath()); } //post提交方式 curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); //运行curl $data = curl_exec($ch); //返回结果 if ($data) { curl_close($ch); return $data; } else { $error = curl_errno($ch); curl_close($ch); throw new WxPayException("curl出错,错误码:{$error}"); } }
function getWXCfg() { $weChatOptions = get_option('woocommerce_wechatpay_settings'); $WxCfg = new WxPayConfig($weChatOptions["wechatpay_appID"], $weChatOptions["wechatpay_mchId"], $weChatOptions["wechatpay_key"]); $WxCfg->setEnableProxy($weChatOptions["WX_EnableProxy"]); if ($weChatOptions["WX_EnableProxy"]) { $WxCfg->setCURLPROXYHOST($weChatOptions["WX_ProxyHost"]); $WxCfg->setCURLPROXYPORT($weChatOptions["WX_ProxyPort"]); } return $WxCfg; }