PayNotifyCallBack.class.php.back 15 KB

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