Notify.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | framework
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2018 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://framework.thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | github开源项目:https://github.com/zoujingli/framework
  12. // +----------------------------------------------------------------------
  13. namespace app\store\controller\api;
  14. use app\store\service\OrderService;
  15. use think\Db;
  16. /**
  17. * 支付通知处理
  18. * Class Notify
  19. * @package app\store\controller\api
  20. */
  21. class Notify
  22. {
  23. /**
  24. * 微信支付通知处理
  25. * @return string
  26. * @throws \WeChat\Exceptions\InvalidResponseException
  27. * @throws \think\Exception
  28. * @throws \think\exception\PDOException
  29. */
  30. public function wxpay()
  31. {
  32. $wechat = \We::WePayOrder(config('wechat.miniapp'));
  33. $notify = $wechat->getNotify();
  34. if ($notify['result_code'] == 'SUCCESS' && $notify['return_code'] == 'SUCCESS') {
  35. if ($this->update($notify['out_trade_no'], $notify['transaction_id'], $notify['cash_fee'] / 100, 'wechat')) {
  36. return $wechat->getNotifySuccessReply();
  37. }
  38. } else return $wechat->getNotifySuccessReply();
  39. }
  40. /**
  41. * 订单状态更新
  42. * @param string $order_no 订单号
  43. * @param string $pay_no 交易号
  44. * @param string $pay_price 交易金额
  45. * @param string $type 支付类型
  46. * @return boolean
  47. * @throws \think\Exception
  48. * @throws \think\exception\PDOException
  49. */
  50. private function update($order_no, $pay_no, $pay_price, $type = 'wechat')
  51. {
  52. // 检查订单支付状态
  53. $where = ['order_no' => $order_no, 'pay_state' => '0', 'status' => '2'];
  54. $order = Db::name('StoreOrder')->where($where)->find();
  55. if (empty($order)) return false;
  56. // 更新订单支付状态
  57. $result = Db::name('StoreOrder')->where($where)->update([
  58. 'pay_type' => $type, 'pay_no' => $pay_no, 'status' => '3',
  59. 'pay_price' => $pay_price, 'pay_state' => '1', 'pay_at' => date('Y-m-d H:i:s'),
  60. ]);
  61. // 调用会员升级机制
  62. OrderService::update($order['order_no']);
  63. return $result !== false;
  64. }
  65. }