PaymentController.class.php 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  1. <?php
  2. /**
  3. * 小梦科技资源nanodreamtech.com
  4. *
  5. * ==========================================================================
  6. * @link https://www.nanodreamtech.com/
  7. * @copyright Copyright (c) 2015 liofis.com.
  8. * @license https://www.nanodreamtech.com/license.html License
  9. * ==========================================================================
  10. *
  11. * @author fish
  12. *
  13. */
  14. namespace Home\Controller;
  15. class PaymentController extends CommonController {
  16. protected function _initialize()
  17. {
  18. parent::_initialize();
  19. }
  20. //会员中心页面,去付款
  21. function confirm_pay(){
  22. if(I('token')!=md5(session('pay_token'))){
  23. $url=U('/checkout');
  24. @header("Location: ".$url);
  25. die();
  26. }
  27. $order=M('order')->where(array('order_id'=>get_url_id('id')))->find();
  28. $data['notify_url']=C('SITE_URL').U('Payment/alipay_notify');
  29. $data['return_url']=C('SITE_URL').U('Payment/alipay_return');
  30. $data['order_type']='goods_buy';
  31. $data['subject']='购买商品';
  32. $data['name']=$order['shipping_name'];
  33. $data['pay_order_no']=$order['order_num_alias'];
  34. $data['pay_total']=(float)$order['total'];
  35. storage_user_action(session('user_auth.uid'),session('user_auth.username'),C('FRONTEND_USER'),'点击了去支付订单 '.$order['order_num_alias']);
  36. $url=$this->pay_api($order['payment_code'], $data);
  37. @header("Location: ".$url);
  38. die();
  39. }
  40. /**
  41. 接受快递鸟通知
  42. **/
  43. function kuaidiniao()
  44. {
  45. echo json_encode(array('Success'=>true));
  46. $request_data = I('post.RequestData');
  47. $request_data = htmlspecialchars_decode($request_data);
  48. $request_data_arr = json_decode($request_data,true);
  49. foreach($request_data_arr['Data'] as $data)
  50. {
  51. $LogisticCode = $data['LogisticCode'];
  52. $trace = serialize($data['Traces']);
  53. M('order')->where( array('shipping_no' => $LogisticCode) )->save( array('shipping_traces' => $trace) );
  54. }
  55. }
  56. /**
  57. * 提交订单支付
  58. * @param unknown $order_id
  59. */
  60. function order_pay()
  61. {
  62. $order_id = I('get.order_id',0);
  63. if($order_id > 0)
  64. {
  65. $order = M('order')->where( array('order_id' => $order_id) )->find();
  66. if($order['order_status_id'] == 3)
  67. {
  68. if($order['total'] <= 0)
  69. {
  70. $this->yuer_payreturn($order['order_num_alias']);
  71. } else {
  72. $order['payment_code'] = empty($order['payment_code']) ? 'wxpay': $order['payment_code'];
  73. //单独支付一个店铺的订单
  74. M('order_relate')->where( array('order_id' => $order_id) )->delete();
  75. $order_all_data = array();
  76. $order_all_data['member_id'] = session('user_auth.uid');
  77. $order_all_data['order_num_alias'] = build_order_no(session('user_auth.uid'));;
  78. $order_all_data['transaction_id'] = '';
  79. $order_all_data['order_status_id'] = 3;
  80. $order_all_data['is_pin'] = $order['is_pin'];
  81. $order_all_data['paytime'] = 0;
  82. $order_all_data['total_money'] = $order['total'];
  83. $order_all_data['addtime'] = time();
  84. $order_all_id = M('order_all')->add($order_all_data);
  85. $order_relate_data = array();
  86. $order_relate_data['order_all_id'] = $order_all_id;
  87. $order_relate_data['order_id'] = $order_id;
  88. $order_relate_data['addtime'] = time();
  89. M('order_relate')->add($order_relate_data);
  90. $wxpay_url = C('SITE_URL')."index.php?s=/Payment/wxpay_order/pay_order_no/{$order_all_data[order_num_alias]}";
  91. header('Location: '.$wxpay_url);
  92. die();
  93. }
  94. }
  95. }
  96. }
  97. /**
  98. * $pay_type 购买商品,还是预存款
  99. * $order 订单信息
  100. */
  101. function pay_api($payment_method,$order_all_id){
  102. $order_all = M('order_all')->where( array('id' => $order_all_id) )->find();
  103. if($payment_method=='alipay'){
  104. $alipay= new \Lib\Payment\Alipay(get_payment_config('alipay'),$order_all);
  105. return $alipay->get_payurl();
  106. }
  107. //wxpay
  108. if($payment_method == 'wxpay')
  109. {
  110. $wxpay_url = C('SITE_URL')."index.php?s=/Payment/wxpay_order/pay_order_no/{$order_all[order_num_alias]}";
  111. echo json_encode( array('code' =>1 ,'url' => $wxpay_url) );
  112. die();
  113. //header('Location: '.$wxpay_url);
  114. //$this->redirect( 'Payment/wxpay_order',array('pay_order_no' => $order['order_num_alias']) );
  115. }
  116. }
  117. //微信支付通知
  118. function weixin_notify()
  119. {
  120. $lib_path = dirname(dirname( dirname(__FILE__) )).'/Lib/';
  121. $data_path = dirname( dirname(dirname( dirname(__FILE__) )) ).'/Data/wxpaylogs/'.date('Y-m-d')."/";
  122. require_once $lib_path."/Weixin/PayNotifyCallBack.class.php";
  123. $notify = new \PayNotifyCallBack();
  124. $notify->Handle(false);
  125. }
  126. //开始微信支付订单
  127. function wxpay_order()
  128. {
  129. $lib_path = dirname(dirname( dirname(__FILE__) )).'/Lib/';
  130. $data_path = dirname( dirname(dirname( dirname(__FILE__) )) ).'/Data/wxpaylogs/'.date('Y-m-d')."/";
  131. RecursiveMkdir($data_path);
  132. require_once $lib_path."/Weixin/JsApiPay.class.php";
  133. $pay_order_no = I('get.pay_order_no',0);
  134. $order = M('order_all')->where(array('order_num_alias'=>$pay_order_no, 'member_id' => is_login() ))->find();
  135. if(empty($order)) {
  136. $this->redirect( U('Index/index') );
  137. }
  138. //初始化日志
  139. $logHandler= new \CLogFileHandler( $data_path .date('Y-m-d').'.log');
  140. $log = \Log::Init($logHandler, 15);
  141. $member_info = M('member')->where( array('member_id' => $order['member_id']) )->find();
  142. //①、获取用户openid
  143. $tools = new \JsApiPay();
  144. //$order['total'] = 0.01;
  145. //②、统一下单
  146. $input = new \WxPayUnifiedOrder();
  147. $input->SetBody(mb_substr('商品购买', 0, 30, 'utf-8'));
  148. $input->SetAttach(mb_substr('商品购买', 0, 30, 'utf-8'));
  149. $input->SetOut_trade_no($order['id'].'-'.time());
  150. $input->SetTotal_fee( ( $order['total_money'] *100) );
  151. $input->SetTime_start(date("YmdHis"));
  152. $input->SetTime_expire(date("YmdHis", time() + 600));
  153. $input->SetGoods_tag("");
  154. $notify_url = C('SITE_URL').'notify.php';
  155. $input->SetNotify_url($notify_url);
  156. $input->SetTrade_type("JSAPI");
  157. $input->SetOpenid($member_info['openid']);
  158. $order_str = \WxPayApi::unifiedOrder($input);
  159. $jsApiParameters = $tools->GetJsApiParameters($order_str);
  160. //var_dump($jsApiParameters);die();
  161. //获取共享收货地址js函数参数
  162. $editAddress = $tools->GetEditAddressParameters();
  163. $hashids = new \Lib\Hashids(C('PWD_KEY'), C('URL_ID'));
  164. //$order_id = $hashids->encode($order['id']);
  165. if($order['is_pin'] == 1)
  166. {
  167. $order_relate = M('order_relate')->where( array('order_all_id' => $order['id']) )->find();
  168. $order_id = $hashids->encode($order_relate['order_id']);
  169. $refurl = C('SITE_URL')."index.php?s=/Group/info/group_order_id/{$order_id}/is_show/1";
  170. } else {
  171. $refurl = C('SITE_URL')."index.php?s=/order/order_all_info/id/{$order[id]}";
  172. }
  173. $backurl = C('SITE_URL')."index.php?s=/order/order_all_info/id/{$order[id]}";
  174. $this->order_id = $order['id'];
  175. $this->refurl = $refurl;
  176. $this->backurl = $backurl;
  177. $this->jsApiParameters = $jsApiParameters;
  178. $this->editAddress = $editAddress;
  179. $this->display();
  180. }
  181. //写入订单
  182. function pay(){
  183. $json=array();
  184. //pickup express
  185. $pay_method = I('post.pay_method');//支付类型
  186. $order_msg_str = I('post.order_msg_str');//商品订单留言
  187. $quan_arr = I('post.quan_arr');//使用优惠券
  188. //express_method:express_method,pick_mobile:pick_mobile,pick_id:pick_id,pick_name:pick_name
  189. $pick_up_id = I('post.pick_id');//$data_s['pick_up_id'];
  190. $dispatching = I('post.dispatching');//$data_s['dispatching'];
  191. $ziti_name = I('post.pick_name');//$data_s['ziti_name'];
  192. $ziti_mobile = I('post.pick_mobile');//$data_s['ziti_mobile'];
  193. $order_msg_arr = explode('@,@', $order_msg_str);
  194. $msg_arr = array();
  195. foreach($order_msg_arr as $val)
  196. {
  197. $tmp_val = explode('@_@', $val);
  198. $msg_arr[ $tmp_val[0] ] = $tmp_val[1];
  199. }
  200. $member_id = session('user_auth.uid');
  201. $order_quan_arr = array();
  202. if( !empty($quan_arr) )
  203. {
  204. foreach($quan_arr as $q_val)
  205. {
  206. $tmp_q = array();
  207. $tmp_q = explode('_',$q_val);
  208. $voucher_info = M('voucher_list')->where( array('id' =>$tmp_q[1],
  209. 'store_id' =>$tmp_q[0], 'user_id' => session('user_auth.uid'),'consume' =>'N','end_time' => array('gt',time() ) ) )->find();
  210. if( !empty($voucher_info) )
  211. {
  212. $order_quan_arr[$tmp_q[0]] = $tmp_q[1];
  213. }
  214. }
  215. }
  216. $cart=new \Lib\Cart();
  217. // 验证商品数量
  218. $buy_type = I('post.buy_type');
  219. $is_pin = 0;
  220. if($buy_type == 'pin')
  221. {
  222. $is_pin = 1;
  223. }
  224. $goodss = $cart->get_all_goods($buy_type,1);
  225. $seller_goodss = array();
  226. $del_integral = 0;
  227. foreach($goodss as $key => $val)
  228. {
  229. if($buy_type == 'dan')
  230. {
  231. $new_key = 'cart.'.$key;
  232. $s = session($new_key);
  233. if( isset($s['can_del']) && $s['can_del'] == 1)
  234. {
  235. $cart->remove($key);
  236. }else if(isset($s['old_quantity'])){
  237. $s['quantity'] = $s['old_quantity'];
  238. session($new_key,$s);
  239. }
  240. }
  241. $goods_store_field = M('goods')->field('store_id')->where( array('goods_id' => $val['goods_id']) )->find();
  242. $seller_goodss[ $goods_store_field['store_id'] ][$key] = $val;
  243. }
  244. //付款人
  245. $payment=M('Member')->find(session('user_auth.uid'));
  246. //收货人
  247. $add_where = array('member_id'=>session('user_auth.uid'));
  248. $address = M('address')->where( $add_where )->order('is_default desc,address_id desc')->find();
  249. $pay_total = 0;
  250. //M('order_all')
  251. $order_all_data = array();
  252. $order_all_data['member_id'] = session('user_auth.uid');
  253. $order_all_data['order_num_alias'] = build_order_no(session('user_auth.uid'));;
  254. $order_all_data['transaction_id'] = '';
  255. $order_all_data['order_status_id'] = 3;
  256. $order_all_data['is_pin'] = $is_pin;
  257. $order_all_data['paytime'] = 0;
  258. $order_all_data['addtime'] = time();
  259. $order_all_id = M('order_all')->add($order_all_data);
  260. $integral_model = D('Seller/Integral');
  261. foreach($seller_goodss as $kk => $vv)
  262. {
  263. $data = array();
  264. $data['member_id']=session('user_auth.uid');
  265. $data['name']=session('user_auth.username');
  266. $data['telephone']=$address['telephone'];
  267. $data['shipping_name']=$address['name'];
  268. $data['shipping_address']=$address['address'];
  269. $data['shipping_tel']=$address['telephone'];
  270. $data['shipping_province_id']=$address['province_id'];
  271. $data['shipping_city_id']=$address['city_id'];
  272. $data['shipping_country_id']=$address['country_id'];
  273. $data['shipping_method'] = 0;
  274. $data['delivery']=$dispatching;
  275. $data['pick_up_id']=$pick_up_id;
  276. $data['ziti_name']=$ziti_name;
  277. $data['ziti_mobile']=$ziti_mobile;
  278. $data['payment_method']=$pay_method;
  279. $data['address_id']= $address['address_id'];
  280. //quan_arr
  281. $data['voucher_id'] = isset($order_quan_arr[$kk]) ? $order_quan_arr[$kk]:0;
  282. $data['user_agent']=$_SERVER['HTTP_USER_AGENT'];
  283. $data['date_added']=time();
  284. $hashids = new \Lib\Hashids(C('PWD_KEY'), C('URL_ID'));
  285. $subject='';
  286. $fare = 0;
  287. $goodss = $vv;
  288. $trans_free_toal = 0;//运费
  289. $goods_data = array();
  290. $order_total = 0;
  291. $is_lottery = 0;
  292. $is_integral = 0;
  293. foreach($goodss as $key => $good)
  294. {
  295. if($good['shipping']==1)
  296. {
  297. //统一运费
  298. $trans_free_toal += $good['goods_freight'];
  299. $trans_free = $good['goods_freight'];
  300. }else {
  301. //运费模板
  302. $trans_free = D('Home/Transport')->calc_transport($good['transport_id'], $good['quantity']*$good['weight'], $address['city_id'] );
  303. $trans_free_toal +=$trans_free;
  304. }
  305. $goods_info = explode(':', $key);
  306. $goods_id = $goods_info[0];
  307. if (!empty($goods_info[1])) {
  308. $options = base64_decode($goods_info[1]);
  309. } else {
  310. $options = '';
  311. }
  312. $order_total += $good['total'];
  313. $tp_goods_info = M('goods')->field('store_id,type')->where( array('goods_id' =>$good['goods_id'] ) )->find();
  314. if($tp_goods_info['type'] == 'lottery')
  315. {
  316. $is_lottery = 1;
  317. }
  318. if($tp_goods_info['type'] == 'integral')
  319. {
  320. $is_integral = 1;
  321. $is_pin = 0;
  322. $check_result = $integral_model->check_user_score_can_pay($member_id, $good['sku_str'], $good['goods_id'] );
  323. if($check_result['code'] == 1)
  324. {
  325. die();
  326. }
  327. }
  328. $goods_data[] = array(
  329. 'goods_id' => $good['goods_id'],
  330. 'store_id' => $tp_goods_info['store_id'],
  331. 'name' => $good['name'],
  332. 'model' => $good['model'],
  333. 'is_pin' => $is_pin,
  334. 'pin_id' => $good['pin_id'],
  335. 'header_disc' => $good['header_disc'],
  336. 'option' => $options,
  337. 'quantity' => $good['quantity'],
  338. 'shipping_fare' => $trans_free,
  339. 'price' => $good['price'],
  340. 'total' => $good['total'],
  341. 'comment' => htmlspecialchars($msg_arr[$key])
  342. );
  343. }
  344. $data['type'] = 'normal';
  345. if($is_pin == 1)
  346. {
  347. $data['type'] = 'pintuan';
  348. if($is_lottery == 1)
  349. {
  350. $data['type'] = 'lottery';
  351. }
  352. }
  353. if($is_integral == 1)
  354. {
  355. $data['type'] = 'integral';
  356. $is_pin = 0;
  357. }
  358. $data['shipping_fare'] = floatval($trans_free_toal);
  359. $data['store_id']= $kk;
  360. // $tp_goods_info['store_id'],
  361. $data['goodss'] = $goods_data;
  362. $data['order_num_alias']=build_order_no($kk);
  363. $data['totals'][0]=array(
  364. 'code'=>'sub_total',
  365. 'title'=>'商品价格',
  366. 'text'=>'¥'.$order_total,
  367. 'value'=>$order_total
  368. );
  369. $data['totals'][1]=array(
  370. 'code'=>'shipping',
  371. 'title'=>'运费',
  372. 'text'=>'¥'.$trans_free_toal,
  373. 'value'=>$trans_free_toal
  374. );
  375. $data['totals'][2]=array(
  376. 'code'=>'total',
  377. 'title'=>'总价',
  378. 'text'=>'¥'.($order_total+$trans_free_toal),
  379. 'value'=>($order_total+$trans_free_toal)
  380. );
  381. if($data['voucher_id'] > 0) {
  382. $voucher_info = M('voucher_list')->where( array('id' => $data['voucher_id']) )->find();
  383. $data['voucher_credit'] = $voucher_info['credit'];
  384. M('voucher_list')->where( array('id' => $data['voucher_id']) )->save( array('consume' => 'Y') );
  385. } else {
  386. $data['voucher_credit'] = 0;
  387. }
  388. $data['total']=($order_total+$trans_free_toal- $data['voucher_credit'] );
  389. //积分商城
  390. if($data['type'] == 'integral')
  391. {
  392. $del_integral += $order_total;//扣除积分
  393. $data['total'] = 0;
  394. $order_total = 0;
  395. }
  396. $oid=D('Order')->addOrder($data);
  397. if($data['delivery'] == 'pickup')
  398. {
  399. $verify_bool = true;
  400. $verifycode = 0;
  401. while($verify_bool)
  402. {
  403. $code = (ceil(time()/100)+rand(10000000,40000000)).rand(1000,9999);
  404. $verifycode = $code ? $code : rand(100000,999999);
  405. $verifycode = str_replace('1989','9819',$verifycode);
  406. $verifycode = str_replace('1259','9521',$verifycode);
  407. $verifycode = str_replace('12590','95210',$verifycode);
  408. $verifycode = str_replace('10086','68001',$verifycode);
  409. $pick_order = M('pick_order')->where( array('pick_sn' => $verifycode) )->find();
  410. if(empty($pick_order))
  411. {
  412. $verify_bool = false;
  413. }
  414. }
  415. $pick_data = array();
  416. $pick_data['pick_sn'] = $verifycode;
  417. $pick_data['pick_id'] = $pick_up_id;
  418. $pick_data['order_id'] = $oid;
  419. $pick_data['state'] = 0;
  420. $pick_data['ziti_name'] = $ziti_name;
  421. $pick_data['ziti_mobile'] = $ziti_mobile;
  422. $pick_data['addtime'] = time();
  423. M('pick_order')->add($pick_data);
  424. }
  425. $pay_total = $pay_total + $order_total+$trans_free_toal- $data['voucher_credit'];
  426. $order_relate_data = array();
  427. $order_relate_data['order_all_id'] = $order_all_id;
  428. $order_relate_data['order_id'] = $oid;
  429. $order_relate_data['addtime'] = time();
  430. M('order_relate')->add($order_relate_data);
  431. }
  432. M('order_all')->where( array('id' => $order_all_id) )->save( array('total_money' => $pay_total) );
  433. if($order_all_id){
  434. //session('cart_total',null);
  435. $order['notify_url']=C('SITE_URL').U('Payment/alipay_notify');
  436. $order['return_url']=C('SITE_URL').U('Payment/alipay_return');
  437. $order['order_type']='goods_buy';
  438. $order['subject']=$subject;
  439. $order['name']=session('shipping_name');
  440. $order['order_num_alias']=$data['order_num_alias'];
  441. $order['pay_total']=($order_total+$trans_free_toal);
  442. //free_tuan
  443. //session('back_cart_address_id',null);
  444. //session('cart',null);
  445. //session('total',null);
  446. //session('shipping_address_id',null);
  447. //session('back_order_id',$oid);
  448. if($data['total']<=0 )
  449. {
  450. //检测是否需要扣除积分
  451. //var_dump($del_integral,$is_integral );die();
  452. if($del_integral> 0 && $is_integral == 1)
  453. {
  454. //
  455. $integral_model->charge_member_score( $member_id, $del_integral,'out', 'orderbuy', $oid);
  456. }
  457. $this->yuer_payreturn($oid); //测试使用
  458. }else {
  459. $url=$this->pay_api('wxpay', $order_all_id);
  460. }
  461. die();
  462. }else{
  463. echo 9999;die();
  464. $url=U('/checkout');
  465. @header("Location: ".$url);
  466. die();
  467. }
  468. }
  469. //写入订单
  470. function pay2(){
  471. $json=array();
  472. if(I('token')!=md5(session('token'))){
  473. $url=U('/checkout');
  474. @header("Location: ".$url);
  475. die();
  476. }
  477. $cart=new \Lib\Cart();
  478. // 验证商品数量
  479. $goodss = $cart->get_all_goods();
  480. //付款人
  481. $payment=M('Member')->find(session('user_auth.uid'));
  482. //收货人
  483. $shipping=M('Address')->find(session('shipping_address_id'));
  484. $data['member_id']=session('user_auth.uid');
  485. $data['name']=session('user_auth.username');
  486. $data['telephone']=$payment['telephone'];
  487. $data['shipping_name']=$shipping['name'];
  488. $data['shipping_address']=$shipping['address'];
  489. $data['shipping_tel']=$shipping['telephone'];
  490. $data['shipping_province_id']=$shipping['province_id'];
  491. $data['shipping_city_id']=$shipping['city_id'];
  492. $data['shipping_country_id']=$shipping['country_id'];
  493. $data['shipping_method'] = session('express_id');
  494. $data['delivery']=session('shipping_method');
  495. $data['payment_method']=session('payment_method');
  496. $data['address_id']=session('shipping_address_id');
  497. $data['voucher_id']=session('payment_voucher_id');
  498. //payment_voucher_id
  499. $data['user_agent']=$_SERVER['HTTP_USER_AGENT'];
  500. $data['date_added']=time();
  501. $data['comment']=session('remark');
  502. $hashids = new \Lib\Hashids(C('PWD_KEY'), C('URL_ID'));
  503. $subject='';
  504. $fare = 0;
  505. if($goodss){
  506. /**
  507. $sm=D('Transport')->calc_transport(session('shipping_method'),
  508. session('quantity'),
  509. $shipping['city_id']
  510. );
  511. **/
  512. $sm = D('Transport')->calc_transport(null,
  513. session('quantity'),
  514. $shipping['city_id']
  515. );
  516. $t=0;
  517. foreach ($goodss as $goods) {
  518. $option_data = array();
  519. foreach ($goods['option'] as $option) {
  520. $value = $option['value'];
  521. $option_data[] = array(
  522. 'goods_option_id' => $option['goods_option_id'],
  523. 'goods_option_value_id' => $option['goods_option_value_id'],
  524. 'option_id' => $option['option_id'],
  525. 'option_value_id' => $option['option_value_id'],
  526. 'name' => $option['name'],
  527. 'value' => $value,
  528. 'type' => $option['type']
  529. );
  530. }
  531. $tp_goods_info = M('goods')->field('store_id,express_list')->where( array('goods_id' =>$hashids->decode($goods['goods_id'])) )->find();
  532. $express_list_arr = unserialize($tp_goods_info['express_list']);
  533. if($data['delivery'] == 'express')
  534. {
  535. $fare = isset($express_list_arr[$data['shipping_method']]) ? $express_list_arr[$data['shipping_method']]['price'] : 0;
  536. }
  537. $t+=$goods['total'];
  538. $goods['total'] = $goods['total'];
  539. $goods_data[] = array(
  540. 'goods_id' => $goods['goods_id'],
  541. 'name' => $goods['name'],
  542. 'is_header_disc' => $goods['is_header_disc'],
  543. 'store_id' => $tp_goods_info['store_id'],
  544. 'model' => $goods['model'],
  545. 'option' => $option_data,
  546. 'quantity' => $goods['quantity'],
  547. 'pin_type' => $goods['pin_type'],
  548. 'pin_id' => $goods['pin_id'],
  549. 'price' => $goods['price'],
  550. 'total' => $goods['total']
  551. );
  552. $subject.=$goods['name'].' ';
  553. }
  554. if($data['voucher_id'] > 0) {
  555. $voucher_info = M('voucher_list')->where( array('id' => $data['voucher_id']) )->find();
  556. $data['voucher_credit'] = $voucher_info['credit'];
  557. M('voucher_list')->where( array('id' => $data['voucher_id']) )->save( array('consume' => 'Y') );
  558. } else {
  559. $data['voucher_credit'] = 0;
  560. }
  561. $data['shipping_fare'] = floatval($fare);
  562. $data['total']=($t+$fare - $data['voucher_credit']);
  563. $data['goodss'] = $goods_data;
  564. $data['order_num_alias']=build_order_no($data['member_id']);
  565. $data['totals'][0]=array(
  566. 'code'=>'sub_total',
  567. 'title'=>'商品价格',
  568. 'text'=>'¥'.$t,
  569. 'value'=>$t
  570. );
  571. $data['totals'][1]=array(
  572. 'code'=>'shipping',
  573. 'title'=>'运费',
  574. 'text'=>'¥'.$fare,
  575. 'value'=>$fare
  576. );
  577. $data['totals'][2]=array(
  578. 'code'=>'voucher',
  579. 'title'=>'优惠券',
  580. 'text'=>'¥'.$data['voucher_credit'],
  581. 'value'=>$data['voucher_credit']
  582. );
  583. $data['totals'][3]=array(
  584. 'code'=>'total',
  585. 'title'=>'总价',
  586. 'text'=>'¥'.($t+$fare- $data['voucher_credit']),
  587. 'value'=>($t+$fare- $data['voucher_credit'])
  588. );
  589. $oid=D('Order')->addOrder($data);
  590. //delivery pickup pick_up_id
  591. if($data['delivery'] == 'pickup')
  592. {
  593. $verify_bool = true;
  594. $verifycode = 0;
  595. while($verify_bool)
  596. {
  597. $code = (ceil(time()/100)+rand(10000000,40000000)).rand(1000,9999);
  598. $verifycode = $code ? $code : rand(100000,999999);
  599. $verifycode = str_replace('1989','9819',$verifycode);
  600. $verifycode = str_replace('1259','9521',$verifycode);
  601. $verifycode = str_replace('12590','95210',$verifycode);
  602. $verifycode = str_replace('10086','68001',$verifycode);
  603. $pick_order = M('pick_order')->where( array('pick_sn' => $verifycode) )->find();
  604. if(empty($pick_order))
  605. {
  606. $verify_bool = false;
  607. }
  608. }
  609. $pick_data = array();
  610. $pick_data['pick_sn'] = $verifycode;
  611. $pick_data['pick_id'] = session('pick_up_id');
  612. $pick_data['order_id'] = $oid;
  613. $pick_data['state'] = 0;
  614. $pick_data['addtime'] = time();
  615. M('pick_order')->add($pick_data);
  616. }
  617. if($oid){
  618. session('cart_total',null);
  619. $order['notify_url']=C('SITE_URL').U('Payment/alipay_notify');
  620. $order['return_url']=C('SITE_URL').U('Payment/alipay_return');
  621. $order['order_type']='goods_buy';
  622. $order['subject']=$subject;
  623. $order['name']=session('shipping_name');
  624. $order['order_num_alias']=$data['order_num_alias'];
  625. $order['pay_total']=($t+$sm['price']);
  626. //free_tuan
  627. $order_goods_info = M('order_goods')->where( array('order_id' => $oid) )->find();
  628. session('back_cart_address_id',null);
  629. session('cart',null);
  630. session('total',null);
  631. session('shipping_address_id',null);
  632. session('back_order_id',$oid);
  633. if($data['total']<=0 || $order_goods_info['free_tuan'] ==1)
  634. {
  635. $this->yuer_payreturn($data['order_num_alias']); //测试使用
  636. }else {
  637. $url=$this->pay_api('wxpay', $order);
  638. }
  639. die();
  640. }else{
  641. echo 9999;die();
  642. $url=U('/checkout');
  643. @header("Location: ".$url);
  644. die();
  645. }
  646. }
  647. }
  648. public function success()
  649. {
  650. $order_id = I('get.order_id');
  651. //removeAll
  652. $cart=new \Lib\Cart();
  653. $cart->removeAll();
  654. session('total',null);
  655. $pin_order = M('pin_order')->field('pin_id')->where( array('order_id' =>$order_id) )->find();
  656. //Order/info/id/14
  657. $redir_url = U('Order/info', array('id' => $order_id));
  658. if(!empty($pin_order))
  659. {
  660. $hashids = new \Lib\Hashids(C('PWD_KEY'), C('URL_ID'));
  661. $group_order_id = $hashids->encode($order_id);
  662. $redir_url = U('Group/info', array('group_order_id' => $group_order_id,'is_show' =>1));
  663. redirect($redir_url,0);
  664. }
  665. $this->redir_url = $redir_url;
  666. $this->order_id = $order_id;
  667. $this->display();
  668. }
  669. function de_bug($content){
  670. $file = ROOT_PATH."/Tmp/wxpay_debug.php";
  671. file_put_contents($file,$content);
  672. }
  673. //数据以post方式返回
  674. function alipay_notify(){
  675. $alipay= new \Lib\Payment\Alipay(get_payment_config('alipay'));
  676. $verify_result = $alipay->verifyNotify();
  677. if($verify_result) {
  678. //$this->de_bug('success');
  679. //商户订单号
  680. //$out_trade_no = $_POST['out_trade_no'];
  681. //支付宝交易号
  682. //$trade_no = $_POST['trade_no'];
  683. //交易状态
  684. //$trade_status = $_POST['trade_status'];
  685. if($_POST['trade_status'] == 'TRADE_FINISHED') {
  686. //$this->de_bug('TRADE_FINISHED');
  687. }
  688. else if ($_POST['trade_status'] == 'TRADE_SUCCESS') {
  689. //$this->de_bug('TRADE_SUCCESS');
  690. $order=M('Order')->getByOrderNumAlias($_POST['out_trade_no']);
  691. if($order&&($order['order_status_id']!=C('paid_order_status_id'))){
  692. //支付完成
  693. $o['order_id']=$order['order_id'];
  694. $o['order_status_id']=C('paid_order_status_id');
  695. $o['date_modified']=time();
  696. $o['pay_time']=time();
  697. M('Order')->save($o);
  698. $oh['order_id']=$order['order_id'];
  699. $oh['order_status_id']=C('paid_order_status_id');
  700. $oh['comment']='买家已付款';
  701. $oh['date_added']=time();
  702. $oh['notify']=1;
  703. M('OrderHistory')->add($oh);
  704. $model=new \Admin\Model\OrderModel();
  705. $this->order=$model->order_info($order['order_id']);
  706. $html=$this->fetch('Mail:order');
  707. think_send_mail($order['email'],$order['name'],'下单成功-'.C('SITE_NAME'),$html);
  708. storage_user_action(session('user_auth.uid'),session('user_auth.username'),C('FRONTEND_USER'),'支付了订单 '.$order['order_num_alias']);
  709. //@header("Location: ".U('/pay_success'));
  710. }
  711. echo "success";
  712. }
  713. }else{
  714. //$this->de_bug('fail');
  715. echo "fail";
  716. }
  717. }
  718. private function yuer_payreturn($order_id){
  719. // $order=M('Order')->getByOrderNumAlias($out_trade_no);
  720. $order=M('Order')->where( array('order_id' => $order_id) )->find();
  721. $out_trade_no = $order['order_num_alias'];
  722. if($order['order_status_id']==C('paid_order_status_id')){
  723. @header("Location: ".U('/pay_success'));
  724. die;
  725. }
  726. if($order&&($order['order_status_id']!=C('paid_order_status_id'))){
  727. //支付完成
  728. if(true){
  729. $goods_model = D('Home/Goods');
  730. $kucun_method = C('kucun_method');
  731. $kucun_method = empty($kucun_method) ? 0 : intval($kucun_method);
  732. if($kucun_method == 1)
  733. {//支付完减库存,增加销量
  734. $order_goods_list = M('order_goods')->where( array('order_id' => $order['order_id']) )->select();
  735. foreach($order_goods_list as $order_goods)
  736. {
  737. //销量增加 del_goods_mult_option_quantity($order_id,$option,$goods_id,$quantity,$type='1')
  738. $goods_model->del_goods_mult_option_quantity($order['order_id'],$order_goods['rela_goodsoption_valueid'],$order_goods['goods_id'],$order_goods['quantity'],1);
  739. //扣除库存
  740. }
  741. }
  742. $oh['order_id']=$order['order_id'];
  743. $oh['order_status_id'] = 1;
  744. $oh['comment']='买家已付款';
  745. $oh['date_added']=time();
  746. $oh['notify']=0;
  747. M('OrderHistory')->add($oh);
  748. $pin_order = M('pin_order')->where( array('order_id' =>$order['order_id'] ) )->find();
  749. if(!empty($pin_order))
  750. {
  751. $o = array();
  752. $o['order_id']=$order['order_id'];
  753. $o['order_status_id']= 2;
  754. $o['date_modified']=time();
  755. $o['pay_time']=time();
  756. M('Order')->save($o);
  757. //group_order_id
  758. $pin_model = D('Home/Pin');
  759. $is_pin_success = $pin_model->checkPinSuccess($pin_order['pin_id']);
  760. if($is_pin_success) {
  761. //todo send pintuan success notify
  762. $pin_model->updatePintuanSuccess($pin_order['pin_id']);
  763. }else{
  764. }
  765. }else{
  766. $o = array();
  767. $o['order_id']=$order['order_id'];
  768. $o['order_status_id']= 1;
  769. $o['date_modified']=time();
  770. $o['pay_time']=time();
  771. M('Order')->save($o);
  772. $share_model = D('Seller/Fissionsharing');
  773. $share_model->send_order_commiss_money( $order['order_id'] );
  774. }
  775. $result = array('code' => 1, 'url' => U('Payment/success',array('order_id' => $order['order_id'])));
  776. echo json_encode($result);
  777. die();
  778. }
  779. }
  780. }
  781. private function yuer_payreturn2($out_trade_no){
  782. $order=M('Order')->getByOrderNumAlias($out_trade_no);
  783. if($order['order_status_id']==C('paid_order_status_id')){
  784. @header("Location: ".U('/pay_success'));
  785. die;
  786. }
  787. if($order&&($order['order_status_id']!=C('paid_order_status_id'))){
  788. //支付完成
  789. if(true){
  790. $o['order_id']=$order['order_id'];
  791. $o['order_status_id']= $order['is_pin'] == 1 ? 2:1;
  792. $o['date_modified']=time();
  793. $o['pay_time']=time();
  794. M('Order')->save($o);
  795. $goods_model = D('Home/Goods');
  796. $kucun_method = C('kucun_method');
  797. $kucun_method = empty($kucun_method) ? 0 : intval($kucun_method);
  798. if($kucun_method == 1)
  799. {//支付完减库存,增加销量
  800. $order_goods_list = M('order_goods')->where( array('order_id' => $order['order_id']) )->select();
  801. foreach($order_goods_list as $order_goods)
  802. {
  803. //销量增加 rela_goodsoption_valueid
  804. $goods_model->del_goods_mult_option_quantity($order['order_id'],$order_goods['quantity'],1);
  805. //扣除库存
  806. }
  807. }
  808. $oh['order_id']=$order['order_id'];
  809. $oh['order_status_id']= $order['is_pin'] == 1 ? 2:1;
  810. $oh['comment']='买家已付款';
  811. $oh['date_added']=time();
  812. $oh['notify']=1;
  813. M('OrderHistory')->add($oh);
  814. //发送购买通知
  815. $weixin_nofity = D('Home/Weixinnotify');
  816. $weixin_nofity->orderBuy($order);
  817. $model=new \Admin\Model\OrderModel();
  818. $this->order=$model->order_info($order['order_id']);
  819. $hashids = new \Lib\Hashids(C('PWD_KEY'), C('URL_ID'));
  820. $order_id = $hashids->encode($order['order_id']);
  821. if($order['is_pin'] == 1)
  822. {
  823. $pin_model = D('Home/Pin');
  824. $is_pin_success = $pin_model->checkPinSuccess($order['pin_id']);
  825. if($is_pin_success) {
  826. //todo send pintuan success notify
  827. $pin_model->updatePintuanSuccess($order['pin_id']);
  828. }
  829. $refurl = C('SITE_URL')."index.php?s=/Group/info/group_order_id/{$order_id}/is_show/1";
  830. @header("Location: ".$refurl);
  831. die();
  832. } else {
  833. @header("Location: ".U('Order/info',array('id' =>$order['order_id'] )));
  834. die();
  835. }
  836. }
  837. }
  838. }
  839. function alipay_return(){
  840. $alipay= new \Lib\Payment\Alipay(get_payment_config('alipay'));
  841. //对进入的参数进行远程数据判断
  842. $verify = $alipay->return_verify();
  843. if($verify){
  844. $order=M('Order')->getByOrderNumAlias($_GET['out_trade_no']);
  845. if($order['order_status_id']==C('paid_order_status_id')){
  846. @header("Location: ".U('/pay_success'));
  847. die;
  848. }
  849. if($order&&($order['order_status_id']!=C('paid_order_status_id'))){
  850. //支付完成
  851. if($_GET['trade_status']=='TRADE_SUCCESS'){
  852. $o['order_id']=$order['order_id'];
  853. $o['order_status_id']=C('paid_order_status_id');
  854. $o['date_modified']=time();
  855. $o['pay_time']=time();
  856. M('Order')->save($o);
  857. $oh['order_id']=$order['order_id'];
  858. $oh['order_status_id']=C('paid_order_status_id');
  859. $oh['comment']='买家已付款';
  860. $oh['date_added']=time();
  861. $oh['notify']=1;
  862. M('OrderHistory')->add($oh);
  863. $model=new \Admin\Model\OrderModel();
  864. $this->order=$model->order_info($order['order_id']);
  865. $html=$this->fetch('Mail:order');
  866. think_send_mail($order['email'],$order['name'],'下单成功-'.C('SITE_NAME'),$html);
  867. storage_user_action(session('user_auth.uid'),session('user_auth.username'),C('FRONTEND_USER'),'支付了订单 '.$order['order_num_alias']);
  868. @header("Location: ".U('/pay_success'));
  869. }
  870. }else{
  871. die('订单不存在');
  872. }
  873. }else{
  874. die('支付失败');
  875. }
  876. }
  877. }