config = Config::pull('wxpay'); $this->wxPayConfig = new WxPayConfig($this->config); } //统一下单 public function setWxpayOrderData($data) { $this->unifiedOrderObj = new \WxPayUnifiedOrder(); $this->unifiedOrderObj->SetBody($data['body']); $this->unifiedOrderObj->SetAttach($data['attach'] ?? 'test');//设置附加数据,在查询API和支付通知中原样返回,该字段主要用于商户携带订单的自定义数据 $this->unifiedOrderObj->SetOut_trade_no($data['out_trade_no']); $this->unifiedOrderObj->SetTotal_fee($data['total_fee']); $this->unifiedOrderObj->SetTime_start(date("YmdHis")); $this->unifiedOrderObj->SetTime_expire(date("YmdHis", time() + $data['time_expire'])); $this->unifiedOrderObj->SetNotify_url($this->config['NOTIFYURL']); $this->unifiedOrderObj->SetTrade_type($data['trade_type']); } //统一下单 public function unifiedOrder() { $res = WxPayApi::unifiedOrder($this->wxPayConfig, $this->unifiedOrderObj); if ($res['return_code'] == "SUCCESS" && $res['result_code'] == "SUCCESS") { //根据微信支付返回的结果进行二次签名 //二次签名剩余参数的补充 $result = array( "appid" => $this->config['WXAPPID'], "partnerid" => $this->config['WXMCHID'], "prepayid" => $res['prepay_id'], "noncestr" => strtoupper(md5(uniqid(rand(10000, 99999)))), "package" => "Sign=WXPay", "timestamp" => time() . "" ); $sign = self::getSign($result, $this->config['WXKEY'], $this->config['SIGNTYPE']); $result['sign'] = $sign; return $result; } else { throw new Exception('支付请求出现异常'); } } //二次签名 private static function getSign($arr, $appkey, $signType = 'md5') { ksort($arr); $strInfo = ''; foreach ($arr as $key => $val) { if ($val === '') { continue; } if ($strInfo) { $strInfo .= "&" . $key . "=" . $val; } else { $strInfo = $key . "=" . $val; } } $strInfo = trim($strInfo, "&"); if ($signType == 'md5') { return strtoupper(md5($strInfo . '&key=' . $appkey)); } elseif ($signType == 'sha1') { return sha1($strInfo . '&key=' . $appkey); } elseif ($signType == "HMAC-SHA256") { return hash_hmac("sha256", $strInfo . '&key=' . $appkey, $appkey); } } /** * @param $transaction_id 微信订单号 * @param $total_fee 订单总金额 * @param $refund_fee 退款金额,不超过订单总金额 * @return \wxpayNew\lib\成功时返回,其他抛异常 * @throws Exception * @throws \wxpayNew\lib\WxPayException */ public function refund($transaction_id,$total_fee,$refund_fee) { try { $wxPayFound = new \WxPayRefund(); $wxPayFound->SetTransaction_id($transaction_id); $wxPayFound->SetTotal_fee($total_fee); $wxPayFound->SetRefund_fee($refund_fee); $wxPayFound->SetOut_refund_no("sdkphp" . date("YmdHis")); $wxPayFound->SetOp_user_id($this->wxPayConfig->GetMerchantId()); $res=WxPayApi::refund($this->wxPayConfig,$wxPayFound); //返回结果处理 if(isset($res['return_code'])){ if($res['return_code']=='FAIL'){ throw new Exception("退款申请失败,原因:".$res['return_msg']); } if($res['return_code']=='SUCCESS'){ if($res['result_code']=='FAIL'){ throw new Exception("退款申请失败,原因:".$res['err_code_des']); } if($res['result_code']=='SUCCESS'){ return $res;//申请成功 } } } throw new Exception('退款申请失败,原因:未知'); }catch (Exception $e) { throw new Exception( $e->getMessage()); } } }