【推荐理由】赠运费险
【优惠券】20 【券后价】59.00
【下单链接】18¥ CZ0001 G5fHdEqOsnB¥
这两天要小程序用到微信支付功能,应用thinkphp3.2的,在集成回调通知处理结果的时候,一直卡住了,不知道怎么写,在经过了查找N多资料后,终于皇天不负有心人,找到了解决方法,下面代码经测试通过,确信可用:
public function notify_url() { $filename = './Public/file00.txt'; file_put_contents($filename, date("Y-m-d H:i:s",NOW_TIME)); try { $filename = './Public/file01.txt'; file_put_contents($filename, 'file01'); /* $notify = new OrderNotifyCallBack(); $notify->Handle(false); */ Vendor("WxpayAPI_php_v3.lib.WxPay","",".Api.php"); Vendor("WxpayAPI_php_v3.lib.WxPay","",".Data.php"); Vendor("WxpayAPI_php_v3.example.log"); $handle=new \CLogFileHandler('./Public/wxlog.txt'); //$log=\Logwx::Init($handle); $xml = $GLOBALS['HTTP_RAW_POST_DATA'];//获取数据 //$log->INFO('xml:'.$xml); $filename = './Public/file02.txt'; file_put_contents($filename, $xml); $wxpay=new \WxPayApi(); $notify=new \WxPayNotifyReply(); $result=\WxPayResults::Init($xml);//获取数据并转换为数组 if ($result['return_code']=='SUCCESS' && $result['result_code']=='SUCCESS') { //此字段是通信标识,非交易标识,交易是否成功需要查看result_code来判断 ///-------------------------------------------------begin修改订单状态 $openid = $result["openid"]; $product_id = $result["out_trade_no"]; $tarnsactionId=$result["transaction_id"]; $res=true;//订单处理结果 ///-------------------------------------------------end 修改订单状态 //TODO:进行数据库操作的业务逻辑处理,假设其成功与否的数据为$res $filename = './Public/file03.txt'; file_put_contents($filename, "file03"); if ($res) { //$log->INFO('订单:'.$result['out_trade_no'].'支付成功'); $notify->SetReturn_code('SUCCESS'); $notify->SetReturn_msg('OK'); $notify->SetSign(); $filename = './Public/file04.txt'; file_put_contents($filename, "file04:SUCCESS"); } else { //$log->ERROR('微信支付失败'); $notify->SetReturn_code('FAIL'); $notify->SetReturn_msg('客户服务器错误'); $filename = './Public/file04.txt'; file_put_contents($filename, "file04:客户服务器错误"); } } else { //$log->ERROR('微信回调返回错误'); $notify->SetReturn_code('FAIL'); $notify->SetReturn_msg('微信支付失败'); $filename = './Public/file04.txt'; file_put_contents($filename, "file04:微信回调返回错误"); } //返回微信端 $wxpay->replyNotify($notify->ToXml()); $filename = './Public/file06.txt'; file_put_contents($filename, 'file06'); } catch(Exception $e) { //捕获异常 $filename = './Public/file07.txt'; file_put_contents($filename, 'Message: ' .$e->getMessage() ); } }
这里放弃了,微信支付sdk中的继承WxPayNotify通知类,直接调用
Vendor("WxpayAPI_php_v3.lib.WxPay","",".Api.php"); Vendor("WxpayAPI_php_v3.lib.WxPay","",".Data.php");
处理异步通知消息。