123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- <?php
- namespace app\api\controller\v1;
- use EasyWeChat\Factory;
- use think\Controller;
- use think\Db;
- use think\Exception;
- class Notify extends Controller {
-
- public function WxOrder(){
- $options = config('app.wx_mini_config');
- $app = Factory::payment($options);
- $response = $app->handlePaidNotify(function ($message, $fail) {
- if ($message['return_code'] === 'SUCCESS' && $message['result_code'] === 'SUCCESS') { // return_code 表示通信状态,不代表支付状态
- // 用户是否支付成功
- $tradeNo = $message['out_trade_no'];
- // 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单
- $order = Db::name('wx_orders')->where('order_sn',$tradeNo)->find();
- if (!$order || $order['status'] != 0) { // 如果订单不存在 或者 订单已经支付过了
- return true; // 告诉微信,我已经处理完了,订单没找到,别再通知我了
- }
- $ret = Db::name('wx_orders')->where('id',$order['id'])->update(['status'=>1,'pay_time'=>date('Y-m-d H:i:s')]);
- $goods = Db::name('wx_orders_goods')->where('order_id',$order['id'])->select();
- //增加销量
- $is = false;
- foreach ($goods as $k=>$v){
- Db::name('wx_goods')
- ->where('id',$v['goods_id'])
- ->inc('sale',$v['nums'])
- ->update();
- $gInfo = Db::name('wx_goods')
- ->where('id',$v['goods_id'])
- ->find();
- $cateInfo = Db::name('wx_goods_cate')
- ->where('id',$gInfo['cate_id'])
- ->find();
- if($cateInfo['is_water']==1){
- $is = true;
- }
- }
- if($is){
- model('Orders')->waterSend($order['org_id']);
- }
- if(!$ret){
- trace('订单修改失败');
- $fail('订单修改失败');
- }
- } else {
- return $fail('通信失败,请稍后再通知我');
- }
- return true; // 返回处理完成
- });
- $response->send();
- }
- public function cashOrder(){
- $options = config('app.wx_mini_config');
- $app = Factory::payment($options);
- $response = $app->handlePaidNotify(function ($message, $fail) {
- if ($message['return_code'] === 'SUCCESS' && $message['result_code'] === 'SUCCESS') { // return_code 表示通信状态,不代表支付状态
- // 用户是否支付成功
- $tradeNo = $message['out_trade_no'];
- // 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单
- $order = Db::name('wx_cash_pay_log')->where('sn',$tradeNo)->find();
- if (!$order || $order['status'] != 0) { // 如果订单不存在 或者 订单已经支付过了
- return true; // 告诉微信,我已经处理完了,订单没找到,别再通知我了
- }
- Db::startTrans();
- try{
- $ret = Db::name('wx_cash_pay_log')
- ->where('id',$order['id'])
- ->update(['status'=>1,'pay_time'=>date('Y-m-d H:i:s')]);
- if(!$ret){
- \exception('操作失败');
- }
- /* $old = Db::name('wx_cash')
- ->where('barrel_id',$order['barrel_id'])
- ->where('user_id',$order['user_id'])
- ->where('del',0)
- ->find();
- if($old){
- $r = Db::name('wx_cash')
- ->where('id',$old['id'])
- ->setInc('num',$order['num']);
- if(!$r){
- \exception('操作失败');
- }
- }else{
- $barrel = Db::name('wx_barrel')
- ->where('id',$order['barrel_id'])
- ->find();
- $params = [
- 'sn'=>get_unique_id(),
- 'user_id'=>$order['user_id'],
- 'barrel_id'=>$order['barrel_id'],
- 'org_id'=>$order['org_id'],
- 'num'=>$order['num'],
- 'amount'=>$order['amount'],
- 'price'=>$barrel['cash_price'],
- 'create_time'=>getTime()
- ];
- $res = Db::name('wx_cash')
- ->insertGetId($params);
- if(!$res){
- \exception('操作失败');
- }
- }*/
- $barrel = Db::name('wx_barrel')
- ->where('id',$order['barrel_id'])
- ->find();
- $params = [
- 'sn'=>$order['sn'],
- 'user_id'=>$order['user_id'],
- 'barrel_id'=>$order['barrel_id'],
- 'org_id'=>$order['org_id'],
- 'num'=>$order['num'],
- 'amount'=>$order['amount'],
- 'total_money'=>$order['amount'],
- 'price'=>$barrel['cash_price'],
- 'create_time'=>getTime()
- ];
- $res = Db::name('wx_cash')
- ->insertGetId($params);
- if(!$res){
- \exception('操作失败');
- }
- Db::commit();
- return true;
- }catch (Exception $e){
- Db::rollback();
- trace($e->getMessage());
- $fail($e->getMessage());
- }
- } else {
- return $fail('通信失败,请稍后再通知我');
- }
- return true; // 返回处理完成
- });
- $response->send();
- }
- }
|