PayNotifyCallBack.class.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. <?php
  2. require_once dirname(__FILE__) ."/lib/WxPay.Api.php";
  3. require_once dirname(__FILE__) .'/lib/WxPay.Notify.php';
  4. require_once dirname(__FILE__) .'/log.php';
  5. $lib_path = dirname(dirname( dirname(__FILE__) )).'/Lib/';
  6. $data_path = dirname( dirname(dirname( dirname(__FILE__) )) ).'/Data/wxpaylogs/'.date('Y-m-d')."/";
  7. RecursiveMkdir($data_path);
  8. //初始化日志
  9. //\Think\Log::record("begin notify222");
  10. class PayNotifyCallBack extends WxPayNotify
  11. {
  12. //查询订单
  13. public function Queryorder($transaction_id)
  14. {
  15. global $INI;
  16. $input = new WxPayOrderQuery();
  17. $input->SetTransaction_id($transaction_id);
  18. $result = WxPayApi::orderQuery($input);
  19. if(array_key_exists("return_code", $result)
  20. && array_key_exists("result_code", $result)
  21. && $result["return_code"] == "SUCCESS"
  22. && $result["result_code"] == "SUCCESS")
  23. {
  24. //DO
  25. return true;
  26. }
  27. return false;
  28. }
  29. //重写回调处理函数
  30. public function NotifyProcess($data, &$msg)
  31. {
  32. global $_W;
  33. global $_GPC;
  34. //global $_W;
  35. $out_trade_no_str = $data['out_trade_no'];
  36. $out_trade_no_arr = explode('-',$out_trade_no_str);
  37. $out_trade_no = $out_trade_no_arr[0];
  38. $data_path = dirname( dirname(dirname( dirname(__FILE__) )) ).'/Data/wxpaylogs/'.date('Y-m-d')."/";
  39. RecursiveMkdir($data_path);
  40. $file = $data_path.date('Y-m-d').'.txt';
  41. $handl = fopen($file,'a');
  42. fwrite($handl,"Queryorder");
  43. fwrite($handl,"call back:" . json_encode($data));
  44. fwrite($handl,"小程序开始查询支付:");
  45. fclose($handl);
  46. $notfiyOutput = array();
  47. //\Think\Log::record("call back:" . json_encode($data));
  48. if(!array_key_exists("transaction_id", $data)){
  49. $msg = "输入参数不正确";
  50. return false;
  51. }
  52. //查询订单,判断订单真实性
  53. if( false && !$this->Queryorder($data["transaction_id"])){
  54. $msg = "订单查询失败";
  55. return false;
  56. }else {
  57. /**
  58. {
  59. "appid":"wx334ca53b2a62661a",
  60. "bank_type":"CFT",
  61. "cash_fee":"1",
  62. "fee_type":"CNY",
  63. "is_subscribe":"N",
  64. "mch_id":"1246637501",
  65. "nonce_str":"eKWYmZBlPgeRUoeyNpOAFuBXvXWVofsD",
  66. "openid":"o_57D5DcRw-r6SdRxF98ikhf5dLY",
  67. "out_trade_no":"7-1540693614",
  68. "result_code":"SUCCESS",
  69. "return_code":"SUCCESS",
  70. "sign":"2B480C75338FCDE3972DF21AC7CC7596",
  71. "time_end":"20181028102711",
  72. "total_fee":"1",
  73. "trade_type":"JSAPI",
  74. "transaction_id":"4200000236201810287313447913"
  75. }
  76. **/
  77. $total_fee = $data['total_fee'];
  78. $transaction_id = $data['transaction_id'];
  79. $out_trade_no_arr = explode('-',$data['out_trade_no']);
  80. $out_trade_no = $out_trade_no_arr[0];
  81. if( isset($out_trade_no_arr[2]) && $out_trade_no_arr[2] == 'charge' )
  82. {
  83. //暂时屏蔽会员充值代码
  84. $member_charge_flow_info = M('lionfish_comshop_member_charge_flow')->where( array('id' => $out_trade_no ) )->find();
  85. if(!empty($member_charge_flow_info) && $member_charge_flow_info['state'] == 0)
  86. {
  87. $charge_flow_data = array();
  88. $charge_flow_data['trans_id'] = $transaction_id;
  89. $charge_flow_data['state'] = 1;
  90. $charge_flow_data['charge_time'] = time();
  91. M('lionfish_comshop_member_charge_flow')->where( array('id' => $out_trade_no) )->save( $charge_flow_data );
  92. if( !empty($member_charge_flow_info['give_money']) && $member_charge_flow_info['give_money'] > 0 )
  93. {
  94. $member_charge_flow_info['money'] += $member_charge_flow_info['give_money'];
  95. }
  96. M('lionfish_comshop_member')->where( array('member_id' => $member_charge_flow_info['member_id'] ) )->setInc('account_money', $member_charge_flow_info['money'] );
  97. $mb_info = M('lionfish_comshop_member')->field('account_money')->where( array('member_id' => $member_charge_flow_info['member_id']) )->find();
  98. if( empty($mb_info['account_money']) )
  99. {
  100. $mb_info['account_money'] = 0;
  101. }
  102. M('lionfish_comshop_member_charge_flow')->where( array('id' => $out_trade_no) )->save( array('operate_end_yuer' => $mb_info['account_money']) );
  103. $mb_info = M('lionfish_comshop_member')->field('account_money')->where( array('member_id' => $member_charge_flow_info['member_id']) )->find();
  104. M('lionfish_comshop_member_charge_flow')->where( array('id' => $out_trade_no) )->save( array('operate_end_yuer' => $mb_info['account_money']) );
  105. for($i=0;$i<3;$i++)
  106. {
  107. $member_formid_data = array();
  108. $member_formid_data['member_id'] = $member_charge_flow_info['member_id'];
  109. $member_formid_data['state'] = 0;
  110. $member_formid_data['formid'] = $member_charge_flow_info['formid'];
  111. $member_formid_data['addtime'] = time();
  112. M('lionfish_comshop_member_formid')->add($member_formid_data);
  113. }
  114. }
  115. }
  116. else if( isset($out_trade_no_arr[2]) && $out_trade_no_arr[2] == 'buycard' )
  117. {
  118. //购买会员卡代码
  119. $member_charge_flow_info = M('lionfish_comshop_member_card_order')->where( array('id' => $out_trade_no ) )->find();
  120. $member_info = M('lionfish_comshop_member')->where( array('member_id' => $member_charge_flow_info['member_id'] ) )->find();
  121. if(!empty($member_charge_flow_info) && $member_charge_flow_info['state'] == 0)
  122. {
  123. $begin_time = 0;
  124. $end_time = 0;
  125. if($member_charge_flow_info['order_type'] == 1)
  126. {
  127. //首次购买
  128. $begin_time = time();
  129. $end_time = $begin_time + 86400 * $member_charge_flow_info['expire_day'];
  130. }else if($member_charge_flow_info['order_type'] == 2)
  131. {
  132. //有效期内续期
  133. $begin_time = $member_info['card_end_time'];
  134. $end_time = $begin_time + 86400 * $member_charge_flow_info['expire_day'];
  135. }else if($member_charge_flow_info['order_type'] == 3)
  136. {
  137. //过期后续费
  138. $begin_time = time();
  139. $end_time = $begin_time + 86400 * $member_charge_flow_info['expire_day'];
  140. }
  141. $charge_flow_data = array();
  142. $charge_flow_data['trans_id'] = $transaction_id;
  143. $charge_flow_data['state'] = 1;
  144. $charge_flow_data['pay_time'] = time();
  145. $charge_flow_data['begin_time'] = $begin_time;
  146. $charge_flow_data['end_time'] = $end_time;
  147. $charge_flow_data['state'] = 1;
  148. M('lionfish_comshop_member_card_order')->where( array('id' => $out_trade_no ) )->save( $charge_flow_data );
  149. $mb_up_data = array();
  150. $mb_up_data['card_id'] = $member_charge_flow_info['car_id'];
  151. $mb_up_data['card_begin_time'] = $begin_time;
  152. $mb_up_data['card_end_time'] = $end_time;
  153. M('lionfish_comshop_member')->where( array('member_id' => $member_charge_flow_info['member_id'] ) )->save( $mb_up_data );
  154. for($i=0;$i<3;$i++)
  155. {
  156. $member_formid_data = array();
  157. $member_formid_data['member_id'] = $member_charge_flow_info['member_id'];
  158. $member_formid_data['state'] = 0;
  159. $member_formid_data['formid'] = $member_charge_flow_info['formid'];
  160. $member_formid_data['addtime'] = time();
  161. M('lionfish_comshop_member_formid')->add($member_formid_data);
  162. }
  163. }
  164. }
  165. else{
  166. //
  167. $order_all = M('lionfish_comshop_order_all')->where( array('id' => $out_trade_no ) )->find();
  168. if( in_array($order_all['order_status_id'], array(1,2,15)) ){
  169. $stop_pay = false;
  170. $order_relate_list = M('lionfish_comshop_order_relate')->where( array('order_all_id' => $order_all['id'] ) )->select();
  171. foreach($order_relate_list as $order_relate)
  172. {
  173. $order = M('lionfish_comshop_order')->where( array('order_id' => $order_relate['order_id'] ) )->find();
  174. if( $order && ($order['order_status_id'] != 3 && $order['order_status_id'] == 5) )
  175. {
  176. $stop_pay = true;
  177. }
  178. }
  179. if( $stop_pay )
  180. {
  181. $msg = "付款成功";
  182. return true;
  183. }
  184. }
  185. $o = array();
  186. $o['order_status_id'] = $order_all['is_pin'] == 1 ? 2:1;
  187. $o['paytime']=time();
  188. $o['transaction_id'] = $transaction_id;
  189. M('lionfish_comshop_order_all')->where( array('id' => $out_trade_no) )->save($o);
  190. $order_relate_list = M('lionfish_comshop_order_relate')->where( array('order_all_id' => $order_all['id']) )->select();
  191. //1
  192. $data_path = dirname( dirname(dirname( dirname(__FILE__) )) ).'/Data/wxpaylogs/'.date('Y-m-d')."/";
  193. RecursiveMkdir($data_path);
  194. $file = $data_path.date('Y-m-d').'.txt';
  195. $handl = fopen($file,'a');
  196. fwrite($handl,"关联--");
  197. fwrite($handl,"关联");
  198. fwrite($handl,":".json_encode($order_relate_list));
  199. fclose($handl);
  200. foreach($order_relate_list as $order_relate)
  201. {
  202. $order = M('lionfish_comshop_order')->where( array('order_id' => $order_relate['order_id'] ) )->find();
  203. //检测是否预售
  204. $order_presale_info = M('lionfish_comshop_order_presale')->where(['order_id' => $order_relate['order_id'] ])->find();
  205. //检测是否礼品卡
  206. $order_virtualcard_info = D('Seller/VirtualCard')->getOrderVirtualCardByOrderId( $order_relate['order_id'] );
  207. if( $order && ($order['order_status_id'] == 3 || $order['order_status_id'] == 5 || $order['order_status_id'] == 15 ) )
  208. {
  209. //begin 判断预售第一次还是第二次付款
  210. $order_status_id = $order['is_pin'] == 1 ? 2:1;
  211. if( !empty($order_presale_info) )
  212. {
  213. if( $order_presale_info['state'] == 0 )
  214. {
  215. $order_status_id = 15;//首次付款
  216. }else if( $order_presale_info['state'] == 1 )
  217. {
  218. $order_status_id = 1;//二次付款
  219. }
  220. }
  221. //end
  222. $o = array();
  223. $o['order_status_id'] = $order_status_id;
  224. $o['date_modified']=time();
  225. $o['pay_time']=time();
  226. $o['payment_code']='weixin';
  227. $o['transaction_id'] = $transaction_id;
  228. if( empty($order_presale_info) || $order_presale_info['state'] == 1 )
  229. {
  230. if($order['delivery'] == 'hexiao' ){//核销订单 支付完成状态改成 已发货待收货
  231. $o['order_status_id'] = 4;
  232. }
  233. }
  234. M('lionfish_comshop_order')->where( array('order_id' => $order['order_id'] ) )->save( $o );
  235. //暂时屏蔽库存代码
  236. $kucun_method = D('Home/Front')->get_config_by_name('kucun_method');
  237. if( empty($kucun_method) )
  238. {
  239. $kucun_method = 0;
  240. }
  241. //kucun_method $_W['uniacid']
  242. if($kucun_method == 1)
  243. {//支付完减库存,增加销量
  244. $order_goods_list = M('lionfish_comshop_order_goods')->where( array('order_id' => $order['order_id']) )->select();
  245. foreach($order_goods_list as $order_goods)
  246. {
  247. D('Home/Pingoods')->del_goods_mult_option_quantity($order['order_id'],$order_goods['rela_goodsoption_valueid'],$order_goods['goods_id'],$order_goods['quantity'],1);
  248. }
  249. }
  250. $oh = array();
  251. $oh['order_id']=$order['order_id'];
  252. $oh['uniacid']=$order['uniacid'];
  253. $oh['order_status_id']= $order_status_id;
  254. $oh['comment']='买家已付款';
  255. $oh['date_added']=time();
  256. $oh['notify']=1;
  257. if($order['delivery'] == 'hexiao' && empty($order_presale_info) ){//核销订单 支付完成状态改成 已发货待收货
  258. $oh['order_status_id'] = 4;
  259. }
  260. M('lionfish_comshop_order_history')->add($oh);
  261. //邀新有礼下单完成领取礼包
  262. D('Home/Invitegift')->collectInvitegiftAfterOrder($order, 'orderpay');
  263. if( empty($order_presale_info) )
  264. {
  265. //订单自动配送
  266. D('Home/Order')->order_auto_delivery($order);
  267. }
  268. //$weixin_nofity->orderBuy($order['order_id']);
  269. //$hashids = new \Lib\Hashids(C('PWD_KEY'), C('URL_ID'));
  270. //$order_id = $hashids->encode($order['order_id']);
  271. //group_order_id
  272. if($order['is_pin'] == 1)
  273. {
  274. $pin_order = M('lionfish_comshop_pin_order')->where( array('order_id' => $order['order_id'] ) )->find();
  275. D('Home/Pin')->insertNotifyOrder($order['order_id']);
  276. //检测拼团是否已经成功了。如果已经成功了。那么重新开团,并且迁移掉目前的这个拼团订单到新的团去
  277. //state
  278. $pin_info = M('lionfish_comshop_pin')->where(array('pin_id' => $pin_order['pin_id']) )->find();//加锁查询
  279. $pin_buy_count = D('Home/Pin')->get_tuan_buy_count($pin_order['pin_id']);
  280. $res = D('Seller/Redisorder')->add_pintuan_user( $pin_order['pin_id'] );
  281. if( $pin_info['state'] == 1 && !$res )
  282. {
  283. $order_goods_info = M('lionfish_comshop_order_goods')->where( array('order_id' => $order['order_id']) )->find();
  284. M('lionfish_comshop_pin_order')->where( array('pin_id' =>$pin_order['pin_id'],'order_id' => $order['order_id'] ) )->delete();
  285. $newpin_id = D('Home/Pin')->openNewTuan($order['order_id'],$order_goods_info['goods_id'],$order['member_id']);
  286. //插入拼团订单
  287. D('Home/Pin')->insertTuanOrder($newpin_id,$order['order_id']);
  288. unset($pin_info);
  289. $is_pin_success = D('Home/Pin')->checkPinSuccess($newpin_id);
  290. if($is_pin_success) {
  291. D('Home/Pin')->updatePintuanSuccess($newpin_id);
  292. }
  293. }else{
  294. $is_pin_success = D('Home/Pin')->checkPinSuccess($pin_order['pin_id']);
  295. if($is_pin_success) {
  296. D('Home/Pin')->updatePintuanSuccess($pin_order['pin_id']);
  297. }
  298. }
  299. }
  300. //检测预售支付
  301. if( !empty($order_presale_info) )
  302. {
  303. D('Home/PresaleGoods')->payBackOrder( $order['order_id'] , $transaction_id );
  304. }
  305. //检测是否礼品卡
  306. if( !empty($order_virtualcard_info) )
  307. {
  308. D('Seller/VirtualCard')->payBackOrder( $order['order_id'] );
  309. }
  310. //发送购买通知
  311. D('Home/Weixinnotify')->orderBuy($order['order_id']);
  312. //微信交易组件
  313. if( $order['from_type'] == 'tradecomponts' )
  314. {
  315. D('Seller/MpModifyTradeComponts')->orderPay( $order['order_id'], '1' );
  316. }
  317. }
  318. }
  319. }
  320. //$order=M('Order')->getByOrderNumAlias($out_trade_no);
  321. return true;
  322. }
  323. }
  324. }