Нет описания
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

PayLogic.php 41KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  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-06-25
  12. */
  13. namespace app\user\logic;
  14. use think\Model;
  15. use think\Db;
  16. use think\Request;
  17. use think\Config;
  18. /**
  19. * 回调逻辑处理
  20. * @package user\Logic
  21. */
  22. class PayLogic extends Model
  23. {
  24. private $home_lang = 'cn';
  25. /**
  26. * 初始化操作
  27. */
  28. public function initialize() {
  29. parent::initialize();
  30. $this->home_lang = get_home_lang();
  31. $this->users_db = Db::name('users'); // 会员数据表
  32. $this->users_money_db = Db::name('users_money'); // 会员金额明细表
  33. $this->shop_order_db = Db::name('shop_order'); // 订单主表
  34. $this->shop_order_details_db = Db::name('shop_order_details'); // 订单明细表
  35. $this->users_type_manage_db = Db::name('users_type_manage'); // 会员升级分类价格表
  36. $this->media_order_db = Db::name('media_order'); // 视频订单表
  37. $this->article_order_db = Db::name('article_order'); // 文章订单表
  38. $this->download_order_db = Db::name('download_order'); // 下载模型订单表
  39. }
  40. /*----------支付宝回调开始----------*/
  41. public function alipay_return()
  42. {
  43. if (!empty($_POST)) {
  44. foreach($_POST as $key => $value){
  45. $_GET[$key] = $value;
  46. }
  47. }
  48. $param = $data = $_GET;
  49. if (empty($param['total_amount']) && !empty($param['total_fee'])){
  50. $param['total_amount'] = $param['total_fee'];
  51. }
  52. $OrderData = $this->checkAmount($param['out_trade_no'],$param['total_amount'],$param['transaction_type']);
  53. if(empty($OrderData)){
  54. if (!empty($param['is_notify']) && 1 == $param['is_notify']) {
  55. echo 'fail'; exit;
  56. }else{
  57. $retData = [
  58. 'code' => 0,
  59. 'msg' => '支付失败,支付金额与订单金额不相符',
  60. ];
  61. return $retData;
  62. }
  63. }
  64. // 支付宝配置信息
  65. $where = [
  66. 'pay_id' => 2,
  67. 'pay_mark' => 'alipay'
  68. ];
  69. $pay_alipay_config = Db::name('pay_api_config')->where($where)->getField('pay_info');
  70. if (empty($pay_alipay_config)) {
  71. $pay_alipay_config = getUsersConfigData('pay.pay_alipay_config');
  72. if (empty($pay_alipay_config)) {
  73. if (!empty($param['is_notify']) && 1 == $param['is_notify']) {
  74. echo 'fail'; exit;
  75. }else{
  76. $retData = [
  77. 'code' => 0,
  78. 'msg' => '支付宝配置不正确',
  79. ];
  80. return $retData;
  81. }
  82. }
  83. }
  84. $pay_alipay_config = unserialize($pay_alipay_config);
  85. // 新旧版处理
  86. switch ($pay_alipay_config['version']) {
  87. // 新版支付宝
  88. case '0':
  89. // 新版获取RSA2加密返回,返回bool值
  90. $Return = $this->GetNewAliPayRsa2Return($data, $pay_alipay_config);
  91. if (!empty($Return)) {
  92. $return = $this->NewAliPayProcessing($param);
  93. if (!empty($param['is_notify']) && 1 == $param['is_notify']) {
  94. echo $return; exit;
  95. }else{
  96. return $return;
  97. }
  98. }else{
  99. if (!empty($param['is_notify']) && 1 == $param['is_notify']) {
  100. echo 'fail'; exit;
  101. }else{
  102. $retData = [
  103. 'code' => 0,
  104. 'msg' => '订单验证失败!',
  105. ];
  106. return $retData;
  107. }
  108. }
  109. break;
  110. // 旧版支付宝
  111. case '1':
  112. // 旧版获取MD5加密的sign
  113. $Sign = $this->GetOldAliPayMd5Sign($data, $pay_alipay_config['code']);
  114. if ($Sign == $data['sign']){
  115. $return = $this->OldAliPayProcessing($param);
  116. if (!empty($param['is_notify']) && 1 == $param['is_notify']) {
  117. echo $return; exit;
  118. }else{
  119. return $return;
  120. }
  121. }else{
  122. if (!empty($param['is_notify']) && 1 == $param['is_notify']) {
  123. echo "fail"; exit;
  124. }else{
  125. $retData = [
  126. 'code' => 0,
  127. 'msg' => '订单验证失败!',
  128. ];
  129. return $retData;
  130. }
  131. }
  132. break;
  133. }
  134. }
  135. // 新版
  136. private function NewAliPayProcessing($param = array())
  137. {
  138. // 实际付款金额
  139. $order_amount = $param['total_amount'];
  140. if (2 == $param['transaction_type']) {
  141. // 商城订单购买支付回调处理
  142. $return = $this->ShopOrderProcessing($param, $order_amount);
  143. return $return;
  144. }else if (1 == $param['transaction_type'] || 3 == $param['transaction_type']) {
  145. // 会员充值或升级支付回调处理
  146. $return = $this->MoneyOrderProcessing($param, $order_amount);
  147. return $return;
  148. }else if (8 == $param['transaction_type']) {
  149. // 视频购买
  150. $return = $this->MediaOrderProcessing($param, $order_amount);
  151. return $return;
  152. }else if (9 == $param['transaction_type']) {
  153. // 文章购买
  154. $return = $this->ArticleOrderProcessing($param, $order_amount);
  155. return $return;
  156. }else if (10 == $param['transaction_type']) {
  157. // 下载购买
  158. $return = $this->DownloadOrderProcessing($param, $order_amount);
  159. return $return;
  160. }
  161. }
  162. // 旧版
  163. private function OldAliPayProcessing($param = array())
  164. {
  165. // 实际付款金额
  166. $order_amount = $param['total_fee'];
  167. if (2 == $param['transaction_type']) {
  168. // 商城订单购买支付回调处理
  169. $return = $this->ShopOrderProcessing($param, $order_amount);
  170. return $return;
  171. }else if (1 == $param['transaction_type'] || 3 == $param['transaction_type']) {
  172. // 会员充值或升级支付回调处理
  173. $return = $this->MoneyOrderProcessing($param, $order_amount);
  174. return $return;
  175. }else if (8 == $param['transaction_type']) {
  176. // 视频购买
  177. $return = $this->MediaOrderProcessing($param, $order_amount);
  178. return $return;
  179. }else if (9 == $param['transaction_type']) {
  180. // 文章购买
  181. $return = $this->ArticleOrderProcessing($param, $order_amount);
  182. return $return;
  183. }else if (10 == $param['transaction_type']) {
  184. // 文章购买
  185. $return = $this->DownloadOrderProcessing($param, $order_amount);
  186. return $return;
  187. }
  188. }
  189. // 商城订单购买支付回调处理
  190. private function ShopOrderProcessing($param = array(), $order_amount = null)
  191. {
  192. if (!empty($param['out_trade_no']) && !empty($param['trade_no'])) {
  193. $OrderWhere = [
  194. // 订单号
  195. 'order_code' => $param['out_trade_no'],
  196. // 实际支付金额
  197. 'order_amount' => $order_amount,
  198. ];
  199. $OrderData = $this->shop_order_db->where($OrderWhere)->find();
  200. if (!empty($OrderData)) {
  201. // 支付宝付款成功后,订单并未修改状态时,修改订单状态并返回
  202. if (0 == $OrderData['order_status']) {
  203. $returnData = pay_success_logic($OrderData['users_id'], $OrderData['order_code'], $param, 'alipay');
  204. if (is_array($returnData)) {
  205. if (1 == $returnData['code']) {
  206. if (1 == $param['is_notify']) {
  207. return "success";
  208. } else {
  209. $retData = [
  210. 'code' => 1,
  211. 'msg' => '订单支付完成!',
  212. 'url' => url('user/Shop/shop_centre'),
  213. ];
  214. return $retData;
  215. }
  216. }
  217. }
  218. }else{
  219. if (1 == $param['is_notify']) {
  220. return "success";
  221. }else{
  222. $msg = [
  223. 'code' => 1,
  224. 'msg' => '订单支付完成!',
  225. 'url' => url('user/Shop/shop_centre'),
  226. ];
  227. return $msg;
  228. }
  229. }
  230. }
  231. }
  232. if (1 == $param['is_notify']) {
  233. return "fail";
  234. }else{
  235. $retData = [
  236. 'code' => 0,
  237. 'msg' => '订单处理失败,如已确认付款,请联系管理员!',
  238. 'url' => '',
  239. ];
  240. return $retData;
  241. }
  242. }
  243. // 会员充值或升级支付回调处理
  244. private function MoneyOrderProcessing($param = array(), $order_amount = null)
  245. {
  246. if (!empty($param['out_trade_no']) && !empty($param['trade_no'])) {
  247. // 付款成功
  248. $MoneyWhere = [
  249. 'lang' => $this->home_lang,
  250. // 实际付款金额
  251. 'money' => $order_amount,
  252. // 订单号
  253. 'order_number' => $param['out_trade_no'],
  254. ];
  255. $MoneyData = $this->users_money_db->where($MoneyWhere)->find();
  256. // 支付宝订单统一处理
  257. $msg = $this->MoneyUnifiedProcessing($param, $MoneyData, $order_amount);
  258. return $msg;
  259. }
  260. if (1 == $param['is_notify']) {
  261. return "fail";
  262. }else{
  263. $msg = [
  264. 'code' => 1,
  265. 'msg' => '订单处理失败,如已确认付款,请联系管理员!',
  266. 'url' => '',
  267. ];
  268. return $msg;
  269. }
  270. }
  271. // 文章购买支付回调处理
  272. private function ArticleOrderProcessing($param = array(), $order_amount = null)
  273. {
  274. if (!empty($param['out_trade_no']) && !empty($param['trade_no'])) {
  275. $OrderWhere = [
  276. // 订单号
  277. 'order_code' => $param['out_trade_no'],
  278. // 实际支付金额
  279. 'order_amount' => $order_amount,
  280. ];
  281. $OrderData = $this->article_order_db->where($OrderWhere)->find();
  282. $ViewUrl = cookie($OrderData['users_id'] . '_' . $OrderData['product_id'] . '_EyouArticleViewUrl');
  283. if (!empty($OrderData)) {
  284. // 支付宝付款成功后,订单并未修改状态时,修改订单状态并返回
  285. if (0 == $OrderData['order_status']) {
  286. // 订单更新数据,更新为已付款
  287. $OrderData = [
  288. 'order_status' => 1,
  289. 'pay_details' => serialize($param),
  290. 'pay_time' => getTime(),
  291. 'update_time' => getTime()
  292. ];
  293. // 订单更新
  294. $returnData = $this->article_order_db->where($OrderWhere)->update($OrderData);
  295. if (!empty($returnData)) {
  296. if (1 == $param['is_notify']) {
  297. return "success";
  298. } else {
  299. $retData = [
  300. 'code' => 1,
  301. 'msg' => '订单支付完成!',
  302. 'url' => $ViewUrl,
  303. ];
  304. return $retData;
  305. }
  306. }
  307. }else{
  308. if (1 == $param['is_notify']) {
  309. return "success";
  310. }else{
  311. $msg = [
  312. 'code' => 1,
  313. 'msg' => '订单支付完成!',
  314. 'url' => $ViewUrl
  315. ];
  316. return $msg;
  317. }
  318. }
  319. }
  320. }
  321. if (1 == $param['is_notify']) {
  322. return "fail";
  323. }else{
  324. $retData = [
  325. 'code' => 0,
  326. 'msg' => '订单处理失败,如已确认付款,请联系管理员!',
  327. 'url' => '',
  328. ];
  329. return $retData;
  330. }
  331. }
  332. // 视频购买支付回调处理
  333. private function MediaOrderProcessing($param = array(), $order_amount = null)
  334. {
  335. if (!empty($param['out_trade_no']) && !empty($param['trade_no'])) {
  336. $OrderWhere = [
  337. // 订单号
  338. 'order_code' => $param['out_trade_no'],
  339. // 实际支付金额
  340. 'order_amount' => $order_amount,
  341. ];
  342. $OrderData = $this->media_order_db->where($OrderWhere)->find();
  343. $ViewUrl = cookie($OrderData['users_id'] . '_' . $OrderData['product_id'] . '_EyouMediaViewUrl');
  344. if (!empty($OrderData)) {
  345. // 支付宝付款成功后,订单并未修改状态时,修改订单状态并返回
  346. if (0 == $OrderData['order_status']) {
  347. // 订单更新数据,更新为已付款
  348. $OrderData = [
  349. 'order_status' => 1,
  350. 'pay_details' => serialize($param),
  351. 'pay_time' => getTime(),
  352. 'update_time' => getTime()
  353. ];
  354. $returnData = $this->media_order_db->where($OrderWhere)->update($OrderData);
  355. if (!empty($returnData)) {
  356. if (1 == $param['is_notify']) {
  357. return "success";
  358. } else {
  359. $retData = [
  360. 'code' => 1,
  361. 'msg' => '订单支付完成!',
  362. 'url' => $ViewUrl,
  363. ];
  364. return $retData;
  365. }
  366. }
  367. }else{
  368. if (1 == $param['is_notify']) {
  369. return "success";
  370. }else{
  371. $msg = [
  372. 'code' => 1,
  373. 'msg' => '订单支付完成!',
  374. 'url' => $ViewUrl
  375. ];
  376. return $msg;
  377. }
  378. }
  379. }
  380. }
  381. if (1 == $param['is_notify']) {
  382. return "fail";
  383. }else{
  384. $retData = [
  385. 'code' => 0,
  386. 'msg' => '订单处理失败,如已确认付款,请联系管理员!',
  387. 'url' => '',
  388. ];
  389. return $retData;
  390. }
  391. }
  392. // 下载模型购买支付回调处理
  393. private function DownloadOrderProcessing($param = array(), $order_amount = null)
  394. {
  395. if (!empty($param['out_trade_no']) && !empty($param['trade_no'])) {
  396. $OrderWhere = [
  397. // 订单号
  398. 'order_code' => $param['out_trade_no'],
  399. // 实际支付金额
  400. 'order_amount' => $order_amount,
  401. ];
  402. $OrderData = $this->download_order_db->where($OrderWhere)->find();
  403. $ViewUrl = cookie($OrderData['users_id'] . '_' . $OrderData['product_id'] . '_EyouDownloadViewUrl');
  404. if (!empty($OrderData)) {
  405. // 支付宝付款成功后,订单并未修改状态时,修改订单状态并返回
  406. if (0 == $OrderData['order_status']) {
  407. // 订单更新数据,更新为已付款
  408. $OrderData = [
  409. 'order_status' => 1,
  410. 'pay_details' => serialize($param),
  411. 'pay_time' => getTime(),
  412. 'update_time' => getTime()
  413. ];
  414. // 订单更新
  415. $returnData = $this->download_order_db->where($OrderWhere)->update($OrderData);
  416. if (!empty($returnData)) {
  417. if (1 == $param['is_notify']) {
  418. return "success";
  419. } else {
  420. $retData = [
  421. 'code' => 1,
  422. 'msg' => '订单支付完成!',
  423. 'url' => $ViewUrl,
  424. ];
  425. return $retData;
  426. }
  427. }
  428. }else{
  429. if (1 == $param['is_notify']) {
  430. return "success";
  431. }else{
  432. $msg = [
  433. 'code' => 1,
  434. 'msg' => '订单支付完成!',
  435. 'url' => $ViewUrl
  436. ];
  437. return $msg;
  438. }
  439. }
  440. }
  441. }
  442. if (1 == $param['is_notify']) {
  443. return "fail";
  444. }else{
  445. $retData = [
  446. 'code' => 0,
  447. 'msg' => '订单处理失败,如已确认付款,请联系管理员!',
  448. 'url' => '',
  449. ];
  450. return $retData;
  451. }
  452. }
  453. // 支付宝订单处理流程
  454. // 参数1为支付宝返回数据集
  455. // 参数2为充值记录表数据集
  456. // 参数3为订单实际付款金额
  457. private function MoneyUnifiedProcessing($param, $MoneyData, $PayMoney){
  458. $referurl = input('param.referurl/s', null, 'urldecode,base64_decode');
  459. // 支付宝付款成功后,订单并未修改状态时,修改订单状态并返回
  460. if ($MoneyData['status'] == 1) {
  461. // 当前时间
  462. $time = getTime();
  463. // 更新条件
  464. $where = [
  465. 'moneyid' => $MoneyData['moneyid'],
  466. 'users_id' => $MoneyData['users_id'],
  467. ];
  468. // 更新数据
  469. $UpMoneyData = [
  470. 'status' => 2,
  471. 'pay_method' => 'alipay',
  472. 'wechat_pay_type' => '',
  473. 'pay_details' => serialize($param),
  474. 'update_time' => $time,
  475. ];
  476. // 若类型为会员升级则删除订单详情
  477. if (0 == $MoneyData['cause_type']) unset($UpMoneyData['pay_details']);
  478. // 执行更新
  479. $ReturnId = $this->users_money_db->where($where)->update($UpMoneyData);
  480. if (!empty($ReturnId)) {
  481. $UpUsersData = [];
  482. $ReturnId = '';
  483. if (1 == $MoneyData['cause_type']) {
  484. // 会员充值
  485. // 更新会员金额
  486. $UpUsersData['users_money'] = Db::raw('users_money+'.$PayMoney);
  487. $UpUsersData['update_time'] = $time;
  488. }else if (0 == $MoneyData['cause_type']) {
  489. // 会员升级
  490. // 更新会员级别和天数
  491. $UpUsersData = $this->GetUsersUpgradeData($MoneyData);
  492. }
  493. if (!empty($UpUsersData)) {
  494. $ReturnId = $this->users_db->where('users_id',$MoneyData['users_id'])->update($UpUsersData);
  495. }
  496. if (!empty($ReturnId)) {
  497. if (1 == $MoneyData['cause_type']) {
  498. // 业务处理完成,订单已完成
  499. $UpMoneyData_ = [
  500. 'status' => 3,
  501. 'update_time' => $time,
  502. ];
  503. $this->users_money_db->where($where)->update($UpMoneyData_);
  504. }
  505. if (1 == $param['is_notify']) {
  506. return "success";
  507. }else{
  508. $msg = [
  509. 'code' => 1,
  510. 'msg' => '支付完成',
  511. 'url' => !empty($referurl) ? $referurl : url('user/Level/level_centre'),
  512. ];
  513. return $msg;
  514. }
  515. }
  516. }
  517. if (1 == $param['is_notify']) {
  518. return "success";
  519. }else{
  520. $msg = [
  521. 'code' => 0,
  522. 'msg' => '支付成功,系统未处理成功,请联系管理员',
  523. 'url' => '',
  524. ];
  525. return $msg;
  526. }
  527. }
  528. if (1 == $param['is_notify']) {
  529. return "success";
  530. }else{
  531. $msg = [
  532. 'code' => 1,
  533. 'msg' => '支付完成',
  534. 'url' => !empty($referurl) ? $referurl : url('user/Level/level_centre'),
  535. ];
  536. return $msg;
  537. }
  538. }
  539. // 获取会员升级更新数组
  540. private function GetUsersUpgradeData($MoneyData = array())
  541. {
  542. $time = getTime();
  543. // 会员期限定义数组
  544. $limit_arr = Config::get('global.admin_member_limit_arr');
  545. // 查询会员升级级别
  546. $MoneyDataCause = unserialize($MoneyData['cause']);
  547. // 到期天数
  548. $maturity_days = $limit_arr[$MoneyDataCause['limit_id']]['maturity_days'];
  549. // 更新会员属性表的数组
  550. $result = [
  551. 'level' => $MoneyDataCause['level_id'],
  552. 'update_time' => $time,
  553. 'level_maturity_days' => Db::raw('level_maturity_days+'.($maturity_days)),
  554. ];
  555. // 查询会员开通会员级别时间和天数
  556. $UsersData = $this->users_db->field('open_level_time, level_maturity_days')->find($MoneyData['users_id']);
  557. // 36600为终身天数,若数据库中的值大于则不执行,反之执行
  558. if ($UsersData['level_maturity_days'] < '36600') {
  559. // 计算逻辑,会员开通的时间戳+(会员到期天数*每天的秒数)
  560. $maturity_time = $UsersData['open_level_time'] + ($UsersData['level_maturity_days'] * 86400);
  561. // 判断是否到期,到期则执行
  562. if ($maturity_time < $time) {
  563. // 会员已到期,追加数组
  564. $result['open_level_time'] = $time;
  565. $result['level_maturity_days'] = $maturity_days;
  566. }
  567. }
  568. return $result;
  569. }
  570. // 旧版加密方式,验证订单是否正确
  571. private function GetOldAliPayMd5Sign($param = array(), $code = null)
  572. {
  573. // 对关联数组按照键名进行升序排序
  574. ksort($param);
  575. reset($param);
  576. // 去除指定参数并拼装成字符串
  577. $sign = '';
  578. foreach ($param as $key => $value)
  579. {
  580. if (!in_array($key, ['sign','sign_type','transaction_type','is_notify','m','c','a','referurl']))
  581. {
  582. $sign .= "$key=$value&";
  583. }
  584. }
  585. // 参数拼装处理并加密为MD5返回
  586. $sign = md5(substr($sign, 0, -1).$code);
  587. return $sign;
  588. }
  589. // 新版加密方式,验证订单是否正确
  590. private function GetNewAliPayRsa2Return($data = array(), $pay_alipay_config = array())
  591. {
  592. // 参数拼装
  593. $config = [
  594. 'app_id' => $pay_alipay_config['app_id'],
  595. 'charset' => 'UTF-8',
  596. 'sign_type' => 'RSA2',
  597. 'gatewayUrl' => 'https://openapi.alipay.com/gateway.do',
  598. 'alipay_public_key' => $pay_alipay_config['alipay_public_key'],
  599. 'merchant_private_key' => $pay_alipay_config['merchant_private_key'],
  600. ];
  601. // 引入支付宝SDK
  602. vendor('alipay.pagepay.service.AlipayTradeService');
  603. // 实例化
  604. $alipaySevice = new \AlipayTradeService($config);
  605. // 删除参数
  606. unset($data['m']);
  607. unset($data['c']);
  608. unset($data['a']);
  609. unset($data['transaction_type']);
  610. unset($data['is_notify']);
  611. if (isset($data['referurl'])) {
  612. unset($data['referurl']);
  613. }
  614. // 获取返回值
  615. $return = $alipaySevice->check($data);
  616. return $return;
  617. }
  618. /*----------支付宝回调结束----------*/
  619. /*----------微信回调开始----------*/
  620. public function wechat_return($GetData = [])
  621. {
  622. $OrderData = $this->checkAmount($GetData['out_trade_no'],$GetData['total_amount'],$GetData['transaction_type']);
  623. if(empty($OrderData)){
  624. return "支付失败,支付金额与订单金额不相符";
  625. }
  626. // 查询订单是否真实已支付
  627. $PayOrder = $this->WeChatPayOrderInquire($GetData['out_trade_no']);
  628. // 已完成支付则执行下列操作
  629. if (!empty($PayOrder)) {
  630. // 拆分自定义参数
  631. $attach = explode('|,|', $GetData['attach']);
  632. // 订单查询
  633. if (1 == $attach[2]) {
  634. // 会员充值
  635. $where = [
  636. 'order_number' => $GetData['out_trade_no']
  637. ];
  638. if (!empty($attach[3])) $where['users_id'] = $attach[3];
  639. // 充值订单查询
  640. $MoneyData = $this->users_money_db->where($where)->find();
  641. if (empty($MoneyData['status']) || in_array($MoneyData['status'], [0, 4])) {
  642. // 充值订单无需处理,直接返回结束
  643. echo 'FAIL'; exit;
  644. } else if (in_array($MoneyData['status'], [1])) {
  645. // 充值订单支付成功后续处理
  646. $Result = $this->RechargeProcessing($GetData, $where, $MoneyData);
  647. // 返回结束
  648. if (!empty($Result)) echo 'SUCCESS'; exit;
  649. } else {
  650. // 充值订单已完成处理,直接返回结束
  651. echo 'SUCCESS'; exit;
  652. }
  653. } else if (2 == $attach[2]) {
  654. // 商品购买
  655. $where = [
  656. 'order_code' => $GetData['out_trade_no']
  657. ];
  658. if (!empty($attach[3])) $where['users_id'] = $attach[3];
  659. // 购物订单查询
  660. $OrderStatus = $this->shop_order_db->where($where)->getField('order_status');
  661. if (empty($OrderStatus) && 0 == $OrderStatus) {
  662. // 购物订单支付成功后续处理
  663. $Result = $this->ProductPayProcessing($attach[3], $GetData['out_trade_no'], $GetData);
  664. // 返回结束
  665. if (!empty($Result)) echo 'SUCCESS'; exit;
  666. } else if (in_array($OrderStatus, [1, 2, 3])) {
  667. // 购物订单已完成处理,直接返回结束
  668. echo 'SUCCESS'; exit;
  669. } else {
  670. // 购物订单无需处理,直接返回结束
  671. echo 'FAIL'; exit;
  672. }
  673. } else if (3 == $attach[2]) {
  674. // 会员升级
  675. $where = [
  676. 'order_number' => $GetData['out_trade_no']
  677. ];
  678. if (!empty($attach[3])) $where['users_id'] = $attach[3];
  679. // 升级订单查询
  680. $MoneyData = $this->users_money_db->where($where)->find();
  681. if (empty($MoneyData['status']) || in_array($MoneyData['status'], [0, 4])) {
  682. // 升级订单无需处理,直接返回结束
  683. echo 'FAIL'; exit;
  684. } else if (in_array($MoneyData['status'], [1])) {
  685. // 升级订单支付成功后续处理
  686. $Result = $this->UpgradeProcessing($GetData, $where, $MoneyData);
  687. // 返回结束
  688. if (!empty($Result)) echo 'SUCCESS'; exit;
  689. } else {
  690. // 升级订单已完成处理,直接返回结束
  691. echo 'SUCCESS'; exit;
  692. }
  693. } else if (8 == $attach[2]) {
  694. // 视频购买
  695. $where = [
  696. 'order_code' => $GetData['out_trade_no']
  697. ];
  698. if (!empty($attach[3])) $where['users_id'] = $attach[3];
  699. // 视频订单查询
  700. $OrderStatus = $this->media_order_db->where($where)->getField('order_status');
  701. if (empty($OrderStatus) && 0 == $OrderStatus) {
  702. // 视频订单支付成功后续处理
  703. $Result = $this->MediaPayProcessing($GetData, $where);
  704. // 返回结束
  705. if (!empty($Result)) echo 'SUCCESS'; exit;
  706. } else if (in_array($OrderStatus, [1])) {
  707. // 视频订单已完成处理,直接返回结束
  708. echo 'SUCCESS'; exit;
  709. } else {
  710. // 视频订单无需处理,直接返回结束
  711. echo 'FAIL'; exit;
  712. }
  713. }else if (9 == $attach[2]) {
  714. // 文章购买
  715. $where = [
  716. 'order_code' => $GetData['out_trade_no']
  717. ];
  718. if (!empty($attach[3])) $where['users_id'] = $attach[3];
  719. // 文章订单查询
  720. $OrderStatus = $this->article_order_db->where($where)->getField('order_status');
  721. if (empty($OrderStatus) && 0 == $OrderStatus) {
  722. // 文章订单支付成功后续处理
  723. $Result = $this->ArticlePayProcessing($GetData, $where);
  724. // 返回结束
  725. if (!empty($Result)) echo 'SUCCESS'; exit;
  726. } else if (in_array($OrderStatus, [1])) {
  727. // 视频订单已完成处理,直接返回结束
  728. echo 'SUCCESS'; exit;
  729. } else {
  730. // 视频订单无需处理,直接返回结束
  731. echo 'FAIL'; exit;
  732. }
  733. }else if (10 == $attach[2]) {
  734. // 下载模型购买
  735. $where = [
  736. 'order_code' => $GetData['out_trade_no']
  737. ];
  738. if (!empty($attach[3])) $where['users_id'] = $attach[3];
  739. // 文章订单查询
  740. $OrderStatus = $this->download_order_db->where($where)->getField('order_status');
  741. if (empty($OrderStatus) && 0 == $OrderStatus) {
  742. // 下载模型订单支付成功后续处理
  743. $Result = $this->DownloadPayProcessing($GetData, $where);
  744. // 返回结束
  745. if (!empty($Result)) echo 'SUCCESS'; exit;
  746. } else if (in_array($OrderStatus, [1])) {
  747. // 视频订单已完成处理,直接返回结束
  748. echo 'SUCCESS'; exit;
  749. } else {
  750. // 视频订单无需处理,直接返回结束
  751. echo 'FAIL'; exit;
  752. }
  753. }else if (99 == $attach[2]){
  754. // 商品购买
  755. $where = [
  756. 'order_code' => $GetData['out_trade_no']
  757. ];
  758. if (!empty($attach[3])) $where['users_id'] = $attach[3];
  759. // 购物订单查询
  760. $OrderStatus = $this->shop_order_db->where($where)->getField('order_status');
  761. if (empty($OrderStatus) && 0 == $OrderStatus) {
  762. // 购物订单支付成功后续处理
  763. $Result = $this->ProductPayProcessing($attach[3], $GetData['out_trade_no'], $GetData);
  764. // 返回结束
  765. if (!empty($Result)) echo 'SUCCESS'; exit;
  766. } else if (in_array($OrderStatus, [1, 2, 3])) {
  767. // 购物订单已完成处理,直接返回结束
  768. echo 'SUCCESS'; exit;
  769. } else {
  770. // 购物订单无需处理,直接返回结束
  771. echo 'FAIL'; exit;
  772. }
  773. }
  774. }
  775. }
  776. // 查询订单是否真实已支付
  777. private function WeChatPayOrderInquire($out_trade_no = null)
  778. {
  779. $Result = false;
  780. // 查询微信支付配置
  781. $where = [
  782. 'pay_id' => 1,
  783. 'pay_mark' => 'wechat'
  784. ];
  785. $PayInfo = Db::name('pay_api_config')->where($where)->getField('pay_info');
  786. // 查询订单是否支付
  787. if (!empty($out_trade_no) && !empty($PayInfo)) {
  788. // 引入SDK
  789. vendor('wechatpay.lib.WxPayApi');
  790. vendor('wechatpay.lib.WxPayConfig');
  791. // 实例化加载订单号
  792. $WxPayOrderQuery = new \WxPayOrderQuery;
  793. $WxPayOrderQuery->SetOut_trade_no($out_trade_no);
  794. // 处理微信配置数据
  795. $PayInfo = unserialize($PayInfo);
  796. $ApiConfig['app_id'] = $PayInfo['appid'];
  797. $ApiConfig['mch_id'] = $PayInfo['mchid'];
  798. $ApiConfig['key'] = $PayInfo['key'];
  799. // 实例化微信配置
  800. $WxPayConfig = new \WxPayConfig($ApiConfig);
  801. $WxPayApi = new \WxPayApi;
  802. if (!empty($WxPayConfig->app_id)) {
  803. // 判断结果
  804. $WeChatOrder = $WxPayApi->orderQuery($WxPayConfig, $WxPayOrderQuery);
  805. if (isset($WeChatOrder['return_code']) && $WeChatOrder['return_code'] == 'SUCCESS' && $WeChatOrder['result_code'] == 'SUCCESS') {
  806. if ($WeChatOrder['trade_state'] == 'SUCCESS' && !empty($WeChatOrder['transaction_id'])) {
  807. $Result = true;
  808. }
  809. }
  810. }
  811. }
  812. // 返回查询结果
  813. return $Result;
  814. }
  815. // 会员充值处理
  816. private function RechargeProcessing($GetData = [], $Where = [], $MoneyData = [])
  817. {
  818. $Return = false;
  819. // 充值订单更新为已付款
  820. $data = [
  821. 'status' => 2,
  822. 'update_time' => getTime(),
  823. 'pay_details' => serialize($GetData)
  824. ];
  825. // 充值订单更新
  826. $ResultID = $this->users_money_db->where($Where)->update($data);
  827. if (!empty($ResultID)) {
  828. // 同步修改会员的金额
  829. $UsersWhere = [
  830. 'users_id' => $MoneyData['users_id']
  831. ];
  832. $UpUsersData = [
  833. 'users_money' => Db::raw('users_money+'.($MoneyData['money']))
  834. ];
  835. $UpdateID = $this->users_db->where($UsersWhere)->update($UpUsersData);
  836. // 业务处理完成,订单已完成
  837. if (!empty($UpdateID)) {
  838. $data2 = [
  839. 'status' => 3,
  840. 'update_time' => getTime()
  841. ];
  842. $this->users_money_db->where($Where)->update($data2);
  843. $Return = true;
  844. }
  845. }
  846. // 返回执行结果
  847. return $Return;
  848. }
  849. // 商品订单处理
  850. private function ProductPayProcessing($users_id = null, $order_code = [], $pay_details = [])
  851. {
  852. // 商品订单处理
  853. $Result = pay_success_logic($users_id, $order_code, $pay_details, 'wechat', false);
  854. $Return = !empty($Result['code']) && 1 == $Result['code'] ? true : false;
  855. // 返回执行结果
  856. return $Return;
  857. }
  858. // 会员升级处理
  859. private function UpgradeProcessing($GetData = [], $Where = [], $MoneyData = [])
  860. {
  861. $Return = false;
  862. // 查询会员升级类型
  863. $CauseData = unserialize($MoneyData['cause']);
  864. $UsersTypeData = $this->users_type_manage_db->where('type_id', $CauseData['type_id'])->find();
  865. if (!empty($UsersTypeData)) {
  866. // 更新数据
  867. $UpMoneyData = [
  868. 'status' => 2,
  869. 'update_time' => getTime()
  870. ];
  871. // 升级订单更新
  872. $ResultID = $this->users_money_db->where($Where)->update($UpMoneyData);
  873. if (!empty($ResultID)) {
  874. // 更新会员数据
  875. $Where = [
  876. 'users_id' => $MoneyData['users_id']
  877. ];
  878. $UpUsersData = $this->GetUsersUpgradeData($MoneyData);
  879. $UpdateID = $this->users_db->where($Where)->update($UpUsersData);
  880. if (!empty($UpdateID)) $Return = true;
  881. }
  882. }
  883. // 返回执行结果
  884. return $Return;
  885. }
  886. // 会员购买视频处理
  887. private function MediaPayProcessing($GetData = [], $Where = [])
  888. {
  889. // 视频订单更新数据,更新为已付款
  890. $OrderData = [
  891. 'order_status' => 1,
  892. 'pay_details' => serialize($GetData),
  893. 'pay_time' => getTime(),
  894. 'update_time' => getTime()
  895. ];
  896. // 视频订单更新
  897. $UpdateID = Db::name('media_order')->where($Where)->update($OrderData);
  898. $Return = !empty($UpdateID) ? true : false;
  899. // 返回执行结果
  900. return $Return;
  901. }
  902. // 会员购买文章处理
  903. private function ArticlePayProcessing($GetData = [], $Where = [])
  904. {
  905. // 视频订单更新数据,更新为已付款
  906. $OrderData = [
  907. 'order_status' => 1,
  908. 'pay_details' => serialize($GetData),
  909. 'pay_time' => getTime(),
  910. 'update_time' => getTime()
  911. ];
  912. // 视频订单更新
  913. $UpdateID = $this->article_order_db->where($Where)->update($OrderData);
  914. $Return = !empty($UpdateID) ? true : false;
  915. // 返回执行结果
  916. return $Return;
  917. }
  918. // 会员购买下载模型处理
  919. private function DownloadPayProcessing($GetData = [], $Where = [])
  920. {
  921. // 订单更新数据,更新为已付款
  922. $OrderData = [
  923. 'order_status' => 1,
  924. 'pay_details' => serialize($GetData),
  925. 'pay_time' => getTime(),
  926. 'update_time' => getTime()
  927. ];
  928. // 订单更新
  929. $UpdateID = $this->download_order_db->where($Where)->update($OrderData);
  930. $Return = !empty($UpdateID) ? true : false;
  931. // 返回执行结果
  932. return $Return;
  933. }
  934. /*----------微信回调结束----------*/
  935. //检验订单支付金额是否相符
  936. public function checkAmount($unified_number = '', $unified_amount = 0, $transaction_type = 2)
  937. {
  938. $OrderData = [];
  939. // 商城订单购买支付回调处理
  940. if (2 === intval($transaction_type)) {
  941. $OrderData = $this->shop_order_db->where(['order_code' => $unified_number,'order_amount' => $unified_amount])->find();
  942. }
  943. // 会员充值或升级支付回调处理
  944. else if (1 === intval($transaction_type) || 3 == intval($transaction_type)) {
  945. $OrderData = $this->users_money_db->where(['money' => $unified_amount,'order_number' => $unified_number])->find();
  946. }
  947. // 视频购买
  948. else if (8 === intval($transaction_type)) {
  949. $OrderData = $this->media_order_db->where(['order_code' => $unified_number,'order_amount' => $unified_amount])->find();
  950. }
  951. // 文章购买
  952. else if (9 === intval($transaction_type)) {
  953. $OrderData = $this->article_order_db->where(['order_code' =>$unified_number,'order_amount' => $unified_amount])->find();
  954. }
  955. // 下载模型购买
  956. else if (10 === intval($transaction_type)) {
  957. $OrderData = $this->download_order_db->where(['order_code' =>$unified_number,'order_amount' => $unified_amount])->find();
  958. }
  959. // 会员充值套餐购买
  960. else if (20 === intval($transaction_type)) {
  961. $OrderData = Db::name('users_recharge_pack_order')->where(['order_pay_code'=>$unified_number, 'order_pay_prices'=>$unified_amount])->find();
  962. }
  963. // 多商家商城购买
  964. else if (99 === intval($transaction_type)) {
  965. $OrderData = Db::name("shop_order_unified_pay")->where(['unified_number' => $unified_number,'unified_amount' => $unified_amount])->find();
  966. }
  967. return $OrderData;
  968. }
  969. }