<?php namespace app\common\util; use Omnipay\Omnipay; use app\common\model\OrderInfo as OrderInfoModel; use app\common\model\OrderAction as OrderActionModel; class WeChatPay { protected $appId; protected $mchId; protected $apiKey; protected $notifyUrl;//異步通知 public function __construct() { $this->appId = config('wechatpay.wechat_appid'); $this->mchId = config('wechatpay.wechat_mch_id'); $this->apiKey = config('wechatpay.wechat_appkey'); $this->notifyUrl = config('wechatpay.wechat_callback'); } public function App($content){ $gateway = $this->getGateway(); $request = $gateway->purchase($content); $response = $request->send(); //available methods if(!$response->isSuccessful()){ $ret = $response->getData();//For debug throw new \Exception($ret['err_code_des']); } $arr = $response->getAppOrderData(); //For WechatPay_App return $arr; // $response->getJsOrderData(); //For WechatPay_Js // $response->getCodeUrl(); //For Native Trade Type } public function notify(){ $gateway = $this->getGateway(); $xml = file_get_contents('php://input', 'r'); $request = $gateway->completePurchase([ 'request_params' => $xml ]); $arr = $this->xml2Array($xml); file_put_contents(__DIR__.'wxpay.txt', var_export($arr, true), FILE_APPEND); $where = []; $where['code'] = $arr['out_trade_no']; $orderInfoModel = new OrderInfoModel(); $orderInfo = $orderInfoModel->verify($where); if($orderInfo['pay_status'] == 2){ return ; } $orderActionModel = new OrderActionModel(); $actionData = []; $actionData['order_id'] = $orderInfo['id']; $actionData['order_code'] = $orderInfo['code']; $actionData['order_status'] = $orderInfo['order_status']; $actionData['shipping_status'] = $orderInfo['shipping_status']; $actionData['log_time'] = time(); try { $response = $request->send(); if ($response->isPaid()) { //pay success $data = []; $data['pay_status'] = 2;//已付款 $data['composition_status'] = 3;//待發貨 $data['online_money'] = $arr['cash_fee'];//線上付款金額 $data['pay_time'] = time();//付款時間 $res = $orderInfoModel::update($data, ['id' => $orderInfo['id']]); $actionData['pay_status'] = 2;//已付款 if($res === false){ $actionData['action_note'] = '微信支付成功,更改訂單狀態失敗'; }else{ $actionData['action_note'] = '微信支付成功,更改訂單狀態成功'; } }else{ //pay fail $actionData['pay_status'] = 1;//付款中 $actionData['action_note'] = '微信支付失敗'; $orderActionModel->create($actionData); } } catch (\Exception $e) { $actionData['pay_status'] = 1;//付款中 $actionData['action_note'] = $e->getMessage(); $orderActionModel->create($actionData); } } private function getGateway(){ $gateway = Omnipay::create('WechatPay_App'); $gateway->setAppId($this->appId); $gateway->setMchId($this->mchId); $gateway->setApiKey($this->apiKey); $gateway->setNotifyUrl($this->notifyUrl); return $gateway; } /** * 將xml轉爲array * @param string $xml xml字符串 * @return array 轉換獲得的數組 */ public function xml2Array($xml) { //禁止引用外部xml實體 libxml_disable_entity_loader(true); $result = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true); return $result; } }