WeixinModel.class.php.back 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496
  1. <?php
  2. /**
  3. * lionfish 商城系统
  4. *
  5. * ==========================================================================
  6. * @link http://www.liofis.com/
  7. * @copyright Copyright (c) 2015 liofis.com.
  8. * @license http://www.liofis.com/license.html License
  9. * ==========================================================================
  10. *
  11. * @author fish
  12. *
  13. */
  14. namespace Home\Model;
  15. class WeixinModel{
  16. public function refundOrder($order_id, $money=0, $uniacid=0,$order_goods_id=0,$is_back_sellcount = 1,$refund_quantity = 0,$is_zi_order_refund =0)
  17. {
  18. $_GPC = I('request.');
  19. $lib_path = dirname(dirname( dirname(__FILE__) )).'/Lib/';
  20. set_time_limit(0);
  21. require_once $lib_path."/Weixin/lib/WxPay.Api.php";
  22. $order_info = M('lionfish_comshop_order')->where( array('order_id' => $order_id) )->find();
  23. $member_info = M('lionfish_comshop_member')->where( array('member_id' => $order_info['member_id'] ) )->find();
  24. $openId = $member_info['openid'];
  25. $we_openid = $member_info['we_openid'];
  26. if( $order_info['from_type'] == 'wepro' )
  27. {
  28. $openId = $we_openid;
  29. }
  30. //we_openid
  31. //money
  32. $transaction_id = $order_info["transaction_id"];
  33. if( $order_info['type'] == 'integral' )
  34. {
  35. $total_fee = ( $order_info["shipping_fare"] )*100;
  36. }else{
  37. $total_fee = ($order_info["total"] + $order_info["shipping_fare"]-$order_info['voucher_credit']-$order_info['fullreduction_money'] - $order_info['score_for_money']-$order_info['fare_shipping_free'] )*100;
  38. }
  39. //预售begin
  40. $presale_result = D('Home/PresaleGoods')->getOrderPresaleInfo( $order_id );
  41. if( $presale_result['code'] ==0 )
  42. {
  43. $total_fee = $total_fee - $presale_result['data']['presale_ding_money'] * 100;
  44. }
  45. //end
  46. $refund_fee = $total_fee;
  47. //order_goods_id
  48. if( !empty($order_goods_id) )
  49. {
  50. $order_goods_info = M('lionfish_comshop_order_goods')->where( array('order_goods_id' =>$order_goods_id ) )->find();
  51. $refund_fee = ($order_goods_info["total"] + $order_goods_info["shipping_fare"]-$order_goods_info['voucher_credit']-$order_goods_info['fullreduction_money'] - $order_goods_info['score_for_money']-$order_goods_info['fare_shipping_free'])*100;
  52. if( $order_info['type'] == 'integral' )
  53. {
  54. $refund_fee = ( $order_goods_info["shipping_fare"] )*100;
  55. }
  56. }
  57. if($money > 0 && $order_info['type'] != 'integral' )
  58. {
  59. $refund_fee = $money * 100;
  60. }else if( $money > 0 && $order_info['type'] == 'integral' && !empty($order_goods_info) )
  61. {
  62. if(!empty($_GPC['refund_money'])){
  63. $refund_money = isset($_GPC['refund_money']) && $_GPC['refund_money'] >0 ? floatval($_GPC['refund_money']) : 0;
  64. //商品多个运费和
  65. $refund_fee = ( $money - $refund_money )*100;
  66. //商品多个商品单价和
  67. $order_info['total'] = $refund_money;
  68. }else{
  69. $order_refund_history_image = M('lionfish_comshop_order_refund')->where( array('order_id' => $order_id) )->order('addtime asc')->find();
  70. $ref_shipping_fare = M('lionfish_comshop_order_refund')->field('ref_id,ref_money,real_refund_quantity,ref_shipping_fare')->where( array('order_id' => $order_id ))->order('ref_id desc')->find();
  71. $refund_fee = ( $ref_shipping_fare['ref_shipping_fare'] )*100;
  72. $order_info['total'] = $money ;
  73. }
  74. }
  75. // else if( isset($is_open_yinpay) && $is_open_yinpay == 3 )
  76. $is_open_yinpay = D('Home/Front')->get_config_by_name('is_open_yinpay');
  77. if($order_info['payment_code'] == 'fypay'){
  78. $order_relate_info = M('lionfish_comshop_order_relate')->where( array('order_id' => $order_id ) )->order('id desc')->find();
  79. if( !empty($order_relate_info) && $order_relate_info['order_all_id'] > 0 )
  80. {
  81. $order_all_info = M('lionfish_comshop_order_all')->where( array('id' => $order_relate_info['order_all_id'] ) )->find();
  82. if( !empty($order_all_info) && !empty($order_all_info['out_trade_no']) )
  83. {
  84. $out_trade_no = $order_all_info['out_trade_no'];
  85. $appid = D('Home/Front')->get_config_by_name('wepro_appid');
  86. $mch_id = D('Home/Front')->get_config_by_name('wepro_partnerid');
  87. $nonce_str = nonce_str();
  88. $pay_key = D('Home/Front')->get_config_by_name('wepro_key');
  89. $post = array();
  90. $post['appid'] = $appid;
  91. $post['mch_id'] = $mch_id;
  92. $post['nonce_str'] = $nonce_str;
  93. $post['out_trade_no'] = $out_trade_no;
  94. $sign = sign($post,$pay_key);
  95. $post_xml = '<xml>
  96. <appid>'.$appid.'</appid>
  97. <mch_id>'.$mch_id.'</mch_id>
  98. <nonce_str>'.$nonce_str.'</nonce_str>
  99. <out_trade_no>'.$out_trade_no.'</out_trade_no>
  100. <sign>'.$sign.'</sign>
  101. </xml>';
  102. $url = "https://api.mch.weixin.qq.com/pay/orderquery";
  103. $result = http_request($url,$post_xml);
  104. $array = xml($result);
  105. if( $array['RETURN_CODE'] == 'SUCCESS' && $array['RETURN_MSG'] == 'OK' )
  106. {
  107. if( $array['TRADE_STATE'] == 'SUCCESS' || $array['TRADE_STATE'] == 'REFUND' )
  108. {
  109. $total_fee = $array['TOTAL_FEE'];
  110. }
  111. }
  112. }
  113. }
  114. //
  115. require_once $lib_path . "/Weixin/Wxfy.class.php";
  116. $refund_no = '1450'.date("YmdHis").$order_info['order_id'];
  117. // dump(strlen($refund_no));
  118. // exit();
  119. $order_all=D("lionfish_comshop_order_all")->where(array("transaction_id"=>$transaction_id))->find();
  120. if($order_all['transaction_id']==$transaction_id){
  121. $input = new \Wxfy();
  122. $notify_data=[
  123. "version"=>"1",
  124. "ins_cd"=>"08M0027018",
  125. "mchnt_cd"=>"0001000F6549249",
  126. "order_type"=>"WECHAT",
  127. "mchnt_order_no"=>$order_all['out_trade_no'],
  128. "random_str"=>$nonce_str,
  129. "term_id"=>"88888888",
  130. "refund_order_no"=>$refund_no,
  131. "total_amt"=>$total_fee,
  132. 'refund_amt'=>$refund_fee
  133. ];
  134. $res=$input->refundOrder($notify_data);
  135. }else{
  136. return array('code' => 0, 'msg' =>"该订单不存在");
  137. }
  138. // dump($res);
  139. // exit();
  140. // $res='{"ins_cd":"08M0027018","mchnt_cd":"0005210F3948471","mchnt_order_no":"145036-1698222261","order_type":"WECHAT","random_str":"8AHA9V61UY1VOXJ5CJP27MIOURZO1TTL","refund_id":"50101607462023102545369668637","refund_order_no":"145014502023102519014730","reserved_fy_settle_dt":"20231025","reserved_fy_trace_no":"221210993339","reserved_refund_amt":"1","result_code":"000000","result_msg":"SUCCESS","sign":"Fi2gsgMHan11IIru8sLAJ8jwLbtUYfndURakne9l16qtEqaQrkhDx+aXo0jfQGPkx171bBi0T74I8Z6PI1RdBZX0Wf2h9tWKnCRVnxEjQIy0BRPN3g+ktkxrtQgzmDL5jxyatjVFrA43J4gwoqt6o7onlclGUeltsdRPJvoOVGs=","term_id":"88888888","transaction_id":"4200001999202310251498038501"}
  141. //';
  142. $res=json_decode($res,true);
  143. if($res["result_code"] != '000000'){
  144. if( $res['result_msg'] == '订单已全额退款' )
  145. {
  146. $res['result_code'] = '000000';
  147. }else{
  148. return array('code' => 0, 'msg' => $res['result_msg']);
  149. }
  150. }
  151. if($res['result_code'] == '000000')
  152. {
  153. if($order_info['order_status_id'] == 12)
  154. {
  155. $ref_count = M('lionfish_comshop_order_refund')->where( array('order_id' => $order_info['order_id'], 'state' => 0 ) )->count();
  156. if( $ref_count <= 1 )
  157. {
  158. M('lionfish_comshop_order')->where( array('order_id' => $order_info['order_id']) )->save( array('order_status_id' => 7) );
  159. }
  160. }
  161. $order_info['total'] = $refund_fee / 100;
  162. $order_goods_list = M('lionfish_comshop_order_goods')->where( array('order_id' => $order_info['order_id']) )->select();
  163. $order_goods_name_arr = array();
  164. $order_goods_name = '';
  165. foreach($order_goods_list as $order_goods)
  166. {
  167. $order_goods_name_arr[] = $order_goods['name'];
  168. //...
  169. if( !empty($order_goods_id) && $order_goods_id > 0 )
  170. {
  171. if($order_goods_id == $order_goods['order_goods_id'] )
  172. {
  173. if($is_back_sellcount == 1)
  174. {
  175. if( $is_zi_order_refund == 1 && $refund_quantity > 0 )
  176. {
  177. D('Home/Pingoods')->del_goods_mult_option_quantity($order_info['order_id'],$order_goods['rela_goodsoption_valueid'],$order_goods['goods_id'],$refund_quantity,2);
  178. }else{
  179. //获取已退款数量
  180. $has_refund_quantity = M('lionfish_comshop_order_goods_refund')->where( array('order_id' => $order_info['order_id'], 'order_goods_id' =>$order_goods['order_goods_id']) )->sum('quantity');
  181. if(!$has_refund_quantity){
  182. $has_refund_quantity = 0;
  183. }
  184. D('Home/Pingoods')->del_goods_mult_option_quantity($order_info['order_id'],$order_goods['rela_goodsoption_valueid'],$order_goods['goods_id'],$order_goods['quantity']-$has_refund_quantity,2);
  185. }
  186. }
  187. $score_refund_info = M('lionfish_comshop_member_integral_flow')->where( array('order_id' => $order_info['order_id'],'order_goods_id'=>$order_goods['order_goods_id'] ,'type' => 'orderbuy') )->find();
  188. if( !empty($score_refund_info) && $is_zi_order_refund == 1 )
  189. {
  190. // D('Admin/Member')->sendMemberPointChange($order_info['member_id'],$score_refund_info['score'], 0 ,'退款增加积分', 'refundorder', $order_info['order_id'] ,$order_goods['order_goods_id'] );
  191. }
  192. }
  193. }else if( empty($order_goods_id) || $order_goods_id <=0 ){
  194. if($is_back_sellcount == 1){
  195. $has_refund_quantity = M('lionfish_comshop_order_goods_refund')->where( array('order_id' => $order_info['order_id'], 'order_goods_id' =>$order_goods['order_goods_id']) )->sum('quantity');
  196. if(!$has_refund_quantity){
  197. $has_refund_quantity = 0;
  198. }
  199. D('Home/Pingoods')->del_goods_mult_option_quantity($order_info['order_id'],$order_goods['rela_goodsoption_valueid'],$order_goods['goods_id'],$order_goods['quantity']-$has_refund_quantity,2);
  200. }
  201. $score_refund_info = M('lionfish_comshop_member_integral_flow')->where( array('order_id' => $order_info['order_id'],'order_goods_id' =>$order_goods['order_goods_id'] ,'type' => 'orderbuy') )->find();
  202. if( !empty($score_refund_info) )
  203. {
  204. // D('Admin/Member')->sendMemberPointChange($order_info['member_id'],$score_refund_info['score'], 0 ,'退款增加积分', 'refundorder', $order_info['order_id'] ,$order_goods['order_goods_id'] );
  205. }
  206. }
  207. if( $order_info['type'] == 'integral' )
  208. {
  209. D('Admin/Member')->sendMemberPointChange($order_info['member_id'],$order_goods['total'], 0 ,'退款增加积分','refundorder', $order_info['order_id'] ,$order_goods['order_goods_id'] );
  210. }
  211. }
  212. $order_goods_name = implode('\r\n', $order_goods_name_arr); //."\r\n";
  213. //分佣也要退回去
  214. if($is_zi_order_refund == 0)
  215. D('Seller/Community')->back_order_commission($order_info['order_id'],$order_goods_id);
  216. return array('code' => 1);
  217. } else {
  218. $order_refund_history = array();
  219. $order_refund_history['order_id'] = $order_info['order_id'];
  220. $order_refund_history['order_goods_id'] = $order_goods_id;
  221. $order_refund_history['message'] = $res['err_code_des'];
  222. $order_refund_history['type'] = 2;
  223. $order_refund_history['addtime'] = time();
  224. M('lionfish_comshop_order_refund_history')->add($order_refund_history);
  225. /**
  226. M('lionfish_comshop_order')->where( array('order_id' => $order_info['order_id']) )->save( array('order_status_id' => 10, 'remarksaler' => $res[err_code_des]) );
  227. **/
  228. return array('code' => 0, 'msg' => $res['result_msg']);
  229. }
  230. }
  231. else if($order_info['payment_code'] == 'yuer')
  232. {
  233. //余额支付的,退款到余额
  234. //退款到余额
  235. //增加会员余额
  236. $refund_fee = $refund_fee / 100;
  237. if( $refund_fee > 0 )
  238. {
  239. //判断是否积分类型的
  240. M('lionfish_comshop_member')->where( array('member_id' => $order_info['member_id']) )->setInc('account_money',$refund_fee);
  241. $account_money_info = M('lionfish_comshop_member')->field('account_money')->where( array('member_id' =>$order_info['member_id'] ) )->find();
  242. $account_money = $account_money_info['account_money'];
  243. $member_charge_flow_data = array();
  244. $member_charge_flow_data['member_id'] = $order_info['member_id'];
  245. $member_charge_flow_data['money'] = $refund_fee;
  246. $member_charge_flow_data['operate_end_yuer'] = $account_money;
  247. $member_charge_flow_data['state'] = 4;
  248. $member_charge_flow_data['trans_id'] = $order_id;
  249. $member_charge_flow_data['order_goods_id'] = $order_goods_id;
  250. $member_charge_flow_data['charge_time'] = time();
  251. $member_charge_flow_data['add_time'] = time();
  252. M('lionfish_comshop_member_charge_flow')->add($member_charge_flow_data);
  253. }
  254. if($order_info['order_status_id'] == 12)
  255. {
  256. $ref_count = M('lionfish_comshop_order_refund')->where( array('order_id' => $order_info['order_id'], 'state' => 0 ) )->count();
  257. if( $ref_count <= 1 )
  258. {
  259. M('lionfish_comshop_order')->where( array('order_id' => $order_info['order_id']) )->save( array('order_status_id' => 7) );
  260. }
  261. }
  262. $order_goods_list = M('lionfish_comshop_order_goods')->where( array('order_id' => $order_info['order_id'] ) )->select();
  263. $goods_model = D('Home/Pingoods');
  264. foreach($order_goods_list as $order_goods)
  265. {
  266. //$goods_model->del_goods_mult_option_quantity($order_info['order_id'],$order_goods['rela_goodsoption_valueid'],$order_goods['goods_id'],$order_goods['quantity'],2);
  267. if( !empty($order_goods_id) && $order_goods_id > 0 )
  268. {
  269. if($order_goods_id == $order_goods['order_goods_id'] )
  270. {
  271. if($is_back_sellcount == 1)
  272. {
  273. if( $is_zi_order_refund == 1 && $refund_quantity > 0 )
  274. {
  275. $goods_model->del_goods_mult_option_quantity($order_info['order_id'],$order_goods['rela_goodsoption_valueid'],$order_goods['goods_id'],$refund_quantity,2);
  276. }else{
  277. //获取已退款数量
  278. $has_refund_quantity = M('lionfish_comshop_order_goods_refund')->where( array('order_id' => $order_info['order_id'], 'order_goods_id' =>$order_goods['order_goods_id']) )->sum('quantity');
  279. if(!$has_refund_quantity){
  280. $has_refund_quantity = 0;
  281. }
  282. $goods_model->del_goods_mult_option_quantity($order_info['order_id'],$order_goods['rela_goodsoption_valueid'],$order_goods['goods_id'],$order_goods['quantity']-$has_refund_quantity,2);
  283. }
  284. }
  285. $score_refund_info = M('lionfish_comshop_member_integral_flow')->where( array('order_goods_id' => $order_goods['order_goods_id'],'order_id' =>$order_info['order_id'],'type' => 'orderbuy' ) )->find();
  286. if( !empty($score_refund_info) && $is_zi_order_refund == 1 )
  287. {
  288. // D('Admin/Member')->sendMemberPointChange($order_info['member_id'],$score_refund_info['score'], 0 ,'退款增加积分', 'refundorder', $order_info['order_id'] ,$order_goods['order_goods_id'] );
  289. }
  290. }
  291. }else if( empty($order_goods_id) || $order_goods_id <=0 ){
  292. if($is_back_sellcount == 1){
  293. //获取已退款数量
  294. $has_refund_quantity = M('lionfish_comshop_order_goods_refund')->where( array('order_id' => $order_info['order_id'], 'order_goods_id' =>$order_goods['order_goods_id']) )->sum('quantity');
  295. if(!$has_refund_quantity){
  296. $has_refund_quantity = 0;
  297. }
  298. $goods_model->del_goods_mult_option_quantity($order_info['order_id'],$order_goods['rela_goodsoption_valueid'],$order_goods['goods_id'],$order_goods['quantity']-$has_refund_quantity,2);
  299. }
  300. $score_refund_info = M('lionfish_comshop_member_integral_flow')->where( array('order_goods_id' => $order_goods['order_goods_id'],'order_id' => $order_info['order_id'] ,'type' => 'orderbuy') )->find();
  301. if( !empty($score_refund_info) )
  302. {
  303. // D('Admin/Member')->sendMemberPointChange($order_info['member_id'],$score_refund_info['score'], 0 ,'退款增加积分','refundorder', $order_info['order_id'] ,$order_goods['order_goods_id'] );
  304. }
  305. }
  306. if( $order_info['type'] == 'integral' )
  307. {
  308. D('Admin/Member')->sendMemberPointChange($order_info['member_id'],$order_info['total'], 0 ,'退款增加积分', 'refundorder', $order_info['order_id'] ,$order_goods['order_goods_id'] );
  309. }
  310. }
  311. //分佣也要退回去
  312. if($is_zi_order_refund == 0)
  313. D('Seller/Community')->back_order_commission($order_info['order_id'],$order_goods_id);
  314. return array('code' => 1);
  315. //$this->refundOrder_success($order_info,$openId);
  316. //检测是否有需要退回积分的订单
  317. //货到付款订单
  318. }else if($order_info['payment_code'] == 'cashon_delivery')
  319. {
  320. //货到付款订单退款金额不到账,但有退款记录
  321. if($order_info['order_status_id'] == 12)
  322. {
  323. $ref_count = M('lionfish_comshop_order_refund')->where( array('order_id' => $order_info['order_id'], 'state' => 0 ) )->count();
  324. if( $ref_count <= 1 )
  325. {
  326. M('lionfish_comshop_order')->where( array('order_id' => $order_info['order_id']) )->save( array('order_status_id' => 7) );
  327. }
  328. }
  329. $order_info['total'] = $refund_fee / 100;
  330. $order_goods_list = M('lionfish_comshop_order_goods')->where( array('order_id' => $order_info['order_id'] ) )->select();
  331. $goods_model = D('Home/Pingoods');
  332. foreach($order_goods_list as $order_goods)
  333. {
  334. //$goods_model->del_goods_mult_option_quantity($order_info['order_id'],$order_goods['rela_goodsoption_valueid'],$order_goods['goods_id'],$order_goods['quantity'],2);
  335. if( !empty($order_goods_id) && $order_goods_id > 0 )
  336. {
  337. if($order_goods_id == $order_goods['order_goods_id'] )
  338. {
  339. if($is_back_sellcount == 1)
  340. {
  341. if( $is_zi_order_refund == 1 && $refund_quantity > 0 )
  342. {
  343. $goods_model->del_goods_mult_option_quantity($order_info['order_id'],$order_goods['rela_goodsoption_valueid'],$order_goods['goods_id'],$refund_quantity,2);
  344. }else{
  345. //获取已退款数量
  346. $has_refund_quantity = M('lionfish_comshop_order_goods_refund')->where( array('order_id' => $order_info['order_id'], 'order_goods_id' =>$order_goods['order_goods_id']) )->sum('quantity');
  347. if(!$has_refund_quantity){
  348. $has_refund_quantity = 0;
  349. }
  350. $goods_model->del_goods_mult_option_quantity($order_info['order_id'],$order_goods['rela_goodsoption_valueid'],$order_goods['goods_id'],$order_goods['quantity']-$has_refund_quantity,2);
  351. }
  352. }
  353. $score_refund_info = M('lionfish_comshop_member_integral_flow')->where( array('order_goods_id' => $order_goods['order_goods_id'],'order_id' =>$order_info['order_id'],'type' => 'orderbuy' ) )->find();
  354. if( !empty($score_refund_info) && $is_zi_order_refund == 1 )
  355. {
  356. // D('Admin/Member')->sendMemberPointChange($order_info['member_id'],$score_refund_info['score'], 0 ,'退款增加积分', 'refundorder', $order_info['order_id'] ,$order_goods['order_goods_id'] );
  357. }
  358. }
  359. }else if( empty($order_goods_id) || $order_goods_id <=0 ){
  360. if($is_back_sellcount == 1){
  361. //获取已退款数量
  362. $has_refund_quantity = M('lionfish_comshop_order_goods_refund')->where( array('order_id' => $order_info['order_id'], 'order_goods_id' =>$order_goods['order_goods_id']) )->sum('quantity');
  363. if(!$has_refund_quantity){
  364. $has_refund_quantity = 0;
  365. }
  366. $goods_model->del_goods_mult_option_quantity($order_info['order_id'],$order_goods['rela_goodsoption_valueid'],$order_goods['goods_id'],$order_goods['quantity']-$has_refund_quantity,2);
  367. }
  368. $score_refund_info = M('lionfish_comshop_member_integral_flow')->where( array('order_goods_id' => $order_goods['order_goods_id'],'order_id' => $order_info['order_id'] ,'type' => 'orderbuy') )->find();
  369. if( !empty($score_refund_info) )
  370. {
  371. // D('Admin/Member')->sendMemberPointChange($order_info['member_id'],$score_refund_info['score'], 0 ,'退款增加积分','refundorder', $order_info['order_id'] ,$order_goods['order_goods_id'] );
  372. }
  373. }
  374. /*if( $order_info['type'] == 'integral' )
  375. {
  376. D('Admin/Member')->sendMemberPointChange($order_info['member_id'],$order_info['total'], 0 ,'退款增加积分', 'refundorder', $order_info['order_id'] ,$order_goods['order_goods_id'] );
  377. }*/
  378. }
  379. //分佣也要退回去
  380. if($is_zi_order_refund == 0)
  381. D('Seller/Community')->back_order_commission($order_info['order_id'],$order_goods_id);
  382. return array('code' => 1);
  383. //$this->refundOrder_success($order_info,$openId);
  384. //检测是否有需要退回积分的订单
  385. }
  386. else if($order_info['payment_code'] == 'admin'){
  387. if($order_info['order_status_id'] == 12)
  388. {
  389. $ref_count = M('lionfish_comshop_order_refund')->where( array('order_id' => $order_info['order_id'], 'state' => 0 ) )->count();
  390. if( $ref_count <= 1 )
  391. {
  392. M('lionfish_comshop_order')->where( array('order_id' => $order_info['order_id']) )->save( array('order_status_id' => 7) );
  393. }
  394. }
  395. $order_info['total'] = $refund_fee / 100;
  396. $order_goods_list = M('lionfish_comshop_order_goods')->where( array('order_id' => $order_info['order_id'] ) )->select();
  397. $goods_model = D('Home/Pingoods');
  398. foreach($order_goods_list as $order_goods)
  399. {
  400. //$goods_model->del_goods_mult_option_quantity($order_info['order_id'],$order_goods['rela_goodsoption_valueid'],$order_goods['goods_id'],$order_goods['quantity'],2);
  401. if( !empty($order_goods_id) && $order_goods_id > 0 )
  402. {
  403. if($order_goods_id == $order_goods['order_goods_id'] )
  404. {
  405. if($is_back_sellcount == 1)
  406. {
  407. if( $is_zi_order_refund == 1 && $refund_quantity > 0 )
  408. {
  409. $goods_model->del_goods_mult_option_quantity($order_info['order_id'],$order_goods['rela_goodsoption_valueid'],$order_goods['goods_id'],$refund_quantity,2);
  410. }else{
  411. //获取已退款数量
  412. $has_refund_quantity = M('lionfish_comshop_order_goods_refund')->where( array('order_id' => $order_info['order_id'], 'order_goods_id' =>$order_goods['order_goods_id']) )->sum('quantity');
  413. if(!$has_refund_quantity){
  414. $has_refund_quantity = 0;
  415. }
  416. $goods_model->del_goods_mult_option_quantity($order_info['order_id'],$order_goods['rela_goodsoption_valueid'],$order_goods['goods_id'],$order_goods['quantity']-$has_refund_quantity,2);
  417. }
  418. }
  419. $score_refund_info = M('lionfish_comshop_member_integral_flow')->where( array('order_goods_id' => $order_goods['order_goods_id'],'order_id' =>$order_info['order_id'],'type' => 'orderbuy' ) )->find();
  420. if( !empty($score_refund_info) && $is_zi_order_refund == 1 )
  421. {
  422. //D('Admin/Member')->sendMemberPointChange($order_info['member_id'],$score_refund_info['score'], 0 ,'退款增加积分', 'refundorder', $order_info['order_id'] ,$order_goods['order_goods_id'] );
  423. }
  424. }
  425. }else if( empty($order_goods_id) || $order_goods_id <=0 ){
  426. if($is_back_sellcount == 1){
  427. //获取已退款数量
  428. $has_refund_quantity = M('lionfish_comshop_order_goods_refund')->where( array('order_id' => $order_info['order_id'], 'order_goods_id' =>$order_goods['order_goods_id']) )->sum('quantity');
  429. if(!$has_refund_quantity){
  430. $has_refund_quantity = 0;
  431. }
  432. $goods_model->del_goods_mult_option_quantity($order_info['order_id'],$order_goods['rela_goodsoption_valueid'],$order_goods['goods_id'],$order_goods['quantity']-$has_refund_quantity,2);
  433. }
  434. $score_refund_info = M('lionfish_comshop_member_integral_flow')->where( array('order_goods_id' => $order_goods['order_goods_id'],'order_id' => $order_info['order_id'] ,'type' => 'orderbuy') )->find();
  435. if( !empty($score_refund_info) )
  436. {
  437. // D('Admin/Member')->sendMemberPointChange($order_info['member_id'],$score_refund_info['score'], 0 ,'退款增加积分','refundorder', $order_info['order_id'] ,$order_goods['order_goods_id'] );
  438. }
  439. }
  440. if( $order_info['type'] == 'integral' )
  441. {
  442. //D('Admin/Member')->sendMemberPointChange($order_info['member_id'],$order_goods['total'], 0 ,'退款增加积分','refundorder', $order_info['order_id'] ,$order_goods['order_goods_id'] );
  443. }
  444. }
  445. //分佣也要退回去
  446. if($is_zi_order_refund == 0)
  447. D('Seller/Community')->back_order_commission($order_info['order_id'],$order_goods_id);
  448. return array('code' => 1);
  449. }
  450. else if($refund_fee == 0)
  451. {
  452. if($order_info['order_status_id'] == 12)
  453. {
  454. $ref_count = M('lionfish_comshop_order_refund')->where( array('order_id' => $order_info['order_id'], 'state' => 0 ) )->count();
  455. if( $ref_count <= 1 )
  456. {
  457. M('lionfish_comshop_order')->where( array('order_id' => $order_info['order_id']) )->save( array('order_status_id' => 7) );
  458. }
  459. }
  460. //ims_ lionfish_comshop_order_goods
  461. $order_goods = M('lionfish_comshop_order_goods')->where( array('order_id' => $order_info['order_id']) )->select();
  462. $order_goods_name = '';
  463. $order_goods_name_arr = array();
  464. $goods_model = D('Home/Pingoods');
  465. //get_config_by_name($name)
  466. foreach ($order_goods as $key => $value) {
  467. //($order_id,$option,$goods_id,$quantity,$type='1')
  468. if($is_back_sellcount == 1)
  469. {
  470. if( $is_zi_order_refund == 1 && $refund_quantity > 0 )
  471. {
  472. $goods_model->del_goods_mult_option_quantity($order_info['order_id'],$value['rela_goodsoption_valueid'],$value['goods_id'],$refund_quantity,2);
  473. }else{
  474. //获取已退款数量
  475. $has_refund_quantity = M('lionfish_comshop_order_goods_refund')->where( array('order_id' => $order_info['order_id'], 'order_goods_id' =>$order_goods['order_goods_id']) )->sum('quantity');
  476. if(!$has_refund_quantity){
  477. $has_refund_quantity = 0;
  478. }
  479. $goods_model->del_goods_mult_option_quantity($order_info['order_id'],$value['rela_goodsoption_valueid'],$value['goods_id'],$value['quantity']-$has_refund_quantity,2);
  480. }
  481. }
  482. $score_refund_info = M('lionfish_comshop_member_integral_flow')->where( array('order_id' =>$order_info['order_id'] ,'order_goods_id' =>$value['order_goods_id'] ,'type' => 'orderbuy') )->find();
  483. if( !empty($score_refund_info) )
  484. {
  485. // D('Admin/Member')->sendMemberPointChange($order_info['member_id'],$score_refund_info['score'], 0 ,'退款增加积分','refundorder', $order_info['order_id'] ,$value['order_goods_id'] );
  486. }
  487. //销量回退
  488. $order_goods_name_arr[] = $value['name'];
  489. }
  490. $order_goods_name = implode('\r\n', $order_goods_name_arr); //."\r\n";
  491. $msg = '订单退款: 您的订单'.$order_info['order_num_alias'].'参团未成功,现退款:'.round($order_info["total"],2).'元,商品名称:'.$order_goods_name;
  492. $url = D('Home/Front')->get_config_by_name('shop_domain');
  493. //weixin_template_refund_order
  494. //send_template_msg($wx_template_data,$url,$openid,C('weixin_template_refund_order'));
  495. /**
  496. {{first.DATA}}
  497. 订单编号:{{keyword1.DATA}}
  498. 退款金额:{{keyword2.DATA}}
  499. {{remark.DATA}}
  500. ---------------------------
  501. 校白君提醒您,您有一笔退款成功,请留意。
  502. 订单编号:20088115853
  503. 退款金额:¥19.00
  504. 更多学生价好货,在底部菜单栏哦~猛戳“校园专区”,享更多优惠!
  505. **/
  506. $wx_template_data = array();
  507. $wx_template_data['first'] = array('value' => '退款通知', 'color' => '#030303');
  508. $wx_template_data['keyword1'] = array('value' => $order_goods_name, 'color' => '#030303');
  509. $wx_template_data['keyword2'] = array('value' => round($order_info["total"],2), 'color' => '#030303');
  510. $wx_template_data['remark'] = array('value' => '拼团失败已按原路退款', 'color' => '#030303');
  511. if( $order_info['from_type'] == 'wepro' )
  512. {
  513. $template_data = array();
  514. $template_data['keyword1'] = array('value' => $order_info['order_num_alias'], 'color' => '#030303');
  515. $template_data['keyword2'] = array('value' => '商户名称', 'color' => '#030303');
  516. $template_data['keyword2'] = array('value' => $order_goods_name, 'color' => '#030303');
  517. $template_data['keyword3'] = array('value' => $order_info['total'].'元', 'color' => '#030303');
  518. $template_data['keyword4'] = array('value' => '已按原路退款', 'color' => '#030303');
  519. $template_data['keyword5'] = array('value' => $member_info['uname'], 'color' => '#030303');
  520. $template_id = D('Home/Front')->get_config_by_name('weprogram_template_refund_order');
  521. $pagepath = 'lionfish_comshop/pages/order/order?id='.$order_info['order_id'];
  522. $member_formid_info = M('lionfish_comshop_member_formid')->where("member_id=".$order_info['member_id']." and formid != '' and state =0")->order('id desc')->find();
  523. if(!empty( $member_formid_info ))
  524. {
  525. D('Seller/User')->send_wxtemplate_msg($template_data,$url,$pagepath,$member_info['we_openid'],$template_id,$member_formid_info['formid'] );
  526. M('lionfish_comshop_member_formid')->where( array('id' => $member_formid_info['id']) )->save(array('state' => 1));
  527. }
  528. if( $openid != '1')
  529. {
  530. //send_template_msg($wx_template_data,$url,$member_info['openid'],C('weixin_template_refund_order'));
  531. }
  532. }else{
  533. //send_template_msg($wx_template_data,$url,$member_info['openid'],C('weixin_template_refund_order'));
  534. }
  535. //检测是否有需要退回积分的订单
  536. }
  537. else if( isset($is_open_yinpay) && $is_open_yinpay == 3 )
  538. {
  539. $order_relate_info = M('lionfish_comshop_order_relate')->where( array('order_id' => $order_id ) )->order('id desc')->find();
  540. if( !empty($order_relate_info) && $order_relate_info['order_all_id'] > 0 )
  541. {
  542. $order_all_info = M('lionfish_comshop_order_all')->where( array('id' => $order_relate_info['order_all_id'] ) )->find();
  543. if( !empty($order_all_info) && !empty($order_all_info['out_trade_no']) )
  544. {
  545. $out_trade_no = $order_all_info['out_trade_no'];
  546. $appid = D('Home/Front')->get_config_by_name('wepro_fuwu_appid');
  547. $mch_id = D('Home/Front')->get_config_by_name('wepro_fuwu_partnerid');
  548. $nonce_str = nonce_str();
  549. $pay_key = D('Home/Front')->get_config_by_name('wepro_key');
  550. $sub_appid = D('Home/Front')->get_config_by_name('wepro_appid');
  551. $sub_mch_id = D('Home/Front')->get_config_by_name('wepro_sub_mch_id');
  552. $post = array();
  553. $post['appid'] = $appid;
  554. $post['mch_id'] = $mch_id;
  555. $post['nonce_str'] = $nonce_str;
  556. $post['out_trade_no'] = $out_trade_no;
  557. $post['sub_appid'] = $sub_appid;
  558. $post['sub_mch_id'] = $sub_mch_id;
  559. $sign = sign($post,$pay_key);
  560. $post_xml = '<xml>
  561. <appid>'.$appid.'</appid>
  562. <mch_id>'.$mch_id.'</mch_id>
  563. <nonce_str>'.$nonce_str.'</nonce_str>
  564. <out_trade_no>'.$out_trade_no.'</out_trade_no>
  565. <sub_appid>'.$post['sub_appid'].'</sub_appid>
  566. <sub_mch_id>'.$post['sub_mch_id'].'</sub_mch_id>
  567. <sign>'.$sign.'</sign>
  568. </xml>';
  569. $url = "https://api.mch.weixin.qq.com/pay/orderquery";
  570. $result = http_request($url,$post_xml);
  571. $array = xml($result);
  572. if( $array['RETURN_CODE'] == 'SUCCESS' && $array['RETURN_MSG'] == 'OK' )
  573. {
  574. if( $array['TRADE_STATE'] == 'SUCCESS' || $array['TRADE_STATE'] == 'REFUND' )
  575. {
  576. $total_fee = $array['TOTAL_FEE'];
  577. }
  578. }
  579. }
  580. }
  581. //
  582. $input = new \WxPayRefund();
  583. $input->SetTransaction_id($transaction_id);
  584. $input->SetTotal_fee($total_fee);
  585. $input->SetRefund_fee($refund_fee);
  586. $mchid = D('Home/Front')->get_config_by_name('wepro_partnerid');
  587. $refund_no = $mchid.date("YmdHis").$order_info['order_id'];
  588. $input->SetOut_refund_no($refund_no);
  589. $input->SetOp_user_id($mchid);
  590. $res = \WxPayApi::refund($input,6,'teweixin');
  591. if($res["return_code"] == 'FAIL'){
  592. return array('code' => 0, 'msg' => $res['return_msg']);
  593. }
  594. if( $res['err_code_des'] == '订单已全额退款' )
  595. {
  596. $res['result_code'] = 'SUCCESS';
  597. }
  598. if($res['result_code'] == 'SUCCESS')
  599. {
  600. if($order_info['order_status_id'] == 12)
  601. {
  602. $ref_count = M('lionfish_comshop_order_refund')->where( array('order_id' => $order_info['order_id'], 'state' => 0 ) )->count();
  603. if( $ref_count <= 1 )
  604. {
  605. M('lionfish_comshop_order')->where( array('order_id' => $order_info['order_id']) )->save( array('order_status_id' => 7) );
  606. }
  607. }
  608. $order_info['total'] = $refund_fee / 100;
  609. $order_goods_list = M('lionfish_comshop_order_goods')->where( array('order_id' => $order_info['order_id']) )->select();
  610. $order_goods_name_arr = array();
  611. $order_goods_name = '';
  612. foreach($order_goods_list as $order_goods)
  613. {
  614. $order_goods_name_arr[] = $order_goods['name'];
  615. //...
  616. if( !empty($order_goods_id) && $order_goods_id > 0 )
  617. {
  618. if($order_goods_id == $order_goods['order_goods_id'] )
  619. {
  620. if($is_back_sellcount == 1)
  621. {
  622. if( $is_zi_order_refund == 1 && $refund_quantity > 0 )
  623. {
  624. D('Home/Pingoods')->del_goods_mult_option_quantity($order_info['order_id'],$order_goods['rela_goodsoption_valueid'],$order_goods['goods_id'],$refund_quantity,2);
  625. }else{
  626. //获取已退款数量
  627. $has_refund_quantity = M('lionfish_comshop_order_goods_refund')->where( array('order_id' => $order_info['order_id'], 'order_goods_id' =>$order_goods['order_goods_id']) )->sum('quantity');
  628. if(!$has_refund_quantity){
  629. $has_refund_quantity = 0;
  630. }
  631. D('Home/Pingoods')->del_goods_mult_option_quantity($order_info['order_id'],$order_goods['rela_goodsoption_valueid'],$order_goods['goods_id'],$order_goods['quantity']-$has_refund_quantity,2);
  632. }
  633. }
  634. $score_refund_info = M('lionfish_comshop_member_integral_flow')->where( array('order_id' => $order_info['order_id'],'order_goods_id'=>$order_goods['order_goods_id'] ,'type' => 'orderbuy') )->find();
  635. if( !empty($score_refund_info) && $is_zi_order_refund == 1 )
  636. {
  637. // D('Admin/Member')->sendMemberPointChange($order_info['member_id'],$score_refund_info['score'], 0 ,'退款增加积分', 'refundorder', $order_info['order_id'] ,$order_goods['order_goods_id'] );
  638. }
  639. }
  640. }else if( empty($order_goods_id) || $order_goods_id <=0 ){
  641. if($is_back_sellcount == 1){
  642. $has_refund_quantity = M('lionfish_comshop_order_goods_refund')->where( array('order_id' => $order_info['order_id'], 'order_goods_id' =>$order_goods['order_goods_id']) )->sum('quantity');
  643. if(!$has_refund_quantity){
  644. $has_refund_quantity = 0;
  645. }
  646. D('Home/Pingoods')->del_goods_mult_option_quantity($order_info['order_id'],$order_goods['rela_goodsoption_valueid'],$order_goods['goods_id'],$order_goods['quantity']-$has_refund_quantity,2);
  647. }
  648. $score_refund_info = M('lionfish_comshop_member_integral_flow')->where( array('order_id' => $order_info['order_id'],'order_goods_id' =>$order_goods['order_goods_id'] ,'type' => 'orderbuy') )->find();
  649. if( !empty($score_refund_info) )
  650. {
  651. // D('Admin/Member')->sendMemberPointChange($order_info['member_id'],$score_refund_info['score'], 0 ,'退款增加积分', 'refundorder', $order_info['order_id'] ,$order_goods['order_goods_id'] );
  652. }
  653. }
  654. if( $order_info['type'] == 'integral' )
  655. {
  656. D('Admin/Member')->sendMemberPointChange($order_info['member_id'],$order_goods['total'], 0 ,'退款增加积分','refundorder', $order_info['order_id'] ,$order_goods['order_goods_id'] );
  657. }
  658. }
  659. $order_goods_name = implode('\r\n', $order_goods_name_arr); //."\r\n";
  660. //分佣也要退回去
  661. if($is_zi_order_refund == 0)
  662. D('Seller/Community')->back_order_commission($order_info['order_id'],$order_goods_id);
  663. return array('code' => 1);
  664. } else {
  665. $order_refund_history = array();
  666. $order_refund_history['order_id'] = $order_info['order_id'];
  667. $order_refund_history['order_goods_id'] = $order_goods_id;
  668. $order_refund_history['message'] = $res['err_code_des'];
  669. $order_refund_history['type'] = 2;
  670. $order_refund_history['addtime'] = time();
  671. M('lionfish_comshop_order_refund_history')->add($order_refund_history);
  672. /**
  673. M('lionfish_comshop_order')->where( array('order_id' => $order_info['order_id']) )->save( array('order_status_id' => 10, 'remarksaler' => $res[err_code_des]) );
  674. **/
  675. return array('code' => 0, 'msg' => $res['err_code_des']);
  676. }
  677. }
  678. else {
  679. $order_relate_info = M('lionfish_comshop_order_relate')->where( array('order_id' => $order_id ) )->order('id desc')->find();
  680. if( !empty($order_relate_info) && $order_relate_info['order_all_id'] > 0 )
  681. {
  682. $order_all_info = M('lionfish_comshop_order_all')->where( array('id' => $order_relate_info['order_all_id'] ) )->find();
  683. if( !empty($order_all_info) && !empty($order_all_info['out_trade_no']) )
  684. {
  685. $out_trade_no = $order_all_info['out_trade_no'];
  686. $appid = D('Home/Front')->get_config_by_name('wepro_appid');
  687. $mch_id = D('Home/Front')->get_config_by_name('wepro_partnerid');
  688. $nonce_str = nonce_str();
  689. $pay_key = D('Home/Front')->get_config_by_name('wepro_key');
  690. $post = array();
  691. $post['appid'] = $appid;
  692. $post['mch_id'] = $mch_id;
  693. $post['nonce_str'] = $nonce_str;
  694. $post['out_trade_no'] = $out_trade_no;
  695. $sign = sign($post,$pay_key);
  696. $post_xml = '<xml>
  697. <appid>'.$appid.'</appid>
  698. <mch_id>'.$mch_id.'</mch_id>
  699. <nonce_str>'.$nonce_str.'</nonce_str>
  700. <out_trade_no>'.$out_trade_no.'</out_trade_no>
  701. <sign>'.$sign.'</sign>
  702. </xml>';
  703. $url = "https://api.mch.weixin.qq.com/pay/orderquery";
  704. $result = http_request($url,$post_xml);
  705. $array = xml($result);
  706. if( $array['RETURN_CODE'] == 'SUCCESS' && $array['RETURN_MSG'] == 'OK' )
  707. {
  708. if( $array['TRADE_STATE'] == 'SUCCESS' || $array['TRADE_STATE'] == 'REFUND' )
  709. {
  710. $total_fee = $array['TOTAL_FEE'];
  711. }
  712. }
  713. }
  714. }
  715. //
  716. $input = new \WxPayRefund();
  717. $input->SetTransaction_id($transaction_id);
  718. $input->SetTotal_fee($total_fee);
  719. $input->SetRefund_fee($refund_fee);
  720. $mchid = D('Home/Front')->get_config_by_name('wepro_partnerid');
  721. $refund_no = $mchid.date("YmdHis").$order_info['order_id'];
  722. $input->SetOut_refund_no($refund_no);
  723. $input->SetOp_user_id($mchid);
  724. $res = \WxPayApi::refund($input,6,$order_info['from_type']);
  725. if($res["return_code"] == 'FAIL'){
  726. return array('code' => 0, 'msg' => $res['return_msg']);
  727. }
  728. if( $res['err_code_des'] == '订单已全额退款' )
  729. {
  730. $res['result_code'] = 'SUCCESS';
  731. }
  732. if($res['result_code'] == 'SUCCESS')
  733. {
  734. if($order_info['order_status_id'] == 12)
  735. {
  736. $ref_count = M('lionfish_comshop_order_refund')->where( array('order_id' => $order_info['order_id'], 'state' => 0 ) )->count();
  737. if( $ref_count <= 1 )
  738. {
  739. M('lionfish_comshop_order')->where( array('order_id' => $order_info['order_id']) )->save( array('order_status_id' => 7) );
  740. }
  741. }
  742. $order_info['total'] = $refund_fee / 100;
  743. $order_goods_list = M('lionfish_comshop_order_goods')->where( array('order_id' => $order_info['order_id']) )->select();
  744. $order_goods_name_arr = array();
  745. $order_goods_name = '';
  746. foreach($order_goods_list as $order_goods)
  747. {
  748. $order_goods_name_arr[] = $order_goods['name'];
  749. //...
  750. if( !empty($order_goods_id) && $order_goods_id > 0 )
  751. {
  752. if($order_goods_id == $order_goods['order_goods_id'] )
  753. {
  754. if($is_back_sellcount == 1)
  755. {
  756. if( $is_zi_order_refund == 1 && $refund_quantity > 0 )
  757. {
  758. D('Home/Pingoods')->del_goods_mult_option_quantity($order_info['order_id'],$order_goods['rela_goodsoption_valueid'],$order_goods['goods_id'],$refund_quantity,2);
  759. }else{
  760. //获取已退款数量
  761. $has_refund_quantity = M('lionfish_comshop_order_goods_refund')->where( array('order_id' => $order_info['order_id'], 'order_goods_id' =>$order_goods['order_goods_id']) )->sum('quantity');
  762. if(!$has_refund_quantity){
  763. $has_refund_quantity = 0;
  764. }
  765. D('Home/Pingoods')->del_goods_mult_option_quantity($order_info['order_id'],$order_goods['rela_goodsoption_valueid'],$order_goods['goods_id'],$order_goods['quantity']-$has_refund_quantity,2);
  766. }
  767. }
  768. $score_refund_info = M('lionfish_comshop_member_integral_flow')->where( array('order_id' => $order_info['order_id'],'order_goods_id'=>$order_goods['order_goods_id'] ,'type' => 'orderbuy') )->find();
  769. if( !empty($score_refund_info) && $is_zi_order_refund == 1 )
  770. {
  771. // D('Admin/Member')->sendMemberPointChange($order_info['member_id'],$score_refund_info['score'], 0 ,'退款增加积分', 'refundorder', $order_info['order_id'] ,$order_goods['order_goods_id'] );
  772. }
  773. }
  774. }else if( empty($order_goods_id) || $order_goods_id <=0 ){
  775. if($is_back_sellcount == 1){
  776. $has_refund_quantity = M('lionfish_comshop_order_goods_refund')->where( array('order_id' => $order_info['order_id'], 'order_goods_id' =>$order_goods['order_goods_id']) )->sum('quantity');
  777. if(!$has_refund_quantity){
  778. $has_refund_quantity = 0;
  779. }
  780. D('Home/Pingoods')->del_goods_mult_option_quantity($order_info['order_id'],$order_goods['rela_goodsoption_valueid'],$order_goods['goods_id'],$order_goods['quantity']-$has_refund_quantity,2);
  781. }
  782. $score_refund_info = M('lionfish_comshop_member_integral_flow')->where( array('order_id' => $order_info['order_id'],'order_goods_id' =>$order_goods['order_goods_id'] ,'type' => 'orderbuy') )->find();
  783. if( !empty($score_refund_info) )
  784. {
  785. // D('Admin/Member')->sendMemberPointChange($order_info['member_id'],$score_refund_info['score'], 0 ,'退款增加积分', 'refundorder', $order_info['order_id'] ,$order_goods['order_goods_id'] );
  786. }
  787. }
  788. if( $order_info['type'] == 'integral' )
  789. {
  790. D('Admin/Member')->sendMemberPointChange($order_info['member_id'],$order_goods['total'], 0 ,'退款增加积分','refundorder', $order_info['order_id'] ,$order_goods['order_goods_id'] );
  791. }
  792. }
  793. $order_goods_name = implode('\r\n', $order_goods_name_arr); //."\r\n";
  794. //分佣也要退回去
  795. if($is_zi_order_refund == 0)
  796. D('Seller/Community')->back_order_commission($order_info['order_id'],$order_goods_id);
  797. return array('code' => 1);
  798. } else {
  799. $order_refund_history = array();
  800. $order_refund_history['order_id'] = $order_info['order_id'];
  801. $order_refund_history['order_goods_id'] = $order_goods_id;
  802. $order_refund_history['message'] = $res['err_code_des'];
  803. $order_refund_history['type'] = 2;
  804. $order_refund_history['addtime'] = time();
  805. M('lionfish_comshop_order_refund_history')->add($order_refund_history);
  806. /**
  807. M('lionfish_comshop_order')->where( array('order_id' => $order_info['order_id']) )->save( array('order_status_id' => 10, 'remarksaler' => $res[err_code_des]) );
  808. **/
  809. return array('code' => 0, 'msg' => $res['err_code_des']);
  810. }
  811. }
  812. }
  813. public function refundOrder2($order_id,$money =0)
  814. {
  815. $lib_path = dirname(dirname( dirname(__FILE__) )).'/Lib/';
  816. $data_path = dirname( dirname(dirname( dirname(__FILE__) )) ).'/Data/wxpaylogs/'.date('Y-m-d')."/";
  817. RecursiveMkdir($data_path);
  818. set_time_limit(0);
  819. require_once $lib_path."/Weixin/lib/WxPay.Api.php";
  820. require_once $lib_path."/Weixin/log.php";
  821. //初始化日志
  822. $logHandler= new \CLogFileHandler( $data_path .date('Y-m-d').'.log');
  823. \Log::Init($logHandler, 15);
  824. \Log::DEBUG("进行中,订单ID :".$order_id );
  825. //pin
  826. $order_info = M('order')->where( array('order_id' => $order_id) )->find();
  827. $member_info = M('member')->where( array('member_id' => $order_info['member_id']) )->find();
  828. $openId = $member_info['openid'];
  829. $we_openid = $member_info['we_openid'];
  830. if( $order_info['from_type'] == 'wepro' )
  831. {
  832. $openId = $we_openid;
  833. }
  834. //we_openid
  835. //money
  836. $transaction_id = $order_info["transaction_id"];
  837. $total_fee = ($order_info["total"])*100;
  838. $refund_fee = $total_fee;
  839. if($money > 0)
  840. {
  841. $refund_fee = $money * 100;
  842. }
  843. if($order_info['payment_code'] == 'yuer')
  844. {
  845. //余额支付的,退款到余额
  846. //退款到余额
  847. $member_charge_flow_data = array();
  848. $member_charge_flow_data['member_id'] = $order_info['member_id'];
  849. $member_charge_flow_data['money'] = $order_info["total"];
  850. $member_charge_flow_data['state'] = 4;
  851. $member_charge_flow_data['trans_id'] = $order_id;
  852. $member_charge_flow_data['charge_time'] = time();
  853. $member_charge_flow_data['add_time'] = time();
  854. M('member_charge_flow')->add($member_charge_flow_data);
  855. //增加会员余额
  856. M('member')->where( array('member_id'=> $order_info['member_id'] ) )->setInc('account_money',$order_info["total"] );
  857. $order_info['total'] = $refund_fee / 100;
  858. $this->refundOrder_success($order_info,$openId);
  859. //检测是否有需要退回积分的订单
  860. }
  861. else if($refund_fee == 0)
  862. {
  863. M('order')->where( array('order_id' => $order_info['order_id']) )->save( array('order_status_id' => 7) );
  864. $config_info = M('config')->where( array('name' => 'SITE_URL') )->find();
  865. $order_goods = M('order_goods')->where( array('order_id' => $order_info['order_id']) )->select();
  866. $order_goods_name = '';
  867. $order_goods_name_arr = array();
  868. foreach ($order_goods as $key => $value) {
  869. $goods_model->del_goods_mult_option_quantity($order_info['order_id'],$value['rela_goodsoption_valueid'],$value['goods_id'],$value['quantity'],2);
  870. //销量回退
  871. //$this->execute("UPDATE " . C('DB_PREFIX') . "goods SET seller_count = (seller_count - " . (int)$value['quantity'] . ") WHERE goods_id = '" . $value['goods_id'] . "' ");
  872. $order_goods_name_arr[] = $value['name'];
  873. }
  874. $order_goods_name = implode('\r\n', $order_goods_name_arr); //."\r\n";
  875. $msg = '订单退款: 您的订单'.$order_info['order_num_alias'].'参团未成功,现退款:'.round($order_info["total"],2).'元,商品名称:'.$order_goods_name;
  876. $url = $config_info['value'];
  877. //weixin_template_refund_order
  878. //send_template_msg($wx_template_data,$url,$openid,C('weixin_template_refund_order'));
  879. $url = $url."/index.php?s=/Order/info/id/{$order_info['order_id']}.html";
  880. /**
  881. {{first.DATA}}
  882. 订单编号:{{keyword1.DATA}}
  883. 退款金额:{{keyword2.DATA}}
  884. {{remark.DATA}}
  885. ---------------------------
  886. 校白君提醒您,您有一笔退款成功,请留意。
  887. 订单编号:20088115853
  888. 退款金额:¥19.00
  889. 更多学生价好货,在底部菜单栏哦~猛戳“校园专区”,享更多优惠!
  890. **/
  891. $wx_template_data = array();
  892. $wx_template_data['first'] = array('value' => '退款通知', 'color' => '#030303');
  893. $wx_template_data['keyword1'] = array('value' => $order_goods_name, 'color' => '#030303');
  894. $wx_template_data['keyword2'] = array('value' => round($order_info["total"],2), 'color' => '#030303');
  895. $wx_template_data['remark'] = array('value' => '拼团失败已按原路退款', 'color' => '#030303');
  896. if( $order_info['from_type'] == 'wepro' )
  897. {
  898. $template_data = array();
  899. $template_data['keyword1'] = array('value' => $order_goods_name, 'color' => '#030303');
  900. $template_data['keyword2'] = array('value' => '参团未成功', 'color' => '#030303');
  901. $template_data['keyword3'] = array('value' => '拼团失败已按原路退款', 'color' => '#030303');
  902. $pay_order_msg_info = M('config')->where( array('name' => 'weprogram_template_fail_pin') )->find();
  903. $template_id = $pay_order_msg_info['value'];
  904. $pagepath = 'pages/order/order?id='.$order_info['order_id'];
  905. /**
  906. $member_formid_info = M('member_formid')->where( array('member_id' => $order_info['member_id'], 'state' => 0) )->find();
  907. send_wxtemplate_msg($template_data,$url,$pagepath,$member_info['we_openid'],$template_id,$member_formid_info['formid']);
  908. //更新
  909. M('member_formid')->where( array('id' => $member_formid_info['id']) )->save( array('state' => 1) );
  910. $order_info['member_id']
  911. **/
  912. $member_formid_info = M('member_formid')->where( array('member_id' => $order_info['member_id'], 'formid' =>array('neq',''), 'state' => 0) )->order('id desc')->find();
  913. if(!empty( $member_formid_info ))
  914. {
  915. send_wxtemplate_msg($template_data,$url,$pagepath,$member_info['we_openid'],$template_id,$member_formid_info['formid'] );
  916. M('member_formid')->where( array('id' => $member_formid_info['id']) )->save( array('state' => 1) );
  917. }
  918. if( $openid != '1')
  919. {
  920. //notify_weixin_msg($member_info['openid'],$msg,'退款通知',$url);
  921. send_template_msg($wx_template_data,$url,$member_info['openid'],C('weixin_template_refund_order'));
  922. }
  923. }else{
  924. //notify_weixin_msg($member_info['openid'],$msg,'退款通知',$url);
  925. send_template_msg($wx_template_data,$url,$member_info['openid'],C('weixin_template_refund_order'));
  926. }
  927. //检测是否有需要退回积分的订单
  928. \Log::DEBUG("退款成功。。。退款日志:退款订单号:" . $order_info['order_id'].',ten:'.$transaction_id.' 退款金额: '
  929. .$order_info["total"].',退款给:openid='.$openId);
  930. } else {
  931. $input = new \WxPayRefund();
  932. $input->SetTransaction_id($transaction_id);
  933. $input->SetTotal_fee($total_fee);
  934. $input->SetRefund_fee($refund_fee);
  935. $refund_no = \WxPayConfig::MCHID.date("YmdHis").$order_info['order_id'];
  936. $input->SetOut_refund_no($refund_no);
  937. $input->SetOp_user_id(\WxPayConfig::MCHID);
  938. $res = (\WxPayApi::refund($input,6,$order_info['from_type']));
  939. //var_dump($res);die(); wx80131aa7dfc4ff71
  940. if($res['result_code'] == 'SUCCESS')
  941. {
  942. $order_info['total'] = $refund_fee / 100;
  943. $this->refundOrder_success($order_info,$openId);
  944. \Log::DEBUG("退款成功。。。退款日志:退款订单号:" . $order_info['order_id'].',ten:'.$transaction_id.' 退款金额: '.$order_info["total"].',退款给:openid='.$openid);
  945. //检测是否有需要退回积分的订单
  946. } else {
  947. M('order')->where( array('order_id' => $order_info['order_id']) )->save( array('order_status_id' => 10, 'comment' => $res[err_code_des]) );
  948. \Log::DEBUG("退款失败。原因:{$res[err_code_des]}。。退款日志:退款订单号:" . $order_info['order_id'].',退款金额: '.$order_info["total"].',退款给:openid='.$openId);
  949. }
  950. }
  951. return true;
  952. }
  953. /**
  954. 取消已经付款的 待发货订单
  955. 5、处理订单,
  956. 6、处理退款,
  957. **/
  958. public function del_cancle_order($order_id)
  959. {
  960. $order_info = M('lionfish_comshop_order')->where( array('order_id' => $order_id ) )->find();
  961. //判断订单状态是否已付款,避免多次退款,不合理
  962. if( $order_info['order_status_id'] == 1 )
  963. {
  964. //del_cancle_order
  965. $total_money = D('Seller/Commonorder')->get_order_paymoney( $order_id );
  966. $has_refun_money = D('Seller/Commonorder')->order_refund_totalmoney( $order_id );
  967. //是否预售订单begin
  968. $presale_result = D('Home/PresaleGoods')->getOrderPresaleInfo( $order_id );
  969. $presale_info = [];
  970. if( $presale_result['code'] == 0 )
  971. {
  972. $presale_info = $presale_result['data'];
  973. $total_money = $total_money - $presale_info['presale_ding_money'];
  974. }
  975. //end
  976. $refund_money = round($total_money - $has_refun_money,2);
  977. $result = $this->refundOrder($order_id, $refund_money);
  978. if( $result['code'] == 1 )
  979. {
  980. $order_history = array();
  981. $order_history['order_id'] = $order_id;
  982. $order_history['order_status_id'] = 5;
  983. $order_history['notify'] = 0;
  984. $order_history['comment'] = '会员前台申请取消订单,取消成功,并退款。';
  985. $order_history['date_added'] = time();
  986. M('lionfish_comshop_order_history')->add( $order_history );
  987. M('lionfish_comshop_order')->where( array('order_id' => $order_id) )->save( array('order_status_id' => 5) );
  988. //整笔
  989. D('Seller/Commonorder')->refund_one_order( $order_id ,1);
  990. return array('code' => 0);
  991. }else{
  992. $order_history = array();
  993. $order_history['order_id'] = $order_id;
  994. $order_history['order_status_id'] = 10;
  995. $order_history['notify'] = 0;
  996. $order_history['comment'] = '申请取消订单,但是退款失败。';
  997. $order_history['date_added'] = time();
  998. M('lionfish_comshop_order_history')->add( $order_history );
  999. M('lionfish_comshop_order')->where( array('order_id' => $order_id ) )->save( array('order_status_id' => 10, 'remarksaler' => $result['msg']) );
  1000. return array('code' => 1, 'msg' => $result['msg'] ,'s' => $result['code'] );
  1001. }
  1002. }
  1003. //如果退款成功了。那么就进行
  1004. }
  1005. public function test_form_msg()
  1006. {
  1007. $member_info = M('member')->where( array('member_id' => 26) )->find();
  1008. $form_id_arr = M('member_formid')->where( array('member_id' => 26,'state' =>0) )->find();
  1009. M('member_formid')->where( array('id' => $form_id_arr['id'] ) )->save( array('state' =>1) );
  1010. $form_id = $form_id_arr['formid'];
  1011. $template_data = array();
  1012. $template_data['keyword1'] = array('value' => '338866', 'color' => '#030303');
  1013. $template_data['keyword2'] = array('value' => '商品名称', 'color' => '#030303');
  1014. $template_data['keyword3'] = array('value' => '18元', 'color' => '#030303');
  1015. $template_data['keyword4'] = array('value' => '已按原路退款', 'color' => '#030303');
  1016. $template_data['keyword5'] = array('value' => '小鱼', 'color' => '#030303');
  1017. $pay_order_msg_info = M('config')->where( array('name' => 'weprogram_template_refund_order') )->find();
  1018. $template_id = $pay_order_msg_info['value'];
  1019. $pagepath = 'pages/order/order?id='.$order_info['order_id'];
  1020. $rs = send_wxtemplate_msg($template_data,$url,$pagepath,$member_info['we_openid'],$template_id,$form_id);
  1021. var_dump($rs);die();
  1022. }
  1023. public function refundOrder_success($order_info,$openid)
  1024. {
  1025. M('order')->where( array('order_id' => $order_info['order_id']) )->save( array('order_status_id' => 7) );
  1026. $member_info = M('member')->where( array('member_id' => $order_info['member_id']) )->find();
  1027. $config_info = M('config')->where( array('name' => 'SITE_URL') )->find();
  1028. $order_goods = M('order_goods')->where( array('order_id' => $order_info['order_id']) )->select();
  1029. $goods_model = D('Home/Goods');
  1030. $order_goods_name = '';
  1031. $order_goods_name_arr = array();
  1032. foreach ($order_goods as $key => $value) {
  1033. //$this->execute("UPDATE " . C('DB_PREFIX') . "goods SET quantity = (quantity + " . (int)$value['quantity'] . ") WHERE goods_id = '" . $value['goods_id'] . "' ");
  1034. $goods_model->del_goods_mult_option_quantity($order_info['order_id'],$value['rela_goodsoption_valueid'],$value['goods_id'],$value['quantity'],2);
  1035. //销量回退
  1036. //$this->execute("UPDATE " . C('DB_PREFIX') . "goods SET seller_count = (seller_count - " . (int)$value['quantity'] . ") WHERE goods_id = '" . $value['goods_id'] . "' ");
  1037. $order_goods_name_arr[] = $value['name'];
  1038. }
  1039. $order_goods_name = implode('\r\n', $order_goods_name_arr); //."\r\n";
  1040. $msg = '订单退款: 您的订单'.$order_info['order_num_alias'].'参团未成功,现退款:'.round($order_info["total"],2).'元,商品名称:'.$order_goods_name;
  1041. $url = $config_info['value'];
  1042. $wx_template_data = array();
  1043. $wx_template_data['first'] = array('value' => '退款通知', 'color' => '#030303');
  1044. $wx_template_data['keyword1'] = array('value' => $order_goods_name, 'color' => '#030303');
  1045. $wx_template_data['keyword2'] = array('value' => $order_info['total'].'元', 'color' => '#030303');
  1046. $wx_template_data['remark'] = array('value' => '已按原路退款', 'color' => '#030303');
  1047. $url = $url."/index.php?s=/Order/info/id/{$order_info['order_id']}.html";
  1048. if( $order_info['from_type'] == 'wepro' )
  1049. {
  1050. /**
  1051. 退款成功通知
  1052. 关键词
  1053. 订单号
  1054. {{keyword1.DATA}}
  1055. 商品名称
  1056. {{keyword2.DATA}}
  1057. 退款金额
  1058. {{keyword3.DATA}}
  1059. 温馨提示
  1060. {{keyword4.DATA}}
  1061. 备注
  1062. {{keyword5.DATA}}
  1063. **/
  1064. //$total_money = ($order_info["total"],2);
  1065. $template_data = array();
  1066. $template_data['keyword1'] = array('value' => $order_info['order_num_alias'], 'color' => '#030303');
  1067. $template_data['keyword2'] = array('value' => $order_goods_name, 'color' => '#030303');
  1068. $template_data['keyword3'] = array('value' => $order_info['total'].'元', 'color' => '#030303');
  1069. $template_data['keyword4'] = array('value' => '已按原路退款', 'color' => '#030303');
  1070. $template_data['keyword5'] = array('value' => $member_info['uname'], 'color' => '#030303');
  1071. $pay_order_msg_info = M('config')->where( array('name' => 'weprogram_template_refund_order') )->find();
  1072. $template_id = $pay_order_msg_info['value'];
  1073. $pagepath = 'pages/order/order?id='.$order_info['order_id'];
  1074. /**
  1075. $member_formid_info = M('member_formid')->where( array('member_id' => $order_info['member_id'], 'state' => 0) )->find();
  1076. send_wxtemplate_msg($template_data,$url,$pagepath,$member_info['we_openid'],$template_id,$member_formid_info['formid']);
  1077. //更新
  1078. M('member_formid')->where( array('id' => $member_formid_info['id']) )->save( array('state' => 1) );
  1079. **/
  1080. $member_formid_info = M('member_formid')->where( array('member_id' => $order_info['member_id'],'formid' =>array('neq',''), 'state' => 0) )->order('id desc')->find();
  1081. //$order_info['member_id']
  1082. if( !empty($member_formid_info) )
  1083. {
  1084. $rs = send_wxtemplate_msg($template_data,$url,$pagepath,$member_info['we_openid'],$template_id,$member_formid_info['formid']);
  1085. M('member_formid')->where( array('id' => $member_formid_info['id']) )->save( array('state' => 1) );
  1086. }
  1087. if( $openid != '1')
  1088. {
  1089. //notify_weixin_msg($openid,$msg,'退款通知',$url);
  1090. send_template_msg($wx_template_data,$url,$openid,C('weixin_template_refund_order'));
  1091. }
  1092. }else{
  1093. //notify_weixin_msg($openid,$msg,'退款通知',$url);
  1094. send_template_msg($wx_template_data,$url,$openid,C('weixin_template_refund_order'));
  1095. }
  1096. }
  1097. }
  1098. ?>