PaymentController.class.php.back 33 KB

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