Няма описание
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793
  1. <?php
  2. /**
  3. * 易优CMS
  4. * ============================================================================
  5. * 版权所有 2016-2028 海口快推科技有限公司,并保留所有权利。
  6. * 网站地址: http://www.eyoucms.com
  7. * ----------------------------------------------------------------------------
  8. * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
  9. * ============================================================================
  10. * Author: 陈风任 <491085389@qq.com>
  11. * Date: 2019-2-25
  12. */
  13. namespace app\user\controller;
  14. use think\Db;
  15. // use think\Session;
  16. use think\Config;
  17. use think\Page;
  18. use app\user\logic\PayLogic;
  19. use think\Cookie;
  20. class Pay extends Base
  21. {
  22. public $php_version = '';
  23. public function _initialize() {
  24. parent::_initialize();
  25. $this->users_db = Db::name('users'); // 会员数据表
  26. $this->users_money_db = Db::name('users_money');// 会员金额明细表
  27. $this->shop_order_db = Db::name('shop_order'); // 订单主表
  28. $this->shop_order_details_db = Db::name('shop_order_details'); // 订单明细表
  29. // 判断PHP版本信息
  30. if (version_compare(PHP_VERSION,'5.5.0','<')) {
  31. $this->php_version = 1; // PHP5.5.0以下版本,可使用旧版支付方式
  32. }else{
  33. $this->php_version = 0;// PHP5.5.0以上版本,可使用新版支付方式,兼容旧版支付方式
  34. }
  35. // 支付功能是否开启
  36. $redirect_url = '';
  37. $pay_open = $this->usersConfig['pay_open'];
  38. $web_users_switch = tpCache('web.web_users_switch');
  39. if (empty($pay_open)) {
  40. // 支付功能关闭,立马跳到会员中心
  41. $redirect_url = url('user/Users/index');
  42. $msg = '支付功能尚未开启!';
  43. } else if (empty($web_users_switch)) {
  44. // 前台会员中心已关闭,跳到首页
  45. $redirect_url = ROOT_DIR.'/';
  46. $msg = '会员中心尚未开启!';
  47. }
  48. if (!empty($redirect_url)) {
  49. Db::name('users_menu')->where([
  50. 'mca' => 'user/Pay/pay_consumer_details',
  51. 'lang' => $this->home_lang,
  52. ])->update([
  53. 'status' => 0,
  54. 'update_time' => getTime(),
  55. ]);
  56. $this->error($msg, $redirect_url);
  57. exit;
  58. }
  59. // --end
  60. }
  61. // 消费明细
  62. public function pay_consumer_details()
  63. {
  64. // 订单超过 get_order_validity 设定的时间,则修改订单为已取消状态,无需返回数据
  65. // model('Pay')->UpdateOrderData($this->users_id);
  66. // 获取金额明细状态
  67. $pay_status_arr = Config::get('global.pay_status_arr');
  68. // 获取金额明细类型
  69. $pay_cause_type_arr = Config::get('global.pay_cause_type_arr');
  70. // 只读取已付款或已完成订单信息
  71. $condition = [];
  72. array_push($condition, "a.lang = '".$this->home_lang.'\'');
  73. array_push($condition, "a.users_id = ".$this->users_id);
  74. array_push($condition, "a.status IN (2, 3)");
  75. // $increase_type = [0, 3, 5, 6];
  76. $increase_type = [3, 5, 6];
  77. $decrease_type = [1, 2, 4, 7];
  78. $queryID = input('param.queryID/d', 0);
  79. if (!empty($queryID)) {
  80. if (in_array($queryID, [10, 20])) {
  81. if (10 === intval($queryID)) {
  82. $cause_type = implode(',', $increase_type);
  83. array_push($condition, "(a.cause_type IN ({$cause_type}) OR (a.cause_type = 0 AND a.pay_method ='balance'))");
  84. } else {
  85. $cause_type = implode(',', $decrease_type);
  86. array_push($condition, "a.cause_type IN ({$cause_type})");
  87. }
  88. }
  89. else if (in_array($queryID, [30, 40, 50, 60])) {
  90. array_push($condition, "a.admin_id = 0");
  91. switch (intval($queryID)) {
  92. case 30:
  93. $queryName = 'shop_order';
  94. break;
  95. case 40:
  96. $queryName = 'article_order';
  97. break;
  98. case 50:
  99. $queryName = 'media_order';
  100. break;
  101. case 60:
  102. $queryName = 'download_order';
  103. break;
  104. }
  105. $where = [
  106. 'pay_name' => 'balance',
  107. 'users_id' => $this->users_id
  108. ];
  109. $orderCodes = Db::name($queryName)->where($where)->column('order_code');
  110. $orderCodes = !empty($orderCodes) ? implode(',', $orderCodes) : '\'\'';
  111. array_push($condition, "a.order_number IN ({$orderCodes})");
  112. }
  113. else if (in_array($queryID, [70])) {
  114. array_push($condition, "a.admin_id > 0");
  115. }
  116. } else {
  117. array_push($condition, "(a.cause_type != 0 OR (a.cause_type = 0 AND a.pay_method ='balance'))");
  118. }
  119. $this->assign('queryID', $queryID);
  120. $this->assign('increase_type', $increase_type);
  121. $this->assign('decrease_type', $decrease_type);
  122. $where_str = "";
  123. if (0 < count($condition)) $where_str = implode(" AND ", $condition);
  124. $count = $this->users_money_db->alias('a')->where($where_str)->count();// 查询满足要求的总记录数
  125. $Page = new Page($count, config('paginate.list_rows'));// 实例化分页类 传入总记录数和每页显示的记录数
  126. $list = $this->users_money_db->field('a.*')
  127. ->alias('a')
  128. ->where($where_str)
  129. ->order('a.add_time desc, a.moneyid desc')
  130. ->limit($Page->firstRow.','.$Page->listRows)
  131. ->select();
  132. foreach ($list as $key => $val) {
  133. $val['money'] = floatval($val['money']);
  134. $val['users_money'] = floatval($val['users_money']);
  135. $val['status_name'] = !empty($pay_status_arr[$val['status']]) ? $pay_status_arr[$val['status']] : '';
  136. if (3 === intval($val['cause_type'])) {
  137. $causeValue = !empty($val['cause']) ? explode(',', $val['cause']) : [];
  138. $val['cause_type_name'] = !empty($causeValue[0]) ? str_replace('购买', '订单', $causeValue[0]) : '';
  139. } else {
  140. $val['cause_type_name'] = !empty($pay_cause_type_arr[$val['cause_type']]) ? $pay_cause_type_arr[$val['cause_type']] : '';
  141. }
  142. if (in_array($val['cause_type'], [4, 5]) && empty($val['order_number'])) {
  143. $val['order_number'] = date('Ymd') . $val['add_time'];
  144. }
  145. $list[$key] = $val;
  146. }
  147. $show = $Page->show();// 分页显示输出
  148. $this->assign('page', $show);// 赋值分页输出
  149. $this->assign('list', $list);// 赋值数据集
  150. $this->assign('pager', $Page);// 赋值分页集
  151. $this->assign('pay_status_arr', $pay_status_arr);
  152. $this->assign('pay_cause_type_arr', $pay_cause_type_arr);
  153. $result = [];
  154. // 菜单名称
  155. $result['title'] = Db::name('users_menu')->where([
  156. 'mca' => 'user/Pay/pay_consumer_details',
  157. 'lang' => $this->home_lang,
  158. ])->getField('title');
  159. $eyou = array(
  160. 'field' => $result,
  161. );
  162. $this->assign('eyou', $eyou);
  163. /*第三套模板,将充值功能加入明细页面*/
  164. $money = input('param.money/f');
  165. $this->assign('money', $money);
  166. $unified_number = input('param.unified_number/s');
  167. $this->assign('unified_number', $unified_number);
  168. /*end*/
  169. return $this->fetch('users/pay_consumer_details');
  170. }
  171. // 账户充值
  172. public function pay_account_recharge()
  173. {
  174. if (IS_AJAX_POST) {
  175. $where = [
  176. 'status' => 1,
  177. 'pay_info' => ['NEQ', '']
  178. ];
  179. $PayApiList = Db::name('pay_api_config')->where($where)->select();
  180. $is_open_wechat = 1;
  181. if (!empty($PayApiList)) {
  182. foreach ($PayApiList as $key => $value) {
  183. if (!empty($value['pay_info'])) {
  184. if ('wechat' == $value['pay_mark']) $is_open_wechat = 0;
  185. $PayInfo = unserialize($value['pay_info']);
  186. if ('wechat' == $value['pay_mark']) {
  187. if (0 == $PayInfo['is_open_wechat']) {
  188. if (empty($PayInfo['appid']) || empty($PayInfo['mchid']) || empty($PayInfo['key'])) {
  189. $is_open_wechat = 1;
  190. unset($PayApiList[$key]);
  191. }
  192. } else {
  193. $is_open_wechat = 1;
  194. unset($PayApiList[$key]);
  195. }
  196. } else if ('alipay' == $value['pay_mark']) {
  197. if (0 == $PayInfo['is_open_alipay']) {
  198. if (version_compare(PHP_VERSION,'5.5.0','<')) {
  199. // 旧版支付宝
  200. if (empty($PayInfo['account']) || empty($PayInfo['code']) || empty($PayInfo['id'])) {
  201. unset($PayApiList[$key]);
  202. }
  203. } else {
  204. if (1 == $PayInfo['version']) {
  205. // 旧版支付宝
  206. if (empty($PayInfo['account']) || empty($PayInfo['code']) || empty($PayInfo['id'])) {
  207. unset($PayApiList[$key]);
  208. }
  209. } else {
  210. // 新版支付宝
  211. if (empty($PayInfo['app_id']) || empty($PayInfo['merchant_private_key']) || empty($PayInfo['alipay_public_key'])) {
  212. unset($PayApiList[$key]);
  213. }
  214. }
  215. }
  216. } else {
  217. unset($PayApiList[$key]);
  218. }
  219. } else if (0 == $value['system_built']) {
  220. if (0 == $PayInfo['is_open_pay']) {
  221. $is_set = false;
  222. foreach ($PayInfo as $kk => $vv) {
  223. if ((stristr($kk, 'appid') || stristr($kk, 'appsecret')) && !empty($vv)) {
  224. $is_set = true;
  225. break;
  226. }
  227. }
  228. if (false === $is_set) {
  229. unset($PayApiList[$key]);
  230. }
  231. } else {
  232. unset($PayApiList[$key]);
  233. }
  234. if (!empty($PayApiList[$key])) {
  235. $PayApiList[$key]['pay_img'] = get_default_pic('/weapp/'.$value['pay_mark'].'/pay.png');
  236. }
  237. }
  238. } else {
  239. unset($PayApiList[$key]);
  240. }
  241. }
  242. if (empty($PayApiList)) $this->error('网站支付配置未完善,充值服务暂停使用');
  243. } else {
  244. $this->error('网站支付配置未完善,充值服务暂停使用');
  245. }
  246. // 判断传入的数据
  247. $money = input('post.money/f');
  248. $unified_number = input('post.unified_number/s', '');
  249. if (!empty($unified_number) && !preg_match('/^\d+$/',$unified_number)) $this->error('订单号不存在!');
  250. // 判断不为空和数字字符串
  251. if (!empty($money) && is_numeric($money)) {
  252. $moneyRow = [];
  253. if (!empty($unified_number)) {
  254. $where = [
  255. 'lang' => $this->home_lang,
  256. 'status' => 1,
  257. 'order_number' => $unified_number
  258. ];
  259. $moneyRow = $this->users_money_db->where($where)->find();
  260. }
  261. $time = getTime();
  262. if (!empty($moneyRow['moneyid'])) {
  263. // 更改充值金额
  264. $moneyid = !empty($moneyRow['moneyid']) ? intval($moneyRow['moneyid']) : 0;
  265. $order_number = date('Ymd') . $time . rand(10, 100);
  266. $order_number_old = !empty($moneyRow['order_number']) ? $moneyRow['order_number'] : '';
  267. $where = [
  268. 'moneyid' => $moneyid,
  269. 'users_id' => $this->users_id,
  270. 'order_number' => $order_number_old,
  271. ];
  272. $data = [
  273. 'money' => $money,
  274. 'users_money' => $this->users['users_money'] + $money,
  275. 'order_number' => $order_number,
  276. 'status' => 1,
  277. 'add_time' => $time,
  278. 'update_time' => $time
  279. ];
  280. $this->users_money_db->where($where)->update($data);
  281. } else {
  282. // 数据添加到订单表
  283. $cause_type = 1;
  284. $order_number = date('Ymd') . $time . rand(10,100); //订单生成规则
  285. $pay_cause_type_arr = Config::get('global.pay_cause_type_arr');
  286. $data = [
  287. 'users_id' => $this->users_id,
  288. 'cause_type' => $cause_type,
  289. 'cause' => $pay_cause_type_arr[$cause_type],
  290. 'money' => $money,
  291. 'users_money' => $this->users['users_money'] + $money,
  292. 'pay_details' => '',
  293. 'order_number' => $order_number,
  294. 'status' => 1,
  295. 'lang' => $this->home_lang,
  296. 'add_time' => $time
  297. ];
  298. if (isMobile() && isWeixin()) {
  299. $data['pay_method'] = 'wechat';// 如果在微信端中则默认为微信支付
  300. $data['wechat_pay_type'] = 'WeChatInternal';// 如果在微信端中则默认为微信端调起支付
  301. }
  302. $moneyid = $this->users_money_db->add($data);
  303. }
  304. // 添加状态
  305. if (!empty($moneyid)) {
  306. if (isMobile() && isWeixin() && 0 == $is_open_wechat) {
  307. $ReturnOrderData = [
  308. 'unified_id' => $moneyid,
  309. 'unified_number' => $order_number,
  310. 'transaction_type' => 1, // 订单支付金额充值
  311. 'is_gourl' => 0
  312. ];
  313. $this->success('等待支付', null, $ReturnOrderData);
  314. } else {
  315. $payment_type = input('post.payment_type/s');
  316. // 第二、第三、第四版会员中心充值处理
  317. if (in_array($this->usersTplVersion, ['v2', 'v3', 'v4']) && !empty($payment_type)) {
  318. $payment_type_arr = explode('_', $payment_type);
  319. $pay_mark = !empty($payment_type_arr[1]) ? $payment_type_arr[1] : '';
  320. $payApiRow = Db::name('pay_api_config')->where(['pay_mark'=>$pay_mark,'lang'=>$this->home_lang])->find();
  321. if (empty($payApiRow)) {
  322. $this->error('请选择正确的支付方式!');
  323. }
  324. $data = [
  325. 'pay_id' => $payApiRow['pay_id'],
  326. 'pay_mark' => $pay_mark,
  327. 'unified_id' => $moneyid,
  328. 'unified_number' => $order_number,
  329. 'transaction_type' => 1,
  330. ];
  331. $this->success('正在支付中', url('user/Pay/pay_consumer_details'), $data);
  332. }
  333. // 第一版会员中心充值处理
  334. else {
  335. $paydata = [
  336. 'moneyid' => $moneyid,
  337. 'order_number' => $order_number,
  338. ];
  339. // 先 json_encode 后 md5 加密信息
  340. $paystr = md5(json_encode($paydata));
  341. // 清除之前的 cookie
  342. Cookie::delete($paystr);
  343. // 存入 cookie
  344. cookie($paystr, $paydata);
  345. // 跳转链接
  346. $url = urldecode(url('user/Pay/pay_recharge_detail', ['paystr'=>$paystr]));
  347. $this->success('等待支付', $url, ['is_gourl'=>1]);
  348. }
  349. }
  350. }
  351. $this->error('充值表单提交失败');
  352. }
  353. $this->error('请输入正确的充值金额!');
  354. }
  355. $money = input('param.money/f');
  356. $this->assign('money', $money);
  357. $unified_number = input('param.unified_number/s', '');
  358. if (empty($unified_number)) {
  359. $where = [
  360. 'status' => 1,
  361. 'cause_type' => 1,
  362. 'users_id' => $this->users_id
  363. ];
  364. $unified_number = $this->users_money_db->where($where)->order('moneyid desc')->getField('order_number');
  365. }
  366. $this->assign('unified_number', $unified_number);
  367. return $this->fetch('users/pay_account_recharge');
  368. }
  369. // 充值详情
  370. public function pay_recharge_detail()
  371. {
  372. // 接收数据读取解析
  373. $Paystr = input('param.paystr/s');
  374. $PayData = cookie($Paystr);
  375. if (!empty($PayData['moneyid']) && !empty($PayData['order_number'])) {
  376. // 充值信息
  377. $moneyid = !empty($PayData['moneyid']) ? intval($PayData['moneyid']) : 0;
  378. $order_number = !empty($PayData['order_number']) ? $PayData['order_number'] : '';
  379. } else if (!empty($PayData['order_id']) && !empty($PayData['order_code'])) {
  380. // 订单信息
  381. $order_id = !empty($PayData['order_id']) ? intval($PayData['order_id']) : 0;
  382. $order_code = !empty($PayData['order_code']) ? $PayData['order_code'] : '';
  383. } else {
  384. $this->error('订单不存在或已变更', url('user/Shop/shop_centre'));
  385. }
  386. // 处理数据
  387. if (is_array($PayData) && (!empty($order_id) || !empty($moneyid)) && (!empty($order_number) || !empty($order_code))) {
  388. $data = [];
  389. if (!empty($moneyid)) {
  390. // 获取会员充值信息
  391. $where = [
  392. 'moneyid' => $moneyid,
  393. 'order_number' => $order_number,
  394. 'users_id' => $this->users_id,
  395. 'lang' => $this->home_lang
  396. ];
  397. $data = $this->users_money_db->where($where)->find();
  398. if (empty($data)) $this->error('订单不存在或已变更', url('user/Pay/pay_consumer_details'));
  399. // 组装数据返回
  400. $data['transaction_type'] = 1; // 交易类型,1为充值
  401. $data['unified_id'] = $data['moneyid'];
  402. $data['unified_amount'] = $data['money'];
  403. $data['unified_number'] = $data['order_number'];
  404. $data['cause'] = '余额充值';
  405. $data['shop_open_offline'] = !empty($this->usersConfig['shop_open_offline']) ? intval($this->usersConfig['shop_open_offline']) : 0;
  406. $this->assign('data', $data);
  407. } else if (!empty($order_id)) {
  408. /*余额开关*/
  409. $pay_balance_open = $this->usersConfig['pay_balance_open'];
  410. if (!is_numeric($pay_balance_open) && empty($pay_balance_open)) {
  411. $pay_balance_open = 1;
  412. }
  413. /*end*/
  414. if (!empty($PayData['type']) && 8 == $PayData['type']) {
  415. // 获取支付订单
  416. $where = [
  417. 'order_id' => $order_id,
  418. 'order_code' => $order_code,
  419. 'users_id' => $this->users_id,
  420. 'lang' => $this->home_lang
  421. ];
  422. $data = Db::name('media_order')->where($where)->find();
  423. if (empty($data)) $this->error('订单不存在或已变更', url('user/Media/index'));
  424. $url = url('user/Media/index');
  425. if (in_array($data['order_status'], [1])) $this->error('订单已支付,即将跳转!', $url);
  426. // 组装数据返回
  427. $data['transaction_type'] = 8; // 交易类型,8为购买视频
  428. $data['unified_id'] = $data['order_id'];
  429. $data['unified_amount'] = floatval($data['order_amount']);
  430. $data['unified_number'] = $data['order_code'];
  431. $data['cause'] = '购买视频';
  432. $data['pay_balance_open'] = $pay_balance_open;
  433. $data['shop_open_offline'] = !empty($this->usersConfig['shop_open_offline']) ? intval($this->usersConfig['shop_open_offline']) : 0;
  434. $this->assign('data', $data);
  435. } else if(!empty($PayData['type']) && 9 == $PayData['type']) {
  436. //文章支付订单
  437. $where = [
  438. 'order_id' => $order_id,
  439. 'order_code' => $order_code,
  440. 'users_id' => $this->users_id,
  441. 'lang' => $this->home_lang
  442. ];
  443. $data = Db::name('article_order')->where($where)->find();
  444. if (empty($data)) $this->error('订单不存在或已变更', url('user/Article/index'));
  445. $url = url('user/Article/index');
  446. if (in_array($data['order_status'], [1])) $this->error('订单已支付,即将跳转!', $url);
  447. // 组装数据返回
  448. $data['transaction_type'] = 9; // 交易类型,9为购买文章
  449. $data['unified_id'] = $data['order_id'];
  450. $data['unified_amount'] = floatval($data['order_amount']);
  451. $data['unified_number'] = $data['order_code'];
  452. $data['cause'] = $data['product_name'];
  453. $data['pay_balance_open'] = $pay_balance_open;
  454. $data['shop_open_offline'] = !empty($this->usersConfig['shop_open_offline']) ? intval($this->usersConfig['shop_open_offline']) : 0;
  455. $this->assign('data', $data);
  456. }else if(!empty($PayData['type']) && 10 == $PayData['type']) {
  457. //下载模型支付订单
  458. $where = [
  459. 'order_id' => $order_id,
  460. 'order_code' => $order_code,
  461. 'users_id' => $this->users_id,
  462. 'lang' => $this->home_lang
  463. ];
  464. $data = Db::name('download_order')->where($where)->find();
  465. if (empty($data)) $this->error('订单不存在或已变更', url('user/Download/index'));
  466. $url = url('user/Download/index');
  467. if (in_array($data['order_status'], [1])) $this->error('订单已支付,即将跳转!', $url);
  468. // 组装数据返回
  469. $data['transaction_type'] = 10; // 交易类型,10为购买下载模型
  470. $data['unified_id'] = $data['order_id'];
  471. $data['unified_amount'] = floatval($data['order_amount']);
  472. $data['unified_number'] = $data['order_code'];
  473. $data['cause'] = $data['product_name'];
  474. $data['pay_balance_open'] = $pay_balance_open;
  475. $data['shop_open_offline'] = !empty($this->usersConfig['shop_open_offline']) ? intval($this->usersConfig['shop_open_offline']) : 0;
  476. $this->assign('data', $data);
  477. } else {
  478. // 获取支付订单
  479. $where = [
  480. 'order_id' => $order_id,
  481. 'order_code' => $order_code,
  482. 'users_id' => $this->users_id,
  483. 'lang' => $this->home_lang
  484. ];
  485. $data = $this->shop_order_db->where($where)->find();
  486. if (empty($data)) $this->error('订单不存在或已变更', url('user/Shop/shop_centre'));
  487. // 非法提交,请正规合法提交订单-订单列表点击立即支付时检测判断-订单主表判断
  488. if (empty($data['order_total_num'])) $this->error('非法提交,请正规合法提交订单,非法代码:403');
  489. // 判断订单状态,1已付款(待发货),2已发货(待收货),3已完成(确认收货),-1订单取消(已关闭),4订单过期
  490. $url = urldecode(url('user/Shop/shop_order_details', ['order_id' => $data['order_id']]));
  491. if (in_array($data['order_status'], [1, 2, 3])) {
  492. $this->error('订单已支付,即将跳转!', $url);
  493. } elseif ($data['order_status'] == 4) {
  494. $this->error('订单已过期,即将跳转!', $url);
  495. } elseif ($data['order_status'] == -1) {
  496. $this->error('订单已关闭,即将跳转!', $url);
  497. }
  498. // 组装数据返回
  499. $data['transaction_type'] = 2; // 交易类型,2为购买
  500. $data['unified_id'] = $data['order_id'];
  501. $data['unified_amount'] = floatval($data['order_amount']);
  502. $data['unified_number'] = $data['order_code'];
  503. $data['cause'] = '购买商品';
  504. $data['pay_balance_open'] = $pay_balance_open;
  505. $data['province'] = get_province_name($data['province']);
  506. $data['city'] = get_city_name($data['city']);
  507. $data['district'] = get_area_name($data['district']);
  508. $data['addressInfo'] = $data['province'].' '.$data['city'].' '.$data['district'].' '.$data['address'];
  509. // 订单关闭倒计时处理
  510. $data['paymentExpire'] = 0;
  511. $data['eyCountdownTimes'] = 'eyCountdownTimes';
  512. if (isset($data['order_status']) && 0 === intval($data['order_status'])) {
  513. // 查询 自动关闭未付款订单 时长
  514. $orderUnpayCloseTime = $this->usersConfig['order_unpay_close_time'];
  515. if (!empty($orderUnpayCloseTime)) {
  516. // 未付款过期时间
  517. $data['paymentExpire'] = $data['add_time'] + (intval($orderUnpayCloseTime) * 60) - getTime();
  518. }
  519. }
  520. // 规格处理
  521. $order_details = Db::name('shop_order_details')->where('order_id',$data['order_id'])->order('details_id asc')->select();
  522. foreach ($order_details as $key => $val) {
  523. // 非法提交,请正规合法提交订单-订单列表点击立即支付时检测判断-订单副表判断
  524. if (empty($val['num'])) $this->error('非法提交,请正规合法提交订单,非法代码:404');
  525. $product_spec = unserialize($val['data']);
  526. $order_details[$key]['pointsGoodsBuyField'] = !empty($product_spec['pointsGoodsBuyField']) ? json_decode($product_spec['pointsGoodsBuyField'], true) : [];
  527. if (!empty($product_spec['spec_value_id'])) {
  528. $spec_value_id = explode('_', $product_spec['spec_value_id']);
  529. if (!empty($spec_value_id)) {
  530. $product_spec_list = [];
  531. $SpecWhere = [
  532. 'aid' => $val['product_id'],
  533. 'spec_value_id' => ['IN',$spec_value_id]
  534. ];
  535. $ProductSpecData = Db::name("product_spec_data")->where($SpecWhere)->field('spec_name, spec_value')->select();
  536. foreach ($ProductSpecData as $spec_value) {
  537. $product_spec_list[] = [
  538. 'name' => $spec_value['spec_name'],
  539. 'value' => $spec_value['spec_value'],
  540. ];
  541. }
  542. $order_details[$key]['product_spec_list'] = $product_spec_list;
  543. }
  544. }
  545. }
  546. $data['order_details'] = $order_details;
  547. // 支付参数
  548. $data['PromType'] = $data['prom_type'];
  549. $data['shop_open_offline'] = !empty($this->usersConfig['shop_open_offline']) ? intval($this->usersConfig['shop_open_offline']) : 0;
  550. $payApiHidden = model('ShopPublicHandle')->getPayApiHidden($data);
  551. $data['use_pay_type'] = $payApiHidden['usePayType'];
  552. $data['payTypeHidden'] = $payApiHidden['payTypeHidden'];
  553. // 存在积分商城订单则执行
  554. if (!empty($data['points_shop_order'])) {
  555. $weappInfo = model('ShopPublicHandle')->getWeappPointsShop();
  556. if (!empty($weappInfo)) {
  557. $list = !empty($data) ? $data : [];
  558. $list['Details'] = !empty($data['order_details']) ? $data['order_details'] : [];
  559. $pointsShopLogic = new \weapp\PointsShop\logic\PointsShopLogic();
  560. $pointsShopLogic->pointsShopOrderDataHandle([$list], $data, $data['order_details']);
  561. $data['order_details'] = !empty($data['Details']) ? $data['Details'] : $data['order_details'];
  562. }
  563. }
  564. // 仅到店核销
  565. $data['onlyVerify'] = !empty($data['logistics_type']) && 2 === intval($data['logistics_type']) ? true : false;
  566. // 仅物流配送
  567. $data['onlyDelivery'] = !empty($data['logistics_type']) && 1 === intval($data['logistics_type']) ? true : false;
  568. $this->assign('data', $data);
  569. }
  570. }
  571. if (!empty($PayData['type']) && 9 === intval($PayData['type']) && isMobile()) {
  572. return $this->fetch('system/article_pay');
  573. }else{
  574. return $this->fetch('users/pay_recharge_detail');
  575. }
  576. }
  577. $this->error('参数错误!');
  578. }
  579. // 支付宝订单查询
  580. public function get_alipay_order($Order = [], $Config = [])
  581. {
  582. if (IS_AJAX_POST || (!empty($Order) && !empty($Config))) {
  583. $unified_id = input('post.unified_id/d') ? input('post.unified_id/d') : $Order['unified_id'];
  584. $unified_number = input('post.unified_number/s') ? input('post.unified_number/s') : $Order['unified_number'];
  585. $transaction_type = input('post.transaction_type/d') ? input('post.transaction_type/d') : $Order['transaction_type'];
  586. //判断openssl是否开启
  587. $openssl_funcs = get_extension_funcs('openssl');
  588. if(empty($openssl_funcs)) $this->error('尚未开启php的openssl扩展');
  589. // 获取支付宝配置信息
  590. if (!empty($Config)) {
  591. $pay_alipay_config = $Config;
  592. } else {
  593. $where = [
  594. 'pay_id' => 2,
  595. 'pay_mark' => 'alipay'
  596. ];
  597. $pay_alipay_config = Db::name('pay_api_config')->where($where)->getField('pay_info');
  598. if (empty($pay_alipay_config)) return false;
  599. $pay_alipay_config = unserialize($pay_alipay_config);
  600. }
  601. if (empty($pay_alipay_config)) $this->error('尚未配置支付宝支付配置');
  602. // 订单号是否存在
  603. $where = [
  604. 'order_id' => $unified_id,
  605. 'order_code' => $unified_number
  606. ];
  607. $Result = $this->shop_order_db->where($where)->field('order_id, pay_name, order_status')->find();
  608. if (empty($Result)) $this->error('订单信息错误,请尝试刷新');
  609. // 获取支付宝配置信息
  610. if (1 == $pay_alipay_config['version']) {
  611. if (0 == $Result['order_status']) {
  612. if (!empty($Config)) {
  613. $this->success('订单正在支付');
  614. } else {
  615. $this->error('订单正在支付');
  616. }
  617. } else {
  618. // 已支付完成则执行
  619. $ResultUrl = urldecode(url('user/Pay/pay_success', ['transaction_type'=>$transaction_type, 'order_code'=>$unified_number]));
  620. $ResultData = [
  621. 'email' => false,
  622. 'mobile' => false
  623. ];
  624. if (2 == $transaction_type) {
  625. /*订单提醒*/
  626. // 邮箱发送
  627. $SmtpConfig = tpCache('smtp');
  628. $ResultData['email'] = GetEamilSendData($SmtpConfig, $this->users, $where, 1, 'alipay');
  629. /* END */
  630. /*手机发送*/
  631. $SmsConfig = tpCache('sms');
  632. $ResultData['mobile'] = GetMobileSendData($SmsConfig, $this->users, $where, 1, 'alipay');
  633. /* END */
  634. }
  635. $this->success('支付完成', $ResultUrl, $ResultData);
  636. }
  637. }
  638. // 引入文件
  639. vendor('alipay.pagepay.service.AlipayTradeService');
  640. vendor('alipay.pagepay.buildermodel.AlipayTradeQueryContentBuilder');
  641. // 实例化加载订单号
  642. $RequestBuilder = new \AlipayTradeQueryContentBuilder;
  643. $out_trade_no = trim($unified_number);
  644. $RequestBuilder->setOutTradeNo($out_trade_no);
  645. // 拼装配置
  646. $config['app_id'] = $pay_alipay_config['app_id'];
  647. $config['merchant_private_key'] = $pay_alipay_config['merchant_private_key'];
  648. $config['charset'] = 'UTF-8';
  649. $config['sign_type'] = 'RSA2';
  650. $config['gatewayUrl'] = 'https://openapi.alipay.com/gateway.do';
  651. $config['alipay_public_key'] = $pay_alipay_config['alipay_public_key'];
  652. // 实例化支付宝配置
  653. $aop = new \AlipayTradeService($config);
  654. $result = $aop->Query($RequestBuilder);
  655. // 解析数据
  656. $result = json_decode(json_encode($result), true);
  657. // 判断结果
  658. if ('40004' == $result['code'] && 'Business Failed' === $result['msg']) {
  659. if (!empty($Config)) {
  660. $this->success('正在建立订单信息');
  661. } else {
  662. $this->error('正在建立订单信息');
  663. }
  664. } else if ('10000' == $result['code'] && 'WAIT_BUYER_PAY' === $result['trade_status']) {
  665. if (!empty($Config)) {
  666. $this->success('订单已建立,尚未支付');
  667. } else {
  668. $this->error('订单已建立,尚未支付');
  669. }
  670. } else if ('10000' == $result['code'] && 'TRADE_SUCCESS' === $result['trade_status']) {
  671. $ResultUrl = urldecode(url('user/Pay/pay_success', ['transaction_type'=>$transaction_type, 'order_code'=>$unified_number]));
  672. $ResultData = [
  673. 'email' => false,
  674. 'mobile' => false
  675. ];
  676. if (2 == $transaction_type) {
  677. /*订单提醒*/
  678. // 邮箱发送
  679. $SmtpConfig = tpCache('smtp');
  680. $ResultData['email'] = GetEamilSendData($SmtpConfig, $this->users, $where, 1, 'alipay');
  681. /* END */
  682. /*手机发送*/
  683. $SmsConfig = tpCache('sms');
  684. $ResultData['mobile'] = GetMobileSendData($SmsConfig, $this->users, $where, 1, 'alipay');
  685. /* END */
  686. }
  687. $this->success('支付完成', $ResultUrl, $ResultData);
  688. }
  689. }
  690. }
  691. public function get_order_detail()
  692. {
  693. if (IS_AJAX_POST) {
  694. // 订单号
  695. $unified_number = input('post.unified_number/s');
  696. $unified_id = input('post.unified_id/d');
  697. $transaction_type = input('post.transaction_type/d');
  698. // 跳转链接
  699. $url = urldecode(url('user/Pay/pay_success', ['transaction_type'=>$transaction_type]));
  700. if ('2' == $transaction_type) {
  701. // 购买订单
  702. // 查询条件
  703. $OrderWhere = array(
  704. 'order_id' => $unified_id,
  705. 'order_code' => $unified_number,
  706. 'users_id' => $this->users_id,
  707. 'lang' => $this->home_lang,
  708. );
  709. $OrderRow = $this->shop_order_db->where($OrderWhere)->field('order_status,pay_name')->find();
  710. if (!empty($OrderRow)) {
  711. // 判断返回
  712. if ('alipay' == $OrderRow['pay_name'] && in_array($OrderRow['order_status'], [1])) {
  713. $this->success('订单已在支付宝付款完成!即将跳转~~~', $url);
  714. }else if ('wechat' == $OrderRow['pay_name'] && in_array($OrderRow['order_status'], [1])) {
  715. $this->success('订单已在微信付款完成!即将跳转~~~', $url);
  716. }else if ('balance' == $OrderRow['pay_name'] && in_array($OrderRow['order_status'], [1])) {
  717. // $this->success('订单已使用余额支付完成!即将跳转~~~', $url);
  718. }else{
  719. $this->error('等待支付');
  720. }
  721. }
  722. }else if ('1' == $transaction_type) {
  723. // 充值订单
  724. // 查询条件
  725. $where = array(
  726. 'moneyid' => $unified_id,
  727. 'order_number' => $unified_number,
  728. 'users_id' => $this->users_id,
  729. 'lang' => $this->home_lang,
  730. );
  731. $moneyRow = $this->users_money_db->where($where)->field('status,pay_method')->find();
  732. if (!empty($moneyRow)) {
  733. // 判断返回
  734. if ('alipay' == $moneyRow['pay_method'] && in_array($moneyRow['status'], [2,3])) {
  735. $this->success('订单已在支付宝付款完成!即将跳转~~~', $url);
  736. }else if ('wechat' == $moneyRow['pay_method'] && in_array($moneyRow['status'], [2,3])) {
  737. $this->success('订单已在微信付款完成!即将跳转~~~', $url);
  738. }else if ('artificial' == $moneyRow['pay_method'] && in_array($moneyRow['status'], [2,3])) {
  739. $this->success('订单已人为处理完成!即将跳转~~~', $url);
  740. }else{
  741. $this->error('等待支付');
  742. }
  743. }
  744. }
  745. }
  746. $this->error('访问错误');
  747. }
  748. // 选择付款方式,目前用于微信,支付宝方式已直接调用链接
  749. public function pay_method()
  750. {
  751. // 付款方式,跳转至微信支付还是支付宝支付。
  752. // $pay_method = input('param.pay_method/s');
  753. // 订单交易类型
  754. $transaction_type = input('param.transaction_type/s');
  755. // 订单号
  756. $unified_number = input('param.unified_number/s');
  757. // 订单ID
  758. $unified_id = input('param.unified_id/d');
  759. // 升级会员支付
  760. $level_pay = input('get.level_pay/d');
  761. $WeChatUrl = '';
  762. if (isset($level_pay) && !empty($level_pay)) {
  763. // 生成回调URL
  764. $WeChatUrl = url('user/Level/wechat_order_inquiry',['_ajax'=>1]);
  765. }
  766. $this->assign('WeChatUrl',$WeChatUrl);
  767. $this->assign('unified_number',$unified_number);
  768. $this->assign('transaction_type',$transaction_type);
  769. // 执行跳转
  770. return $this->fetch('users/pay_wechat');
  771. }
  772. // 微信支付,获取订单信息并调用微信接口,生成二维码用于扫码支付
  773. public function pay_wechat_png()
  774. {
  775. $users_id = session('users_id');
  776. if (!empty($users_id)) {
  777. $unified_number = input('param.unified_number/s');
  778. $transaction_type = input('param.transaction_type/s');
  779. if (2 == $transaction_type) {
  780. // 购买订单
  781. $where = array(
  782. 'users_id' => $users_id,
  783. 'order_code' => $unified_number,
  784. );
  785. $data = $this->shop_order_db->where($where)->find();
  786. $out_trade_no = $data['order_code'];
  787. $total_fee = $data['order_amount'];
  788. } else if (1 == $transaction_type) {
  789. // 充值订单
  790. $where = array(
  791. 'users_id' => $users_id,
  792. 'order_number' => $unified_number,
  793. );
  794. $data = $this->users_money_db->where($where)->find();
  795. $out_trade_no = $data['order_number'];
  796. $total_fee = $data['money'];
  797. }
  798. // 调取微信支付链接
  799. $payUrl = model('PayApi')->payForQrcode($out_trade_no, $total_fee, $transaction_type);
  800. // 生成二维码加载在页面上
  801. vendor('wechatpay.phpqrcode.phpqrcode');
  802. $qrcode = new \QRcode;
  803. $pngurl = $payUrl;
  804. $qrcode->png($pngurl);
  805. exit();
  806. } else {
  807. $this->redirect('user/Users/login');
  808. }
  809. }
  810. // ajax异步查询订单状态,轮询方式(微信)
  811. public function pay_deal_with()
  812. {
  813. if (IS_AJAX_POST) {
  814. $unified_number = input('post.unified_number/s');
  815. $transaction_type = input('post.transaction_type/d');
  816. if(!empty($unified_number)){
  817. // ajax异步查询订单是否完成并处理相应逻辑返回。
  818. vendor('wechatpay.lib.WxPayApi');
  819. vendor('wechatpay.lib.WxPayConfig');
  820. // 实例化加载订单号
  821. $input = new \WxPayOrderQuery;
  822. $input->SetOut_trade_no($unified_number);
  823. // 处理微信配置数据
  824. $where = [
  825. 'pay_id' => 1,
  826. 'pay_mark' => 'wechat'
  827. ];
  828. $pay_wechat_config = Db::name('pay_api_config')->where($where)->getField('pay_info');
  829. if (empty($pay_wechat_config)) return false;
  830. $pay_wechat_config = unserialize($pay_wechat_config);
  831. $config_data['app_id'] = $pay_wechat_config['appid'];
  832. $config_data['mch_id'] = $pay_wechat_config['mchid'];
  833. $config_data['key'] = $pay_wechat_config['key'];
  834. // 实例化微信配置
  835. $config = new \WxPayConfig($config_data);
  836. $wxpayapi = new \WxPayApi;
  837. if (empty($config->app_id)) $this->error('微信支付配置尚未配置完成。');
  838. // 返回结果
  839. $result = $wxpayapi->orderQuery($config, $input);
  840. // 业务处理
  841. if (isset($result['return_code']) && $result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS'){
  842. if ($result['trade_state'] == 'SUCCESS' && !empty($result['transaction_id'])) {
  843. if (2 == $transaction_type) { // 产品购买订单
  844. if (empty($order_data['order_status'])) {
  845. $returnData = pay_success_logic($this->users_id, $result['out_trade_no'], $result, 'wechat');
  846. if (is_array($returnData)) {
  847. if (1 == $returnData['code']) {
  848. // 订单支付完成
  849. if (isMobile() && isWeixin()) {
  850. $url = url('user/Shop/shop_centre');
  851. }else{
  852. $url = urldecode(url('user/Pay/pay_success', ['transaction_type'=>$transaction_type]));
  853. }
  854. $returnData['data']['status'] = 1;
  855. $this->success('支付成功,即将跳转~~~', $url, $returnData['data']);
  856. } else {
  857. $this->error($returnData['msg']);
  858. }
  859. }
  860. }
  861. if ($order_data['order_status'] == 1 && !empty($order_data['pay_details'])) {
  862. // 订单已付款
  863. if (isMobile() && isWeixin()) {
  864. $url = url('user/Shop/shop_centre');
  865. }else{
  866. $url = urldecode(url('user/Pay/pay_success', ['transaction_type'=>$transaction_type]));
  867. }
  868. $this->success('支付成功,即将跳转~~~', $url, ['status'=>1]);
  869. }
  870. if ($order_data['order_status'] == 3) {
  871. // 订单已完成,待处理逻辑
  872. // 待处理逻辑..........
  873. }
  874. if ($order_data['order_status'] == 4) {
  875. // 订单已取消,待处理逻辑
  876. // 待处理逻辑..........
  877. }
  878. }else if (1 == $transaction_type) { // 充值订单
  879. // 付款成功
  880. $moneydata = $this->users_money_db->where([
  881. 'order_number' => $result['out_trade_no'],
  882. 'users_id' => $this->users_id,
  883. 'lang' => $this->home_lang,
  884. ])->find();
  885. if (empty($moneydata)) {
  886. $this->error('支付异常,请刷新页面后重试');
  887. }
  888. // 微信付款成功后,订单并未修改状态时,修改订单状态并返回
  889. if ($moneydata['status'] == 1) {
  890. // 修改会员金额明细表中,对应的订单数据,存入返回的数据,订单已付款
  891. $data = [
  892. 'status' => 2,
  893. // 'pay_method' => 'wechat', //微信支付
  894. 'pay_details' => serialize($result),
  895. 'update_time' => getTime(),
  896. ];
  897. $ismoney = $this->users_money_db->where([
  898. 'moneyid' => $moneydata['moneyid'],
  899. 'users_id' => $this->users_id,
  900. ])->update($data);
  901. if (!empty($ismoney)) {
  902. // 同步修改会员的金额
  903. $usersdata = [
  904. 'users_money' => Db::raw('users_money+'.($moneydata['money'])),
  905. ];
  906. $isusers = $this->users_db->where([
  907. 'users_id' => $this->users_id,
  908. ])->update($usersdata);
  909. if (!empty($isusers)) {
  910. // 业务处理完成,订单已完成
  911. $data2 = [
  912. 'status' => 3,
  913. 'update_time' => getTime(),
  914. ];
  915. $this->users_money_db->where([
  916. 'moneyid' => $moneydata['moneyid'],
  917. 'users_id' => $this->users_id,
  918. ])->update($data2);
  919. if (isMobile() && isWeixin()) {
  920. $url = url('user/Pay/pay_consumer_details');
  921. }else{
  922. $url = urldecode(url('user/Pay/pay_success', ['transaction_type'=>$transaction_type]));
  923. }
  924. $this->success('充值成功,即将跳转~~~', $url, ['status'=>1]);
  925. }else{
  926. $this->success('付款成功,但未充值成功,请联系管理员。', null, ['status'=>2]);
  927. }
  928. }else{
  929. $this->success('付款成功,数据错误,未能充值成功,请联系管理员。', null, ['status'=>2]);
  930. }
  931. }
  932. if ($moneydata['status'] == 2 && !empty($moneydata['pay_details'])) {
  933. // 订单已付款
  934. if (isMobile() && isWeixin()) {
  935. $url = url('user/Pay/pay_consumer_details');
  936. }else{
  937. $url = urldecode(url('user/Pay/pay_success', ['transaction_type'=>$transaction_type]));
  938. }
  939. $this->success('充值成功,即将跳转~~~', $url, ['status'=>1]);
  940. }
  941. if ($moneydata['status'] == 3) {
  942. // 订单已完成,待处理逻辑
  943. // 待处理逻辑..........
  944. }
  945. if ($moneydata['status'] == 4) {
  946. // 订单已取消,待处理逻辑
  947. // 待处理逻辑..........
  948. }
  949. }
  950. }else if ($result['trade_state'] == 'NOTPAY') {
  951. // 付款中
  952. $this->success('正在付款中~~~~', '', ['status'=>0]);
  953. }
  954. }else{
  955. $msg = '订单号:'.$unified_number.',正在付款中~~~~~';
  956. $this->error($msg, null, ['status'=>0]);
  957. }
  958. }
  959. }
  960. $this->error('访问错误');
  961. }
  962. // 微信支付成功后跳转到此页面
  963. public function pay_success()
  964. {
  965. $transaction_type = input('param.transaction_type/d');
  966. if (1 == $transaction_type) {
  967. $url = urldecode(url('user/Pay/pay_consumer_details'));
  968. }else if (2 == $transaction_type) {
  969. $url = urldecode(url('user/Shop/shop_centre'));
  970. }
  971. $this->assign('url',$url);
  972. return $this->fetch('users/pay_success');
  973. }
  974. // 新版支付宝支付
  975. public function newAlipayPayUrl()
  976. {
  977. $data['unified_number'] = input('param.unified_number/s');
  978. $data['unified_amount'] = input('param.unified_amount/f');
  979. $data['transaction_type'] = input('param.transaction_type/d');
  980. /*校验支付金额与订单金额是否相符合,避免被钻空子 start*/
  981. $payLogicObj = new PayLogic();
  982. $OrderData = $payLogicObj->checkAmount($data['unified_number'],$data['unified_amount'],$data['transaction_type']);
  983. if(empty($OrderData)){
  984. $this->error("支付失败,支付金额与订单金额不相符");
  985. }
  986. /*校验支付金额与订单金额是否相符合,避免被钻空子 end*/
  987. // 调用新版支付宝支付方法
  988. $Result = model('PayApi')->getNewAliPayPayUrl($data);
  989. if (!empty($Result)) $this->error($Result);
  990. }
  991. // 支付宝回调接口,处理订单数据
  992. public function alipay_return()
  993. {
  994. // 跳转处理回调信息
  995. $pay_logic = new PayLogic();
  996. $result = $pay_logic->alipay_return();
  997. if (!empty($result['code']) && 1 == $result['code']) {
  998. $this->redirect($result['url']);
  999. }else{
  1000. $msg = !empty($result['msg']) ? $result['msg'] : '数据出错';
  1001. $this->error($msg);
  1002. }
  1003. }
  1004. // 余额支付
  1005. public function balance_payment()
  1006. {
  1007. if (IS_AJAX_POST) {
  1008. $post = input('post.');
  1009. $post['unified_id'] = intval($post['unified_id']);
  1010. $Data = $this->shop_order_db->field('order_code,order_amount,order_id,order_status')->where([
  1011. 'order_id' => $post['unified_id'],
  1012. 'order_code' => $post['unified_number'],
  1013. ])->find();
  1014. if (empty($Data)) $this->error('订单不存在或已变更', url('user/Shop/shop_centre'));
  1015. //1已付款(待发货),2已发货(待收货),3已完成(确认收货),-1订单取消(已关闭),4订单过期
  1016. $url = urldecode(url('user/Shop/shop_order_details', ['order_id' => $Data['order_id']]));
  1017. if (in_array($Data['order_status'], [1,2,3])) {
  1018. $this->success('订单已支付!即将跳转~~~', $url);
  1019. } elseif ($Data['order_status'] == 4) {
  1020. $this->success('订单已过期!即将跳转~~~', $url);
  1021. } elseif ($Data['order_status'] == -1) {
  1022. $this->success('订单已关闭!即将跳转~~~', $url);
  1023. }
  1024. if ($this->users['users_money'] >= $Data['order_amount']) {
  1025. $Where = [
  1026. 'users_id' => $this->users_id,
  1027. 'lang' => $this->home_lang,
  1028. ];
  1029. $post['payment_amount'] = $Data['order_amount'];
  1030. $post['payment_type'] = '余额支付';
  1031. $OrderData = [
  1032. 'order_status' => 1,
  1033. 'pay_name' => 'balance',// 余额支付
  1034. 'wechat_pay_type' => '', // 余额支付则清空微信标志
  1035. 'pay_details' => serialize($post),
  1036. 'pay_time' => getTime(),
  1037. 'update_time' => getTime(),
  1038. ];
  1039. $OrderWhere = [
  1040. 'order_id' => $Data['order_id'],
  1041. 'order_code' => $Data['order_code'],
  1042. ];
  1043. $OrderWhere = array_merge($Where, $OrderWhere);
  1044. $return = $this->shop_order_db->where($OrderWhere)->update($OrderData);
  1045. if (!empty($return)) {
  1046. $DetailsWhere = [
  1047. 'order_id' => $Data['order_id'],
  1048. ];
  1049. $DetailsWhere = array_merge($Where, $DetailsWhere);
  1050. $DetailsData['update_time'] = getTime();
  1051. $this->shop_order_details_db->where($DetailsWhere)->update($DetailsData);
  1052. $UsersData = [
  1053. 'users_money' => $this->users['users_money'] - $Data['order_amount'],
  1054. 'update_time' => getTime(),
  1055. ];
  1056. $users_id = $this->users_db->where($Where)->update($UsersData);
  1057. if (!empty($users_id)) {
  1058. // 添加订单操作记录
  1059. AddOrderAction($Data['order_id'],$this->users_id,'0','1','0','1','支付成功!','会员使用余额完成支付!');
  1060. // 虚拟自动发货
  1061. model('Pay')->afterVirtualProductPay($DetailsWhere);
  1062. // 邮箱发送
  1063. $SmtpConfig = tpCache('smtp');
  1064. $ResultData['email'] = GetEamilSendData($SmtpConfig, $this->users, $OrderWhere, 1, 'balance');
  1065. // 手机发送
  1066. $SmsConfig = tpCache('sms');
  1067. $ResultData['mobile'] = GetMobileSendData($SmsConfig, $this->users, $OrderWhere, 1, 'wechat');
  1068. if (isMobile() && isWeixin()) {
  1069. $url = url('user/Shop/shop_centre');
  1070. }else{
  1071. $url = urldecode(url('user/Pay/pay_success', ['transaction_type'=>2]));
  1072. }
  1073. $this->success('订单已在余额付款完成!即将跳转~~~', $url, $ResultData);
  1074. }
  1075. }else{
  1076. $this->error('订单支付异常,请刷新后再进行支付!');
  1077. }
  1078. }else{
  1079. $url = urldecode(url('user/Pay/pay_account_recharge'));
  1080. $this->error('余额不足,若要使用余额支付,请去充值!',$url);
  1081. }
  1082. }
  1083. }
  1084. public function update_pay_method()
  1085. {
  1086. if (IS_AJAX_POST) {
  1087. $post = input('post.');
  1088. if (!empty($post)) {
  1089. // 初始化默认为微信支付,用于存入数据
  1090. $pay_method = 'wechat';
  1091. // 初始化默认为传入的值,这个参数仅用于微信支付存入数据
  1092. $wechat_pay_type = '';
  1093. // 订单交易类型,用于判断
  1094. $transaction_type = $post['transaction_type'];
  1095. // 支付方式(支付宝或微信),用于判断
  1096. $pay_method_type = $post['pay_method'];
  1097. // 订单ID,用于查询
  1098. $unified_id = intval($post['unified_id']);
  1099. // 订单号,用于查询
  1100. $unified_number = $post['unified_number'];
  1101. /*判断微信支付是否开启*/
  1102. if ('WeChatInternal' == $pay_method_type) {
  1103. $where = [
  1104. 'pay_id' => 1,
  1105. 'pay_mark' => 'wechat'
  1106. ];
  1107. $PayInfo = Db::name('pay_api_config')->where($where)->getField('pay_info');
  1108. if (!empty($PayInfo)) $PayInfo = unserialize($PayInfo);
  1109. if (empty($PayInfo) || 1 == $PayInfo['is_open_wechat']) {
  1110. // $querydata = [
  1111. // 'order_id' => $unified_id,
  1112. // 'order_code' => $unified_number
  1113. // ];
  1114. // /*修复1.4.2漏洞 -- 加密防止利用序列化注入SQL*/
  1115. // $querystr = '';
  1116. // foreach($querydata as $_qk => $_qv)
  1117. // {
  1118. // $querystr .= $querystr ? "&$_qk=$_qv" : "$_qk=$_qv";
  1119. // }
  1120. // $querystr = str_replace('=', '', mchStrCode($querystr));
  1121. // $auth_code = tpCache('system.system_auth_code');
  1122. // $hash = md5("payment".$querystr.$auth_code);
  1123. // /*end*/
  1124. // $PaymentUrl = urldecode(url('user/Pay/pay_recharge_detail', ['querystr'=>$querystr,'hash'=>$hash]));
  1125. // 付款地址处理,对ID和订单号加密,拼装url路径
  1126. $Paydata = [
  1127. 'order_id' => $unified_id,
  1128. 'order_code' => $unified_number
  1129. ];
  1130. // 先 json_encode 后 md5 加密信息
  1131. $Paystr = md5(json_encode($Paydata));
  1132. // 清除之前的 cookie
  1133. Cookie::delete($Paystr);
  1134. // 存入 cookie
  1135. cookie($Paystr, $Paydata);
  1136. // 跳转链接
  1137. $PaymentUrl = urldecode(url('user/Pay/pay_recharge_detail',['paystr'=>$Paystr]));
  1138. $this->success('105:信息正确', $PaymentUrl, ['is_gourl'=>1]);
  1139. }
  1140. }
  1141. /* END */
  1142. // 判断订单交易类型,选择查询条件
  1143. if (1 == $transaction_type) {
  1144. // 充值金额
  1145. $UpdateWhere = [
  1146. 'moneyid' => $unified_id,
  1147. 'order_number' => $unified_number,
  1148. 'users_id' => $this->users_id,
  1149. 'lang' => $this->home_lang,
  1150. ];
  1151. }else if (2 == $transaction_type) {
  1152. // 购买商品
  1153. $UpdateWhere = [
  1154. 'order_id' => $unified_id,
  1155. 'order_code' => $unified_number,
  1156. 'users_id' => $this->users_id,
  1157. 'lang' => $this->home_lang,
  1158. ];
  1159. // 查询订单价格
  1160. $order_total_amount = $this->shop_order_db->where($UpdateWhere)->getField('order_total_amount');
  1161. }
  1162. // 判断支付方式及类型
  1163. if ('AliPay' == $pay_method_type) {
  1164. // 支付宝支付
  1165. $pay_method = 'alipay';
  1166. }else {
  1167. // 微信支付,先判断这个订单是否标记过,标记和传入的参数是否一致,不一致则返回提示结束支付
  1168. if ('1' == $transaction_type) {
  1169. // 充值金额,判断是否属于当前支付类型
  1170. $return = $this->determine_pay_type($this->users_money_db,$UpdateWhere,$pay_method_type);
  1171. if (!empty($return)) {
  1172. $this->error($return);exit;
  1173. }
  1174. }else if ('2' == $transaction_type) {
  1175. // 购买商品,判断是否属于当前支付类型
  1176. $return = $this->determine_pay_type($this->shop_order_db,$UpdateWhere,$pay_method_type);
  1177. if (!empty($return)) {
  1178. $this->error($return);exit;
  1179. }
  1180. }
  1181. // 判断支付类型
  1182. switch ($pay_method_type) {
  1183. case 'WeChatScanCode':
  1184. // PC端微信扫码支付
  1185. $wechat_pay_type = 'WeChatScanCode';
  1186. break;
  1187. case 'WeChatInternal':
  1188. // 手机微信端H5支付
  1189. $wechat_pay_type = 'WeChatInternal';
  1190. break;
  1191. case 'WeChatH5':
  1192. // 手机端浏览器H5支付
  1193. $wechat_pay_type = 'WeChatH5';
  1194. break;
  1195. default:
  1196. $this->error('错误提示:101,选择支付方式错误,请刷新后重试~~');
  1197. break;
  1198. }
  1199. }
  1200. // 判断充值金额\购买商品
  1201. if ('1' == $transaction_type) {
  1202. // 充值金额
  1203. $UpdateData = [
  1204. 'pay_method' => $pay_method,
  1205. 'update_time' => getTime(),
  1206. ];
  1207. if ('AliPay' != $pay_method_type) {
  1208. // 支付方式不等于支付宝时才修改的内容
  1209. $UpdateData['wechat_pay_type'] = $wechat_pay_type;
  1210. }
  1211. $result = $this->users_money_db->where($UpdateWhere)->update($UpdateData);
  1212. }else if ('2' == $transaction_type) {
  1213. // 购买商品
  1214. $UpdateData = [
  1215. 'pay_name' => $pay_method,
  1216. 'update_time' => getTime(),
  1217. ];
  1218. if ('AliPay' != $pay_method_type) {
  1219. // 支付方式不等于支付宝时才修改的内容
  1220. $UpdateData['wechat_pay_type'] = $wechat_pay_type;
  1221. }
  1222. $result = $this->shop_order_db->where($UpdateWhere)->update($UpdateData);
  1223. }
  1224. if (!empty($result)) {
  1225. if (isMobile() && isWeixin()) {
  1226. $ReturnOrderData = [
  1227. 'unified_id' => $unified_id,
  1228. 'unified_number' => $unified_number,
  1229. 'transaction_type' => $transaction_type, // 订单支付购买
  1230. 'order_total_amount' => $order_total_amount,
  1231. 'order_source' => $post['order_source'], // 订单列表页、订单详情页
  1232. 'is_gourl' => 1,
  1233. ];
  1234. if ($this->users['users_money'] <= '0.00') {
  1235. if (!empty($this->users['open_id']) || 0 === intval($this->users['thirdparty'])) {
  1236. // 余额小于0
  1237. $ReturnOrderData['is_gourl'] = 0;
  1238. $this->success('101:信息正确', null, $ReturnOrderData);
  1239. } else if (2 == $post['order_source']) {
  1240. $this->error('余额为0!');
  1241. } else {
  1242. // $this->error('手机端微信使用本站账号登录仅可余额支付!');
  1243. }
  1244. } else {
  1245. if (!empty($this->users['open_id']) || 0 === intval($this->users['thirdparty'])) {
  1246. // 余额大于0
  1247. // $url = url('user/Shop/shop_wechat_pay_select');
  1248. // session($this->users_id.'_ReturnOrderData', $ReturnOrderData);
  1249. // $this->success('102:信息正确', $url, $ReturnOrderData);
  1250. $ReturnOrderData['is_gourl'] = 0;
  1251. $this->success('102:信息正确', null, $ReturnOrderData);
  1252. } else if ($this->users['users_money'] < $order_total_amount){
  1253. $this->error('余额不足!');
  1254. } else {
  1255. $url = url('user/Shop/shop_wechat_pay_select');
  1256. session($this->users_id.'_ReturnOrderData', $ReturnOrderData);
  1257. $this->success('102:信息正确', $url, $ReturnOrderData);
  1258. }
  1259. }
  1260. }else{
  1261. $this->success('103:信息正确');
  1262. }
  1263. }else{
  1264. $this->error('数据错误,请刷新后重试!刷新后仍然无法支付请联系管理员!');
  1265. }
  1266. }else{
  1267. $this->error('数据错误,请刷新后重试~');
  1268. }
  1269. }
  1270. }
  1271. // 确定支付类型
  1272. // $table 查询的表,仅用于充值金额和购买订单表
  1273. // $where 查询条件
  1274. // $pay_method_type 当前提交的类型,用于判断
  1275. private function determine_pay_type($table, $where, $pay_method_type)
  1276. {
  1277. $new_wechat_pay_type = $table->where($where)->getField('wechat_pay_type');
  1278. // 若为空,则表现未标记过支付类型
  1279. if (empty($new_wechat_pay_type)) {
  1280. return false;
  1281. }
  1282. // 是否数据库中的支付类型和传入的一致
  1283. if ($new_wechat_pay_type != $pay_method_type) {
  1284. // 判断返回提示信息
  1285. switch ($new_wechat_pay_type) {
  1286. case 'WeChatScanCode':
  1287. // PC端微信扫码支付
  1288. return '已在PC端浏览器中微信扫码生成订单,请到PC端浏览器完成支付!';
  1289. break;
  1290. case 'WeChatInternal':
  1291. // 手机微信端H5支付
  1292. return '已在手机端微信中生成订单,请到手机端微信完成支付!';
  1293. break;
  1294. case 'WeChatH5':
  1295. // 手机端浏览器H5支付
  1296. return '已在手机端浏览器中生成订单,请到手机端浏览器完成支付!';
  1297. break;
  1298. default:
  1299. return '错误提示:102,选择支付方式错误,请刷新后重试~~';
  1300. break;
  1301. }
  1302. } else {
  1303. return false;
  1304. }
  1305. }
  1306. // 手机微信端H5支付
  1307. public function wechat_pay()
  1308. {
  1309. if (IS_POST) {
  1310. $unified_id = input('post.unified_id/d');
  1311. $unified_number = input('post.unified_number/s');
  1312. $transaction_type = input('post.transaction_type/d');
  1313. $where = [
  1314. 'users_id' => $this->users_id,
  1315. 'lang' => $this->home_lang,
  1316. ];
  1317. $open_id = input('post.openid') ? input('post.openid') : $this->users_db->where($where)->getField('open_id');
  1318. // if (empty($open_id)) $this->error('手机端微信使用本站账号登录仅可余额支付!');
  1319. if (2 == $transaction_type) {
  1320. // 购买商品
  1321. $PayWhere = [
  1322. 'order_id' => $unified_id,
  1323. 'order_code' => $unified_number
  1324. ];
  1325. $PayData = $this->shop_order_db->where($PayWhere)->field('order_code, order_amount')->find();
  1326. $out_trade_no = $PayData['order_code'];
  1327. $total_fee = $PayData['order_amount'];
  1328. } else if (1 == $transaction_type) {
  1329. // 充值金额
  1330. $PayWhere = [
  1331. 'moneyid' => $unified_id,
  1332. 'order_number' => $unified_number
  1333. ];
  1334. $PayData = $this->users_money_db->where($PayWhere)->field('order_number, money')->find();
  1335. $out_trade_no = $PayData['order_number'];
  1336. $total_fee = $PayData['money'];
  1337. } else if (3 == $transaction_type) {
  1338. // 升级金额
  1339. $PayWhere = [
  1340. 'moneyid' => $unified_id,
  1341. 'order_number' => $unified_number
  1342. ];
  1343. $PayData = $this->users_money_db->where($PayWhere)->field('order_number, money')->find();
  1344. $out_trade_no = $PayData['order_number'];
  1345. $total_fee = $PayData['money'];
  1346. } else if (9 == $transaction_type) {
  1347. // 文章付费金额
  1348. $PayWhere = [
  1349. 'order_id' => $unified_id,
  1350. 'order_code' => $unified_number
  1351. ];
  1352. $PayData = M('article_order')->where($PayWhere)->field('order_code, order_amount')->find();
  1353. $out_trade_no = $PayData['order_code'];
  1354. $total_fee = $PayData['order_amount'];
  1355. } else if (10 == $transaction_type) {
  1356. // 下载模型付费金额
  1357. $PayWhere = [
  1358. 'order_id' => $unified_id,
  1359. 'order_code' => $unified_number
  1360. ];
  1361. $PayData = Db::name('download_order')->where($PayWhere)->field('order_code, order_amount')->find();
  1362. $out_trade_no = $PayData['order_code'];
  1363. $total_fee = $PayData['order_amount'];
  1364. } else {
  1365. $this->error('订单类型错误!');
  1366. }
  1367. // 判断是否小程序接入,存在openid则表示小程序接入
  1368. if (input('post.openid')) {
  1369. $data = model('PayApi')->getWechatPay($open_id, $out_trade_no, $total_fee, [], 1, $transaction_type);
  1370. } else {
  1371. $data = model('PayApi')->getWechatPay($open_id, $out_trade_no, $total_fee, [], 0, $transaction_type);
  1372. }
  1373. // 这个data返回的是调用需要时,所需要给微信提供的公众号参数,并非提示信息
  1374. if (!empty($data)) {
  1375. if (input('post.openid')) {
  1376. echo json_encode($data);
  1377. } else {
  1378. $this->success($data);
  1379. }
  1380. } else {
  1381. $this->error('微信支付信息错误,请刷新后重试~');
  1382. }
  1383. }
  1384. }
  1385. public function get_openid()
  1386. {
  1387. // 小程序配置
  1388. $MiniproValue = Db::name('weapp_minipro0002')->where('type', 'minipro')->getField('value');
  1389. if (empty($MiniproValue)) return false;
  1390. $MiniproValue = !empty($MiniproValue) ? json_decode($MiniproValue, true) : [];
  1391. $code = input('param.code');
  1392. $appid = $MiniproValue['appId']; // 小程序APPID
  1393. $secret = $MiniproValue['appSecret']; // 小程序secret
  1394. $url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' . $appid . '&secret=' . $secret . '&js_code=' . $code . '&grant_type=authorization_code';
  1395. $curl = curl_init();
  1396. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  1397. curl_setopt($curl, CURLOPT_TIMEOUT, 500);
  1398. // 为保证第三方服务器与微信服务器之间数据传输的安全性,所有微信接口采用https方式调用,必须使用下面2行代码打开ssl安全校验。
  1399. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  1400. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  1401. curl_setopt($curl, CURLOPT_URL, $url);
  1402. $res = curl_exec($curl);
  1403. curl_close($curl);
  1404. echo json_encode($res);
  1405. }
  1406. public function ajax_applets_pay()
  1407. {
  1408. // 小程序配置
  1409. $MiniproValue = Db::name('weapp_minipro0002')->where('type', 'minipro')->getField('value');
  1410. if (empty($MiniproValue)) return false;
  1411. $MiniproValue = !empty($MiniproValue) ? json_decode($MiniproValue, true) : [];
  1412. $app_id = $MiniproValue['appId'];
  1413. $unified_id = input('post.unified_id/s');
  1414. $unified_number = input('post.unified_number/s');
  1415. $transaction_type = input('post.transaction_type');
  1416. if (!empty($app_id) && !empty($unified_id) && !empty($unified_number)) {
  1417. // ajax异步查询订单是否完成并处理相应逻辑返回。
  1418. vendor('wechatpay.lib.WxPayApi');
  1419. vendor('wechatpay.lib.WxPayConfig');
  1420. // 实例化加载订单号
  1421. $input = new \WxPayOrderQuery;
  1422. $input->SetOut_trade_no($unified_number);
  1423. // 处理微信配置数据
  1424. $where = [
  1425. 'pay_id' => 1,
  1426. 'pay_mark' => 'wechat'
  1427. ];
  1428. $pay_wechat_config = Db::name('pay_api_config')->where($where)->getField('pay_info');
  1429. if (empty($pay_wechat_config)) return false;
  1430. $pay_wechat_config = unserialize($pay_wechat_config);
  1431. $config_data['app_id'] = $app_id;
  1432. $config_data['mch_id'] = $pay_wechat_config['mchid'];
  1433. $config_data['key'] = $pay_wechat_config['key'];
  1434. // 实例化微信配置
  1435. $config = new \WxPayConfig($config_data);
  1436. $wxpayapi = new \WxPayApi;
  1437. // 返回结果
  1438. $result = $wxpayapi->orderQuery($config, $input);
  1439. if (isset($result['return_code']) && $result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS'){
  1440. if ($result['trade_state'] == 'SUCCESS' && !empty($result['transaction_id'])) {
  1441. if (1 == $transaction_type) {
  1442. // 充值处理
  1443. $moneydata = $this->users_money_db->where([
  1444. 'order_number' => $result['out_trade_no'],
  1445. ])->find();
  1446. // 微信付款成功后,订单并未修改状态时,修改订单状态并返回
  1447. if ($moneydata['status'] == 1) {
  1448. // 修改会员金额明细表中,对应的订单数据,存入返回的数据,订单已付款
  1449. $data = [
  1450. 'status' => 2,
  1451. 'pay_details' => serialize($result),
  1452. 'update_time' => getTime(),
  1453. ];
  1454. $ismoney = $this->users_money_db->where([
  1455. 'moneyid' => $moneydata['moneyid'],
  1456. ])->update($data);
  1457. if (!empty($ismoney)) {
  1458. // 同步修改会员的金额
  1459. $usersdata = [
  1460. 'users_money' => Db::raw('users_money+'.($moneydata['money'])),
  1461. ];
  1462. $isusers = $this->users_db->where([
  1463. 'users_id' => $moneydata['users_id'],
  1464. ])->update($usersdata);
  1465. if (!empty($isusers)) {
  1466. // 业务处理完成,订单已完成
  1467. $data2 = [
  1468. 'status' => 3,
  1469. 'update_time' => getTime(),
  1470. ];
  1471. $this->users_money_db->where([
  1472. 'moneyid' => $moneydata['moneyid'],
  1473. 'users_id' => $moneydata['users_id'],
  1474. ])->update($data2);
  1475. $url = url('user/Pay/pay_consumer_details');
  1476. $this->success('充值成功', $url);
  1477. }else{
  1478. $this->error('付款成功,但未充值成功,请联系管理员。');
  1479. }
  1480. }else{
  1481. $this->error('付款成功,数据错误,未能充值成功,请联系管理员。');
  1482. }
  1483. }
  1484. } else if (2 == $transaction_type) {
  1485. // 支付处理
  1486. $order_data = $this->shop_order_db->where([
  1487. 'order_code' => $result['out_trade_no'],
  1488. ])->find();
  1489. if (0 == $order_data['order_status']) {
  1490. $OrderWhere = [
  1491. 'order_id' => $order_data['order_id'],
  1492. ];
  1493. // 修改会员金额明细表中,对应的订单数据,存入返回的数据,订单已付款
  1494. $OrderData = [
  1495. 'order_status' => 1,
  1496. 'pay_details' => serialize($result),
  1497. 'pay_time' => getTime(),
  1498. 'update_time' => getTime(),
  1499. ];
  1500. $order_id = $this->shop_order_db->where($OrderWhere)->update($OrderData);
  1501. if (!empty($order_id)) {
  1502. $DetailsData['update_time'] = getTime();
  1503. $this->shop_order_details_db->where($OrderWhere)->update($DetailsData);
  1504. // 添加订单操作记录
  1505. AddOrderAction($order_data['order_id'],$order_data['users_id'],'0','1','0','1','支付成功!','会员使用微信小程序完成支付!');
  1506. // 邮箱发送
  1507. $SmtpConfig = tpCache('smtp');
  1508. $Result['email'] = GetEamilSendData($SmtpConfig, $this->users, $order_data, 1, 'wechat');
  1509. // 手机发送
  1510. $SmsConfig = tpCache('sms');
  1511. $Result['mobile'] = GetMobileSendData($SmsConfig, $this->users, $order_data, 1, 'wechat');
  1512. $url = url('user/Shop/shop_centre');
  1513. $this->success('支付成功!', $url, $Result);
  1514. }
  1515. }
  1516. } else if (3 == $transaction_type) {
  1517. // 会员升级处理
  1518. $moneydata = $this->users_money_db->where([
  1519. 'order_number' => $result['out_trade_no'],
  1520. ])->find();
  1521. // 微信付款成功后,订单并未修改状态时,修改订单状态并返回
  1522. if ($moneydata['status'] == 1) {
  1523. $cause = unserialize($moneydata['cause']);
  1524. $UsersTypeData = M('users_type_manage')->where('type_id',$cause['type_id'])->find();
  1525. // 订单更新条件
  1526. $where = [
  1527. 'moneyid' => $moneydata['moneyid'],
  1528. 'users_id' => $moneydata['users_id'],
  1529. ];
  1530. // 订单更新数据,更新为已付款
  1531. $details = '会员当前级别为【' . $this->users['level_name'] . '】,使用微信支付【 ' . $UsersTypeData['type_name'] . '】,支付金额为' . $UsersTypeData['price'];
  1532. $UpMoneyData = [
  1533. 'cause' => serialize($UsersTypeData),
  1534. 'money' => $UsersTypeData['price'],
  1535. 'status' => 2,
  1536. 'pay_method' => $moneydata['pay_method'],
  1537. 'wechat_pay_type' => 'WeChatInternal',
  1538. 'pay_details' => serialize($details),
  1539. // 如果时升级订单则存在升级会员级别ID
  1540. 'level_id' => $UsersTypeData['level_id'],
  1541. 'update_time' => getTime()
  1542. ];
  1543. // 订单更新
  1544. $ResultID = $this->users_money_db->where($where)->update($UpMoneyData);
  1545. // 订单更新后续操作
  1546. if (!empty($ResultID)) {
  1547. $Where = [
  1548. 'users_id' => $moneydata['users_id'],
  1549. ];
  1550. // 获取更新会员数据数组
  1551. $UpUsersData = $this->GetUpUsersData($UsersTypeData);
  1552. $ReturnID = M('users')->where($Where)->update($UpUsersData);
  1553. // 用户充值金额后续操作
  1554. if (!empty($ReturnID)) {
  1555. $url = url('user/Level/level_centre');
  1556. $this->success('升级成功', $url);
  1557. } else {
  1558. $this->error('付款成功,但未升级成功,请联系管理员。');
  1559. }
  1560. } else {
  1561. $this->error('付款成功,数据错误,未能升级成功,请联系管理员。');
  1562. }
  1563. }
  1564. } else if (9 == $transaction_type) {
  1565. // 支付处理
  1566. $order_data = M('article_order')->where([
  1567. 'order_code' => $result['out_trade_no'],
  1568. ])->find();
  1569. if (0 == $order_data['order_status']) {
  1570. $OrderWhere = [
  1571. 'order_id' => $order_data['order_id'],
  1572. ];
  1573. // 修改会员金额明细表中,对应的订单数据,存入返回的数据,订单已付款
  1574. $OrderData = [
  1575. 'order_status' => 1,
  1576. 'pay_details' => serialize($result),
  1577. 'pay_time' => getTime(),
  1578. 'pay_name' => 'wechat',
  1579. 'wechat_pay_type' => 'WeChatInternal',
  1580. 'update_time' => getTime(),
  1581. ];
  1582. $order_id = M('article_order')->where($OrderWhere)->update($OrderData);
  1583. if (!empty($order_id)) {
  1584. // 添加订单操作记录
  1585. AddOrderAction($order_data['order_id'],$order_data['users_id'],'0','1','0','1','支付成功!','会员使用微信小程序完成支付!');
  1586. $url = url('user/Users/article_index');
  1587. $this->success('支付成功!', $url);
  1588. } else {
  1589. $this->error('付款成功,但未执行业务,请联系管理员。');
  1590. }
  1591. }
  1592. } else if (10 == $transaction_type) {
  1593. // 支付处理
  1594. $order_data = Db::name('download_order')->where([
  1595. 'order_code' => $result['out_trade_no'],
  1596. ])->find();
  1597. if (0 == $order_data['order_status']) {
  1598. $OrderWhere = [
  1599. 'order_id' => $order_data['order_id'],
  1600. ];
  1601. // 修改会员金额明细表中,对应的订单数据,存入返回的数据,订单已付款
  1602. $OrderData = [
  1603. 'order_status' => 1,
  1604. 'pay_details' => serialize($result),
  1605. 'pay_time' => getTime(),
  1606. 'pay_name' => 'wechat',
  1607. 'wechat_pay_type' => 'WeChatInternal',
  1608. 'update_time' => getTime(),
  1609. ];
  1610. $order_id = Db::name('download_order')->where($OrderWhere)->update($OrderData);
  1611. if (!empty($order_id)) {
  1612. // 添加订单操作记录
  1613. AddOrderAction($order_data['order_id'],$order_data['users_id'],'0','1','0','1','支付成功!','会员使用微信小程序完成支付!');
  1614. $url = url('user/Users/download_index');
  1615. $this->success('支付成功!', $url);
  1616. } else {
  1617. $this->error('付款成功,但未执行业务,请联系管理员。');
  1618. }
  1619. }
  1620. }
  1621. }
  1622. }
  1623. }
  1624. }
  1625. // 拼装更新会员数据数组
  1626. private function GetUpUsersData($data = array(), $balance = false)
  1627. {
  1628. $time = getTime();
  1629. // 会员期限定义数组
  1630. $limit_arr = Config::get('global.admin_member_limit_arr');
  1631. // 到期天数
  1632. $maturity_days = $limit_arr[$data['limit_id']]['maturity_days'];
  1633. // 更新会员属性表的数组
  1634. $result = [
  1635. 'level' => $data['level_id'],
  1636. 'update_time' => $time,
  1637. 'level_maturity_days' => Db::raw('level_maturity_days+'.($maturity_days)),
  1638. ];
  1639. // 如果是余额支付则追加数组
  1640. if (!empty($balance)) $result['users_money'] = Db::raw('users_money-'.($data['price']));
  1641. // 判断是否需要追加天数,maturity_code在Base层已计算,1表示终身会员天数
  1642. if (1 != $this->users['maturity_code']) {
  1643. // 判断是否到期,到期则执行,3表示会员在期限内,不需要进行下一步操作
  1644. if (3 != $this->users['maturity_code']) {
  1645. // 追加天数数组
  1646. $result['open_level_time'] = $time;
  1647. $result['level_maturity_days'] = $maturity_days;
  1648. }
  1649. }
  1650. return $result;
  1651. }
  1652. }