Notify.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Think.Admin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://think.ctolog.com
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | github开源项目:https://github.com/zoujingli/Think.Admin
  12. // +----------------------------------------------------------------------
  13. namespace app\wechat\controller;
  14. use think\Controller;
  15. use think\Db;
  16. use think\Log;
  17. /**
  18. * 微信支付通知处理控制器
  19. * Class Notify
  20. * @package app\wechat\controller
  21. * @author Anyon <zoujingli@qq.com>
  22. * @date 2017/04/05 14:02
  23. */
  24. class Notify extends Controller
  25. {
  26. public function index()
  27. {
  28. // 实例支付接口
  29. $pay = load_wechat('Pay');
  30. // 获取支付通知
  31. $notifyInfo = $pay->getNotify();
  32. // 支付通知数据获取失败
  33. if ($notifyInfo === false) {
  34. // 接口失败的处理
  35. Log::error("微信支付通知消息验证失败,{$pay->errCode}[{$pay->errCode}]");
  36. return $pay->errMsg;
  37. } else {
  38. //支付通知数据获取成功
  39. if ($notifyInfo['result_code'] == 'SUCCESS' && $notifyInfo['return_code'] == 'SUCCESS') {
  40. // 记录支付通知数据
  41. if (!Db::name('WechatPayNotify')->insert($notifyInfo)) {
  42. $pay->replyXml(['return_code' => 'ERROR', 'return_msg' => '系统记录微信通知时发生异常!']);
  43. }
  44. $prepayMap = ['out_trade_no' => $notifyInfo['out_trade_no']];
  45. $prepayData = Db::name('WechatPayPrepayid')->where($prepayMap)->find();
  46. if (empty($prepayData)) {
  47. $pay->replyXml(['return_code' => 'ERROR', 'return_msg' => '系统中未发现对应的预支付记录!']);
  48. }
  49. $prepayUpdateData = ['transaction_id' => $notifyInfo['transaction_id'], 'is_pay' => 1, 'pay_at' => date('Y-m-d H:i:s')];
  50. if (false === Db::name('WechatPayPrepayid')->where($prepayMap)->update($prepayUpdateData)) {
  51. $pay->replyXml(['return_code' => 'ERROR', 'return_msg' => '更新系统预支付记录失败!']);
  52. }
  53. // 支付状态完全成功,可以更新订单的支付状态了
  54. // @todo 这里去完成你的订单状态修改操作
  55. // 回复xml,replyXml方法是终态方法
  56. $pay->replyXml(['return_code' => 'SUCCESS', 'return_msg' => '系统业务处理成功!']);
  57. }
  58. }
  59. }
  60. }