Pay.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Config;
  5. use think\Db;
  6. /**
  7. * 会员接口
  8. */
  9. class Pay extends Api
  10. {
  11. protected $noNeedLogin = ['notify'];
  12. protected $noNeedRight = '*';
  13. public function _initialize()
  14. {
  15. parent::_initialize();
  16. if (!Config::get('fastadmin.usercenter')) {
  17. $this->error(__('User center already closed'));
  18. }
  19. }
  20. //二合一支付
  21. public function pay() {
  22. $user_token = input('user_token', '', 'trim');
  23. $diversionfst = input('diversionfst', '', 'trim'); //位置等导流标识
  24. $diversioncsk = input('diversioncsk', '', 'trim'); //内容等导流标识
  25. $trace_id = input('trace_id', '', 'trim'); //大数据推荐内容有trace_id
  26. $contentid = input('contentid', '', 'trim'); //订购媒资的媒资movie_id
  27. if (!$user_token) {
  28. $this->error('参数缺失');
  29. }
  30. //生成支付订单记录
  31. $rechar_order['user_id'] = $this->auth->id;
  32. $rechar_order['order_no'] = date('YmdHis', time()) . $this->auth->id . rand(10000000, 99999999); //微信订单编号
  33. $rechar_order['money'] = 49;
  34. $rechar_order['purpose'] = 3; //充值用途:1=支付订单,2=充值,3=开通会员
  35. $rechar_order['pay_type'] = 'zhuowang';
  36. $rechar_order['relation_id'] = 1;
  37. $rechar_order['createtime'] = time();
  38. //开始事务
  39. $result = Db::name('rechar_order')->insertGetId($rechar_order);
  40. if (!$result) {
  41. $this->error('网络延迟,请稍后再试');
  42. }
  43. $url = 'http://' . config('pay_ip') . '/HDC/3.0/hop/svc/pay/toPay.ajax';
  44. $data = [
  45. 'transId' => $rechar_order['order_no'],
  46. 'orderNo' => $rechar_order['order_no'],
  47. 'userToken' => $user_token,
  48. 'notifyUrl' => config('img_url') . '/api/pay/notify',
  49. 'backUrl' => 'http://aspire.jshdc.payback',
  50. 'deskCode' => config('desk_code'),
  51. 'products' => [
  52. [
  53. 'productCode' => config('product_code'),
  54. 'productPrice' => '29',
  55. 'productUnit' => '个',
  56. 'productCount' => 1
  57. ],
  58. /*[
  59. 'productCode' => '005912cp0028',
  60. 'productPrice' => '0',
  61. 'productUnit' => '7天体验',
  62. 'productCount' => 1
  63. ],*/
  64. [
  65. 'productCode' => '005912cp0004',
  66. 'productPrice' => '0',
  67. 'productUnit' => '30天体验',
  68. 'productCount' => 1
  69. ],
  70. [
  71. 'productCode' => '0002112cp0030',
  72. 'productPrice' => '0',
  73. 'productUnit' => '90天体验',
  74. 'productCount' => 1
  75. ],
  76. [
  77. 'productCode' => '005911cp0022',
  78. 'productPrice' => '199',
  79. 'productUnit' => '年度特惠',
  80. 'productCount' => 1
  81. ],
  82. [
  83. 'productCode' => '005912cp0051',
  84. 'productPrice' => '119',
  85. 'productUnit' => '半年特惠',
  86. 'productCount' => 1
  87. ],
  88. [
  89. 'productCode' => '005912cp0052',
  90. 'productPrice' => '59',
  91. 'productUnit' => '季度特惠',
  92. 'productCount' => 1
  93. ],
  94. [
  95. 'productCode' => '005912cp0044',
  96. 'productPrice' => '20',
  97. 'productUnit' => '咪咕叠加包',
  98. 'productCount' => 1
  99. ],
  100. [
  101. 'productCode' => '005912cp0047',
  102. 'productPrice' => '30',
  103. 'productUnit' => '咪咕叠加包',
  104. 'productCount' => 1
  105. ],
  106. [
  107. 'productCode' => '0002112cp0031',
  108. 'productPrice' => '39',
  109. 'productUnit' => '畅享会员',
  110. 'productCount' => 1
  111. ],
  112. [
  113. 'productCode' => '005912cp0035',
  114. 'productPrice' => '20',
  115. 'productUnit' => '两年特惠',
  116. 'productCount' => 1
  117. ],
  118. [
  119. 'productCode' => '005912cp0043',
  120. 'productPrice' => '20',
  121. 'productUnit' => '两年折扣',
  122. 'productCount' => 1
  123. ],
  124. /*[
  125. 'productCode' => '005912cp0034',
  126. 'productPrice' => '468',
  127. 'productUnit' => '两年特惠',
  128. 'productCount' => 1
  129. ],*/
  130. [
  131. 'productCode' => '005912cp0046',
  132. 'productPrice' => '49',
  133. 'productUnit' => '连续包月',
  134. 'productCount' => 1
  135. ],
  136. /*[
  137. 'productCode' => '005912cp0039',
  138. 'productPrice' => '12',
  139. 'productUnit' => '一日会员',
  140. 'productCount' => 1
  141. ],*/
  142. [
  143. 'productCode' => '0002112cp0031T1',
  144. 'productPrice' => '1',
  145. 'productUnit' => '畅享折扣',
  146. 'productCount' => 1
  147. ],
  148. [
  149. 'productCode' => '0002112cp0031T9',
  150. 'productPrice' => '9',
  151. 'productUnit' => '畅享折扣',
  152. 'productCount' => 1
  153. ],
  154. [
  155. 'productCode' => '0002112cp0031T19',
  156. 'productPrice' => '19',
  157. 'productUnit' => '畅享折扣',
  158. 'productCount' => 1
  159. ],
  160. [
  161. 'productCode' => '0002112cp0029',
  162. 'productPrice' => '29',
  163. 'productUnit' => '限时特惠',
  164. 'productCount' => 1
  165. ],
  166. [
  167. 'productCode' => '005912cp0002',
  168. 'productPrice' => '35',
  169. 'productUnit' => '畅享特惠',
  170. 'productCount' => 1
  171. ],
  172. /*[
  173. 'productCode' => '0002112cp0031Z39',
  174. 'productPrice' => '39',
  175. 'productUnit' => '畅享会员',
  176. 'productCount' => 1
  177. ],*/
  178. /*[
  179. 'productCode' => '005912cp0040',
  180. 'productPrice' => '39',
  181. 'productUnit' => '月包会员',
  182. 'productCount' => 1
  183. ],*/
  184. [
  185. 'productCode' => '005912cp0036',
  186. 'productPrice' => '105',
  187. 'productUnit' => '连续包季',
  188. 'productCount' => 1
  189. ],
  190. [
  191. 'productCode' => '005912cp0021',
  192. 'productPrice' => '299',
  193. 'productUnit' => '连续包年',
  194. 'productCount' => 1
  195. ],
  196. [
  197. 'productCode' => '005912cp0012',
  198. 'productPrice' => '365',
  199. 'productUnit' => '连续包年',
  200. 'productCount' => 1
  201. ],
  202. [
  203. 'productCode' => '005912cp0037',
  204. 'productPrice' => '59',
  205. 'productUnit' => '30天会员',
  206. 'productCount' => 1
  207. ],
  208. [
  209. 'productCode' => '005911cp0001',
  210. 'productPrice' => '111',
  211. 'productUnit' => '季度会员',
  212. 'productCount' => 1
  213. ],
  214. [
  215. 'productCode' => '005911cp0005',
  216. 'productPrice' => '209',
  217. 'productUnit' => '半年会员',
  218. 'productCount' => 1
  219. ],
  220. [
  221. 'productCode' => '005911cp0002',
  222. 'productPrice' => '399',
  223. 'productUnit' => '年度会员',
  224. 'productCount' => 1
  225. ],
  226. [
  227. 'productCode' => '005911cp0138',
  228. 'productPrice' => '10',
  229. 'productUnit' => '爱家悦享升档包',
  230. 'productCount' => 1
  231. ],
  232. [
  233. 'productCode' => '005911cp0139',
  234. 'productPrice' => '20',
  235. 'productUnit' => '爱家悦享升档包(含奇异果)',
  236. 'productCount' => 1
  237. ]
  238. ],
  239. 'diversionCode' => config('product_code')
  240. ];
  241. if ($diversionfst) {
  242. $data['diversionFST'] = 'SC::' . $diversionfst;
  243. }
  244. if ($diversioncsk) {
  245. $data['diversionCSK'] = '::' . $diversioncsk;
  246. }
  247. if ($trace_id) {
  248. $data['trace_id'] = $trace_id;
  249. }
  250. if ($contentid) {
  251. $data['contentid'] = $contentid;
  252. }
  253. $data = json_encode($data, 320);
  254. //获取鉴权token
  255. $sign_bytes = '/3.0/hop/svc/pay/toPay.ajax' . $this->base16_encode(md5($data));
  256. $hdc_token = hash_hmac('sha256', $sign_bytes, base64_decode(config('pay_appkey')), false);
  257. $header = [
  258. 'Host:' . config('pay_ip'),
  259. 'HDC-Service:2',
  260. 'HDC-APPID:' . config('pay_appid'),
  261. 'HDC-Token:' . $hdc_token,
  262. 'Content-Type:application/json'
  263. ];
  264. $rs = httpRequest($url, 'POST', $data, $header);
  265. if (!$rs) {
  266. $this->error('您的网络开小差了~');
  267. }
  268. $rs = json_decode($rs, true);
  269. if ($rs['resultCode'] != 0) {
  270. $this->error('您的网络开小差了~');
  271. }
  272. $this->success('链接', $rs['data']);
  273. }
  274. //SDK二合一支付
  275. public function paysdk() {
  276. $user_token = input('user_token', '', 'trim');
  277. $diversionfst = input('diversionfst', '', 'trim'); //位置等导流标识
  278. $diversioncsk = input('diversioncsk', '', 'trim'); //内容等导流标识
  279. $trace_id = input('trace_id', '', 'trim'); //大数据推荐内容有trace_id
  280. $contentid = input('contentid', '', 'trim'); //订购媒资的媒资movie_id
  281. if (!$user_token) {
  282. $this->error('参数缺失');
  283. }
  284. //生成支付订单记录
  285. $rechar_order['user_id'] = $this->auth->id;
  286. $rechar_order['order_no'] = date('YmdHis', time()) . $this->auth->id . rand(10000000, 99999999); //微信订单编号
  287. $rechar_order['money'] = 49;
  288. $rechar_order['purpose'] = 3; //充值用途:1=支付订单,2=充值,3=开通会员
  289. $rechar_order['pay_type'] = 'zhuowang';
  290. $rechar_order['relation_id'] = 1;
  291. $rechar_order['createtime'] = time();
  292. //多出来的统计数据
  293. $rechar_order['diversionfst'] = $diversionfst;
  294. $rechar_order['diversioncsk'] = $diversioncsk;
  295. $rechar_order['trace_id'] = $trace_id;
  296. $rechar_order['video_id'] = $contentid;
  297. //开始事务
  298. $result = Db::name('rechar_order')->insertGetId($rechar_order);
  299. if (!$result) {
  300. $this->error('网络延迟,请稍后再试');
  301. }
  302. $url = 'http://' . config('pay_ip') . '/HDC/3.0/hop/svc/pay/toPay.ajax';
  303. $data = [
  304. 'transId' => $rechar_order['order_no'],
  305. 'orderNo' => $rechar_order['order_no'],
  306. 'userToken' => $user_token,
  307. 'notifyUrl' => config('img_url') . '/api/pay/notify',
  308. 'backUrl' => 'http://aspire.jshdc.payback',
  309. 'deskCode' => config('desk_code'),
  310. 'products' => [
  311. [
  312. 'productCode' => config('product_code'),
  313. 'productPrice' => '29',
  314. 'productUnit' => '个',
  315. 'productCount' => 1
  316. ]
  317. ],
  318. 'diversionCode' => config('product_code')
  319. ];
  320. $vip_jifei = Db::name('vip_jifei')->where('pay_switch',1)->where('deletetime',NULL)
  321. ->field('productcode as productCode,productprice as productPrice,productunit as productUnit,productcount as productCount')
  322. ->select();
  323. $data['products'] = array_merge($data['products'],$vip_jifei);
  324. if ($diversionfst) {
  325. $data['diversionFST'] = 'SC::' . $diversionfst;
  326. }
  327. if ($diversioncsk) {
  328. $data['diversionCSK'] = '::' . $diversioncsk;
  329. }
  330. if ($trace_id) {
  331. $data['trace_id'] = $trace_id;
  332. }
  333. if ($contentid) {
  334. $data['contentid'] = $contentid;
  335. }
  336. // $data2 = $data;
  337. $data = json_encode($data, 320);
  338. //获取鉴权token
  339. // $sign_bytes = strtolower($this->base16_encode(md5($data)));
  340. // $sign_bytes = strtoupper($this->base16_encode($sign_bytes));
  341. $sign_bytes = $this->base16_encode(md5($data));
  342. $hdc_token = hash_hmac('sha256', $sign_bytes, config('pay_appkey'), false);
  343. // $hdc_token = strtolower($this->base16_encode($hdc_token));
  344. $header = [
  345. 'Host:' . config('pay_ip'),
  346. 'HDC-Service:2',
  347. 'HDC-APPID:' . config('pay_appid'),
  348. 'HDC-Token:' . $hdc_token,
  349. 'Content-Type:application/json'
  350. ];
  351. $return_data['url'] = $url;
  352. $return_data['data'] = $data;
  353. $return_data['host'] = config('pay_ip');
  354. $return_data['appid'] = config('pay_appid');
  355. $return_data['token'] = $hdc_token;
  356. $return_data['apptoken'] = config('pay_appkey');
  357. /*$rs = httpRequest($url, 'POST', $data, $header);
  358. if (!$rs) {
  359. $this->error('您的网络开小差了~');
  360. }
  361. $rs = json_decode($rs, true);
  362. if ($rs['resultCode'] != 0) {
  363. $this->error('您的网络开小差了~');
  364. }*/
  365. $this->success('参数', $return_data);
  366. }
  367. //支付回调
  368. public function notify() {
  369. $data = file_get_contents('php://input');
  370. if (!$data) {
  371. $this->error('参数缺失');
  372. }
  373. $data = json_decode($data, true);
  374. if (!$data) {
  375. $this->error('参数缺失');
  376. }
  377. if ($data['orderstate'] != 0) {
  378. echo json_encode([
  379. 'transid' => $data['transid'],
  380. 'orderid' => $data['orderid'],
  381. 'statuscode' => -1,
  382. 'statusdesc' => '订单状态失败'
  383. ], 320);
  384. die;
  385. }
  386. $order_no = $data['orderid'];
  387. $where['order_no'] = $order_no;
  388. $order_info = Db::name('rechar_order')->where($where)->find();
  389. if (!$order_info) {
  390. echo json_encode([
  391. 'transid' => $data['transid'],
  392. 'orderid' => $data['orderid'],
  393. 'statuscode' => -1,
  394. 'statusdesc' => '业务订单不存在'
  395. ], 320);
  396. die;
  397. }
  398. if ($order_info['status'] == 1) { //已回调成功
  399. echo json_encode([
  400. 'transid' => $data['transid'],
  401. 'orderid' => $data['orderid'],
  402. 'statuscode' => 0,
  403. 'statusdesc' => ''
  404. ], 320);
  405. die;
  406. }
  407. //构建支付订单处理结果信息
  408. $_data['status'] = 1;
  409. $_data['updatetime'] = time();
  410. if ($order_info['purpose'] == 3) {
  411. //开通会员
  412. Db::startTrans();
  413. //充值
  414. $rs = Db::name('user')->where(['id' => $order_info['user_id']])->setField('is_vip', 1);
  415. if ($rs === false) {
  416. Db::rollback();
  417. $_data['pay_status'] = 3; //回调状态:1=支付订单回调失败,2=充值回调失败,3=开通会员回调失败
  418. } else {
  419. Db::commit();
  420. }
  421. }
  422. //修改订单信息
  423. Db::name('rechar_order')->where($where)->setField($_data);
  424. echo json_encode([
  425. 'transid' => $data['transid'],
  426. 'orderid' => $data['orderid'],
  427. 'statuscode' => 0,
  428. 'statusdesc' => ''
  429. ], 320);
  430. }
  431. }