Notify.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace app\api\controller\v1;
  3. use EasyWeChat\Factory;
  4. use think\Controller;
  5. use think\Db;
  6. use think\Exception;
  7. class Notify extends Controller {
  8. public function WxOrder(){
  9. $options = config('app.wx_mini_config');
  10. $app = Factory::payment($options);
  11. $response = $app->handlePaidNotify(function ($message, $fail) {
  12. if ($message['return_code'] === 'SUCCESS' && $message['result_code'] === 'SUCCESS') { // return_code 表示通信状态,不代表支付状态
  13. // 用户是否支付成功
  14. $tradeNo = $message['out_trade_no'];
  15. // 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单
  16. $order = Db::name('wx_orders')->where('order_sn',$tradeNo)->find();
  17. if (!$order || $order['status'] != 0) { // 如果订单不存在 或者 订单已经支付过了
  18. return true; // 告诉微信,我已经处理完了,订单没找到,别再通知我了
  19. }
  20. $ret = Db::name('wx_orders')->where('id',$order['id'])->update(['status'=>1,'pay_time'=>date('Y-m-d H:i:s')]);
  21. $goods = Db::name('wx_orders_goods')->where('order_id',$order['id'])->select();
  22. //增加销量
  23. $is = false;
  24. foreach ($goods as $k=>$v){
  25. Db::name('wx_goods')
  26. ->where('id',$v['goods_id'])
  27. ->inc('sale',$v['nums'])
  28. ->update();
  29. $gInfo = Db::name('wx_goods')
  30. ->where('id',$v['goods_id'])
  31. ->find();
  32. $cateInfo = Db::name('wx_goods_cate')
  33. ->where('id',$gInfo['cate_id'])
  34. ->find();
  35. if($cateInfo['is_water']==1){
  36. $is = true;
  37. }
  38. }
  39. if($is){
  40. model('Orders')->waterSend($order['org_id']);
  41. }
  42. if(!$ret){
  43. trace('订单修改失败');
  44. $fail('订单修改失败');
  45. }
  46. } else {
  47. return $fail('通信失败,请稍后再通知我');
  48. }
  49. return true; // 返回处理完成
  50. });
  51. $response->send();
  52. }
  53. public function cashOrder(){
  54. $options = config('app.wx_mini_config');
  55. $app = Factory::payment($options);
  56. $response = $app->handlePaidNotify(function ($message, $fail) {
  57. if ($message['return_code'] === 'SUCCESS' && $message['result_code'] === 'SUCCESS') { // return_code 表示通信状态,不代表支付状态
  58. // 用户是否支付成功
  59. $tradeNo = $message['out_trade_no'];
  60. // 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单
  61. $order = Db::name('wx_cash_pay_log')->where('sn',$tradeNo)->find();
  62. if (!$order || $order['status'] != 0) { // 如果订单不存在 或者 订单已经支付过了
  63. return true; // 告诉微信,我已经处理完了,订单没找到,别再通知我了
  64. }
  65. Db::startTrans();
  66. try{
  67. $ret = Db::name('wx_cash_pay_log')
  68. ->where('id',$order['id'])
  69. ->update(['status'=>1,'pay_time'=>date('Y-m-d H:i:s')]);
  70. if(!$ret){
  71. \exception('操作失败');
  72. }
  73. /* $old = Db::name('wx_cash')
  74. ->where('barrel_id',$order['barrel_id'])
  75. ->where('user_id',$order['user_id'])
  76. ->where('del',0)
  77. ->find();
  78. if($old){
  79. $r = Db::name('wx_cash')
  80. ->where('id',$old['id'])
  81. ->setInc('num',$order['num']);
  82. if(!$r){
  83. \exception('操作失败');
  84. }
  85. }else{
  86. $barrel = Db::name('wx_barrel')
  87. ->where('id',$order['barrel_id'])
  88. ->find();
  89. $params = [
  90. 'sn'=>get_unique_id(),
  91. 'user_id'=>$order['user_id'],
  92. 'barrel_id'=>$order['barrel_id'],
  93. 'org_id'=>$order['org_id'],
  94. 'num'=>$order['num'],
  95. 'amount'=>$order['amount'],
  96. 'price'=>$barrel['cash_price'],
  97. 'create_time'=>getTime()
  98. ];
  99. $res = Db::name('wx_cash')
  100. ->insertGetId($params);
  101. if(!$res){
  102. \exception('操作失败');
  103. }
  104. }*/
  105. $barrel = Db::name('wx_barrel')
  106. ->where('id',$order['barrel_id'])
  107. ->find();
  108. $params = [
  109. 'sn'=>$order['sn'],
  110. 'user_id'=>$order['user_id'],
  111. 'barrel_id'=>$order['barrel_id'],
  112. 'org_id'=>$order['org_id'],
  113. 'num'=>$order['num'],
  114. 'amount'=>$order['amount'],
  115. 'total_money'=>$order['amount'],
  116. 'price'=>$barrel['cash_price'],
  117. 'create_time'=>getTime()
  118. ];
  119. $res = Db::name('wx_cash')
  120. ->insertGetId($params);
  121. if(!$res){
  122. \exception('操作失败');
  123. }
  124. Db::commit();
  125. return true;
  126. }catch (Exception $e){
  127. Db::rollback();
  128. trace($e->getMessage());
  129. $fail($e->getMessage());
  130. }
  131. } else {
  132. return $fail('通信失败,请稍后再通知我');
  133. }
  134. return true; // 返回处理完成
  135. });
  136. $response->send();
  137. }
  138. }