config = $config; // 工厂方法构造一个实例 $this->client = Builder::factory([ 'mchid' => $this->config["MCHID"], 'serial' => 'nop', 'privateKey' => 'any', 'certs' => ['any' => null], 'secret' => $this->config["ApiV2Key"], 'merchant' => [ 'cert' => $this->config["ApiclientCert"], 'key' => $this->config["ApiclientKey"], ], ]); } public function wxNotify(){ var_dump(Transformer::toArray($request->getInput())); } public function wxmpPay($post=[]){ $apiUrl = "v2/pay/unifiedorder"; //sign_type默认MD5不用传 $params = [ 'appid' => $this->config["APPID"], // 小程序ID/微信开放平台审核通过的应用APPID 'mch_id' => $this->config["MCHID"], // 商户号 'nonce_str' => Formatter::nonce(), // 32位随机字符串 'body' => empty($post["body"]) ? "微信小程序支付" : $post["body"], 'attach' => empty($post["attach"])? "微信小程序支付" : $post["attach"], 'out_trade_no' => $post["out_trade_no"], // 商户订单号 'total_fee' => (int)(floatval($post["total"])*100), // 订单总金额,单位分 'spbill_create_ip' => empty($post["payer_client_ip"]) ? "127.0.0.1" : $post["payer_client_ip"], // 终端ip 'time_expire' => date("YmdHis",time()+30*60),//交易结束时间2018-06-08T10:34:56+08:00 'notify_url' => $this->config["NOTIFY_URL"], // 通知地址 'trade_type' => "JSAPI", // 交易类型 'openid' => $post["openid"], // 'signType' => Hash::ALGO_MD5, ]; // $params["sign"] = self::getSign($params); $result = $this->clientHttp("POST", $apiUrl, $params); if(empty($result)){ if(empty($this->errorMsg)){ $this->errorMsg = "支付错误001"; } return false; } $resuleAr = Transformer::toArray($result); if(empty($resuleAr)){ if(empty($this->errorMsg)){ $this->errorMsg = "支付错误002"; } return false; } if(empty($resuleAr["prepay_id"])){ if(empty($this->errorMsg)){ $this->errorMsg = "支付错误003"; } return false; } //组装支付参数 $payInfo=array(); $data=$this->v2makeSign(["appId"=>$this->config["APPID"],"prepay_id"=>$resuleAr["prepay_id"]]); $data["payData"] = $params; return $data; } public function v2makeSign($info){ $params = [ 'appId' => $info["appId"], 'timeStamp' => (string)Formatter::timestamp(), 'nonceStr' => Formatter::nonce(), 'package' => 'prepay_id='.$info["prepay_id"], ]; $params['paySign'] = Hash::sign( Hash::ALGO_MD5,//默认md5 Formatter::queryStringLike(Formatter::ksort($params)), $this->config["ApiV2Key"], ); $params['signType'] = Hash::ALGO_MD5; return $params; } /** * 查询订单 * @param type $out_trade_no 商户订单号 */ public function searchOrder($out_trade_no){ $apiUrl = "v3/pay/transactions/out-trade-no/{out_trade_no}"; $result = $this->clientHttp("GET", $apiUrl,[ "out_trade_no"=>$out_trade_no, "query"=>["mchid"=>$this->config["MCHID"]], ]); return $result; } /** * 关闭订单 * @param type $out_trade_no 商户订单号 */ public function closeOrder($out_trade_no){ $apiUrl = "v3/pay/transactions/out-trade-no/{out_trade_no}/close"; $result = $this->clientHttp("GET", $apiUrl,[ "out_trade_no"=>$out_trade_no, "query"=>["mchid"=>$this->config["MCHID"]], ]); return $result; } /** * 生成签名 * @param type $info * @return string */ private function makeSign($info){ $params = [ 'appId' => $info["appId"], 'timeStamp' => (string)Formatter::timestamp(), 'nonceStr' => Formatter::nonce(), 'package' => 'prepay_id='.$info["prepay_id"], ]; $params["paySign"] = Rsa::sign(Formatter::joinedByLineFeed(...array_values($params)),$this->merchantPrivateKeyInstance); $params["signType"] = 'RSA'; return $params; } /** * 解密回调参数 * @param type $data * @return type */ public function aesGcmDecrypt($data){ // 加密文本消息解密 $inBodyResource = AesGcm::decrypt($data["ciphertext"], $this->config["apiv3Key"], $data["nonce"], $data["associated_data"]); // 把解密后的文本转换为PHP Array数组 $inBodyResourceArray = (array)json_decode($inBodyResource, true); return $inBodyResourceArray; } /** * http提交[同步请求] * @param type $type * @param type $url 示例:v3/pay/transactions/native * @param type $json * @return boolean */ private function clientHttp($type='POST',$url='',$json=[]){ try { $resp=null; if($type=="POST"){ $resp = $this->client->chain($url)->post(['xml' => $json]); if(empty($json)){ $resp = $this->client->chain($url)->post(); }else{ $resp = $this->client->chain($url)->post(['xml' => $json]); } } if($type=="GET"){ if(empty($json)){ $resp = $this->client->chain($url)->get(); }else{ $resp = $this->client->chain($url)->get($json); } } if(empty($resp)){ $this->errorMsg="提交方式错误"; return false; } $statusCode = $resp->getStatusCode(); if ($statusCode == 200) { //处理成功 return $resp->getBody()->getContents(); } else if ($statusCode == 204) { //处理成功,无返回Body $this->errorMsg = "处理成功,无返回Body"; return false; }else{ $this->errorMsg = "未知错误"; return false; } } catch (\Exception $e) { // var_dump($e->getMessage()); // 进行错误处理 $this->errorMsg = $e->getMessage(); if ($e instanceof \GuzzleHttp\Exception\RequestException && $e->hasResponse()) { $r = $e->getResponse(); $this->errorMsg=$r->getStatusCode()."".$r->getReasonPhrase().$r->getBody(); } return false; } } //回调验签 public function cesiCheckSign(){ $inBody = '';// 请根据实际情况获取,例如: file_get_contents('php://input'); $apiv2Key = '';// 在商户平台上设置的APIv2密钥 $inBodyArray = Transformer::toArray($inBody); // 部分通知体无`sign_type`,部分`sign_type`默认为`MD5`,部分`sign_type`默认为`HMAC-SHA256` // 部分通知无`sign`字典 // 请根据官方开发文档确定 ['sign_type' => $signType, 'sign' => $sign] = $inBodyArray; $calculated = Hash::sign( $signType ?? Hash::ALGO_MD5,// 如没获取到`sign_type`,假定默认为`MD5` Formatter::queryStringLike(Formatter::ksort($inBodyArray)), $apiv2Key ); $signatureStatus = Hash::equals($calculated, $sign); if ($signatureStatus) { // 如需要解密的 ['req_info' => $reqInfo] = $inBodyArray; $inBodyReqInfoXml = AesEcb::decrypt($reqInfo, Hash::md5($apiv2Key)); $inBodyReqInfoArray = Transformer::toArray($inBodyReqInfoXml); // print_r($inBodyReqInfoArray);// 打印解密后的结果 } } }