/** * 验证配置 */ public function validateConfig() { static $url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s'; $body = Utility::http(sprintf($url, $this->getConfig('appid'), $this->getConfig('appsecret'))); $json = json_decode($body, true); if (!$json || !empty($json['errcode'])) { return false; } else { return true; } }
/** * 获取access_token */ public function getAccessToken() { static $url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s'; $token = Utility::getCorpAccessToken($this); if (empty($token) || time() > $token['expired']) { $body = Utility::http(sprintf($url, $this->getConfig('appid'), $this->getConfig('appsecret'))); $json = json_decode($body, true); if (!$json || !empty($json['errcode'])) { throw new Exception('Error - WeChatCorp Can not get AccessToken.'); } else { $token['token'] = $json['access_token']; $token['expired'] = time() + $json['expires_in'] - 120; Utility::setCorpAccessToken($token, $this); } } return $token['token']; }
/** * 微信统一下单 * @see https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1 * @see https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_7 下单回调结果 * data { body : 商品描述(128) detail : [可选] 商品详情(8192) attach : [可选] 附加信息(127) out_trade_no: 商户订单号(32) total_fee : 总额(单位为分) time_start : [可选] 交易起始时间 yyyyMMddHHmmss time_expire : [可选] 交易结束时间 yyyyMMddHHmmss goods_tag : [可选] 商品标记(32) notify_url : 回调地址 trade_type : 交易类型 JSAPI:公众号支付 NATIVE:原生扫码支付 APP:app支付 product_id : [可选] 商品ID(32) limit_pay : [可选] 指定支付方式 no_credit openid : OPENID (trade_type=JSAPI 此参数必须) appid : [可选] 公众号appid mch_id : [可选] 商户号 device_info : [可选] WEB nonce_str : [可选] 随机字符 fee_type : [可选] CNY spbill_create_ip: IP } *@return array( trade_type JSAPI NATIVE APP prepay_id 预支付交易会话,2小时有效 code_url trade_type为NATIVE是有返回 ) | false */ public function sendOrder(array $data) { static $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder'; $data += array('appid' => $this->getConfig('appid'), 'mch_id' => $this->getConfig('mch_id'), 'device_info' => 'WEB', 'nonce_str' => md5(microtime(true)), 'fee_type' => 'CNY', 'spbill_create_ip' => $this->getConfig('client_ip'), 'trade_type' => 'JSAPI'); $data = Utility::makeSign($data, $this->getConfig('pay_key')); $xml = Utility::buildXML($data); $body = Utility::http($url, $xml); try { $obj = @simplexml_load_string($body, 'SimpleXMLElement', LIBXML_NOCDATA | LIBXML_NOBLANKS); $res = Utility::extractXML($obj); if ($res['return_code'] == 'SUCCESS' && $res['result_code'] == 'SUCCESS') { return $res; } else { logger()->error('sendOrder(Error): ' . $res['return_msg'] . '|' . $res['err_code_des']); return false; } } catch (Exception $e) { logger()->error('sendOrder(Exception): ' . $e->getMessage()); return false; } }