WeixinModel.class.php 53 KB

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