Payios.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use addons\epay\library\Service;
  6. /**
  7. * 充值配置与充值订单
  8. */
  9. class Payios extends Api
  10. {
  11. protected $noNeedLogin = ['auto_renewal_vip_notify'];
  12. protected $noNeedRight = ['*'];
  13. //vip ios用的
  14. public function vip_config_ios(){
  15. $list = Db::name('payvip_config_ios')->field('id,money,days,title,info,bundle_id,is_lianxu')->where('is_show',1)->order('weight asc,id asc')->select();
  16. $data['vipconfig'] = $list;
  17. $data['vip_endtime'] = model('wallet')->getWallet($this->auth->id,'vip_endtime');
  18. $data['is_vip'] = $data['vip_endtime'] > time() ? 1 : 0;
  19. $data['avatar'] = localpath_to_netpath($this->auth->avatar);
  20. $this->success('success',$data);
  21. }
  22. //vip用的,创建订单
  23. public function vip_recharge_ios(){
  24. $rc_id = input('rc_id',0);
  25. $platform = 'app';
  26. $uid = $this->auth->id;
  27. if(!$rc_id){
  28. $this->error('请选择会员套餐');
  29. }
  30. if(!$this->user_auth_limit()){
  31. $this->error('请先完成实名认证');
  32. }
  33. //赋值money
  34. $recharge_config = Db::name('payvip_config_ios')->where('id',$rc_id)->find();
  35. $money = $recharge_config['money'];
  36. if($money<=0)
  37. {
  38. $this->error('支付金额必须大于0');
  39. }
  40. if($money > 10000){
  41. $this->error('支付金额太大');
  42. }
  43. //创建订单
  44. $data = [];
  45. $data['user_id'] = $uid;
  46. $data['out_trade_no'] = createUniqueNo('V',$uid); // 数据库订单号加密
  47. $data['order_amount'] = $money;
  48. $data['createtime'] = time();
  49. $data['pay_type'] = 'ios';
  50. $data['platform'] = $platform;
  51. $data['order_status'] = 0;
  52. $data['table_name'] = 'vip_recharge';
  53. $data['table_id'] = 0;
  54. $data['args'] = json_encode(['days'=>$recharge_config['days']]);
  55. $data['bundle_id'] = $recharge_config['bundle_id'];
  56. $data['is_lianxu'] = $recharge_config['is_lianxu'];
  57. $orderid = Db::name('pay_order')->insertGetId($data);
  58. $this->success('success',$data['out_trade_no']);
  59. }
  60. //vip,苹果内购支付回调
  61. //因前端不传入订单号,作废,且与gold_notify_iosnew 已完成合并
  62. public function vip_notify_ios(){
  63. //苹果内购的验证收据
  64. $receipt_data = input('apple_receipt', '', 'trim');
  65. $order_no = input('order_no', '', 'trim');
  66. filePut('VIP ios充值:参数apple_receipt='.$receipt_data.',order_no='.$order_no);
  67. if (!$receipt_data || !$order_no) {
  68. $this->error('缺少参数');
  69. }
  70. Db::startTrans();
  71. // 查找订单
  72. $order_info = Db::name('pay_order')->where(['out_trade_no' => $order_no])->lock(true)->find();
  73. if (!$order_info) {
  74. Db::rollback();
  75. filePut('VIP ios充值:订单丢失out_trade_no='.$order_no);
  76. $this->error('订单丢失');
  77. }
  78. if ($order_info['order_status'] == 1) {
  79. Db::rollback();
  80. $this->success('充值成功');
  81. }
  82. // 验证支付状态
  83. $result = $this->validate_apple_pay($receipt_data);
  84. if (!$result['status']) {// 验证不通过
  85. Db::rollback();
  86. filePut('VIP ios充值:验证out_trade_no='.$order_no.','.$result['message']);
  87. $this->error($result['message']);
  88. }
  89. $count = count($result['data']['receipt']['in_app']);
  90. $use_count = $count - 1;
  91. $args = json_decode($order_info['args'],true);
  92. if ($result['data']['receipt']['in_app'][$use_count]['product_id'] != $order_info['bundle_id']) {
  93. Db::rollback();
  94. filePut('VIP ios充值:非法请求,请立刻停止out_trade_no='.$order_no.','.$result['message'].','.$result['data']['receipt']['in_app'][$use_count]['product_id'].'!='.$order_info['bundle_id']);
  95. $this->error('非法请求,请立刻停止');
  96. }
  97. //逻辑开始
  98. //先充值
  99. $user_info = Db::name('user_wallet')->where('user_id',$order_info['user_id'])->lock(true)->find();
  100. if($user_info['vip_endtime'] < time()){
  101. //过期了
  102. $vip_endtime = time() + (intval($args['days']) * 86400);
  103. }else{
  104. //追加vip
  105. $vip_endtime = $user_info['vip_endtime'] + (intval($args['days']) * 86400);
  106. }
  107. $update_data = [
  108. 'vip_endtime'=>$vip_endtime,
  109. ];
  110. $result = Db::name('user_wallet')->where('user_id',$order_info['user_id'])->update($update_data);
  111. if($result === false)
  112. {
  113. Db::rollback();
  114. filePut('VIP ios充值:验证out_trade_no='.$order_no.',逻辑续费vip时间失败');
  115. $this->error('充值失败');
  116. }
  117. // 修改订单状态
  118. $ros = Db::name('pay_order')->where(['out_trade_no' => $order_no])->update(['order_status'=>1,'notifytime'=>time()]);
  119. if($ros === false) {
  120. filePut('VIP ios充值:充值失败out_trade_no='.$order_no.','.$result['message'].','.'修改订单状态失败');
  121. Db::rollback();
  122. $this->error('充值失败');
  123. }
  124. Db::commit();
  125. $this->success('充值成功');
  126. //逻辑结束
  127. }
  128. ////////////////////////////////
  129. //金币充值
  130. public function gold_config_ios(){
  131. $list = Db::name('paygold_webcon_ios')->field('id,money,gold,bundle_id')->where('is_show',1)->order('weigh asc,id asc')->select();
  132. $data['goldconfig'] = $list;
  133. $wallet = model('wallet')->getWallet($this->auth->id);
  134. $data['wallet'] = $wallet;
  135. $data['money_to_gold'] = config('site.money_to_gold');
  136. $this->success('success',$data);
  137. }
  138. //充值金币 创建订单
  139. public function gold_recharge_ios(){
  140. $rc_id = input_post('rc_id',0);
  141. $pay_type = 'ios';
  142. $platform = 'app';
  143. $uid = $this->auth->id;
  144. if(!$rc_id){
  145. $this->error('请选择充值金额');
  146. }
  147. if(!$this->user_auth_limit()){
  148. $this->error('请先完成实名认证');
  149. }
  150. //赋值money
  151. $recharge_config = Db::name('paygold_webcon_ios')->where('id',$rc_id)->find();
  152. $money = $recharge_config['money'] ?: 0;
  153. $gold = $recharge_config['gold'] ?: 0;
  154. //
  155. if($money<=0)
  156. {
  157. $this->error('支付金额必须大于0');
  158. }
  159. if($money > 10000){
  160. $this->error('支付金额太大');
  161. }
  162. //创建订单
  163. $data['user_id'] = $uid;
  164. $data['out_trade_no'] = createUniqueNo('P',$uid); // 数据库订单号加密
  165. $data['order_amount'] = $money;
  166. $data['createtime'] = time();
  167. $data['pay_type'] = $pay_type;
  168. $data['platform'] = $platform;
  169. $data['order_status'] = 0;
  170. $data['table_name'] = 'gold_recharge';
  171. $data['table_id'] = 0;
  172. $data['args'] = json_encode(['gold'=>$gold]);
  173. $data['bundle_id'] = $recharge_config['bundle_id'];
  174. $orderid = Db::name('pay_order')->insertGetId($data);
  175. $this->success('success',$data['out_trade_no']);
  176. }
  177. //订阅信息处理,续订情况下,单独分支方法
  178. public function expires(){
  179. //苹果订阅的验证收据
  180. $original_transaction_id = input('original_transaction_id','','trim');
  181. $receipt_data = input('apple_receipt', '', 'trim');
  182. $transaction_id = input('transaction_id', '', 'trim');
  183. if (!$receipt_data || !$original_transaction_id || !$transaction_id) {
  184. $this->error('缺少参数');
  185. }
  186. filePut("\r\n\r\n".'新请求VIP订阅');
  187. $prefix = 'iosVIP订阅'.',传入original_transaction_id:'.$original_transaction_id.',传入transaction_id:'.$transaction_id.'。';
  188. filePut($prefix.'参数apple_receipt:'.$receipt_data);
  189. //检查重复订单
  190. $check_map = [
  191. 'original_transaction_id' => $original_transaction_id,
  192. 'transaction_id' => $transaction_id,
  193. ];
  194. $check_order = Db::name('user_vipxufei_task')->where($check_map)->field('id')->find();
  195. if($check_order){
  196. filePut($prefix.'续费早已完成');
  197. $this->success('充值已完成');
  198. }
  199. Db::startTrans();
  200. //查找订单
  201. $order_map = [
  202. 'original_transaction_id' => $original_transaction_id,
  203. ];
  204. $order_info = Db::name('user_vipxufei_task')->where($order_map)->order('expires_date_ms desc')->lock(true)->find();
  205. if (!$order_info) {
  206. Db::rollback();
  207. filePut($prefix.'不存在的订单');
  208. $this->error('不存在的订单');
  209. }
  210. // 验证支付状态
  211. $result = $this->validate_apple_pay($receipt_data);
  212. if (!$result['status']) {// 验证不通过
  213. Db::rollback();
  214. filePut($prefix.'验证'.$result['message']);
  215. $this->error($result['message']);
  216. }
  217. $in_app = $result['data']['receipt']['in_app'];
  218. $only_trans = [];
  219. foreach($in_app as $key => $trans){
  220. //非订阅信息,原始信息,不验证product_id因为可能换了新的套餐
  221. if($trans['transaction_id'] == $transaction_id && $original_transaction_id == $trans['original_transaction_id']/* && $trans['product_id'] == $order_info['bundle_id']*/){
  222. $only_trans = $trans;
  223. break;
  224. }
  225. }
  226. if(empty($only_trans)){
  227. Db::rollback();
  228. filePut($prefix.'未找到匹配的交易');
  229. $this->error('未找到匹配的交易');
  230. }
  231. //续订,但是换了产品了,重新定义order_info
  232. $order_info_bundle_id = $order_info['bundle_id'];
  233. if($only_trans['product_id'] != $order_info['bundle_id']){
  234. $pay_order_map = [
  235. 'user_id' => $this->auth->id,
  236. 'bundle_id' => $only_trans['product_id'],
  237. 'order_status' => 0,
  238. 'table_name' => 'vip_recharge',
  239. ];
  240. $pay_order = Db::name('pay_order')->where($pay_order_map)->order('id desc')->lock(true)->find();
  241. if(!$pay_order){
  242. Db::rollback();
  243. filePut($prefix.'未找到匹配的交易,新订单找不到');
  244. $this->error('未找到匹配的新订单');
  245. }
  246. // 修改订单状态
  247. $update_order = [
  248. 'notifytime'=>time(),
  249. 'order_status'=>1,
  250. 'original_transaction_id' => $only_trans['original_transaction_id'],
  251. 'in_app_one' => json_encode($only_trans),
  252. ];
  253. $ros = Db::name('pay_order')->where(['id' => $pay_order['id']])->update($update_order);
  254. if($ros === false) {
  255. filePut($prefix.'修改订单状态失败');
  256. Db::rollback();
  257. $this->error('充值失败');
  258. }
  259. $args = json_decode($pay_order['args'],true);
  260. //修改order_info
  261. $order_info['order_id'] = $pay_order['id'];
  262. $order_info['user_id'] = $pay_order['user_id'];
  263. $order_info['bundle_id'] = $pay_order['bundle_id'];
  264. $order_info['days'] = $args['days'];
  265. $order_info['original_transaction_id'] = $only_trans['original_transaction_id'];//多余
  266. }
  267. //验证时间,不得小于最新的一条预定信息
  268. if($only_trans['purchase_date_ms'] <= $order_info['purchase_date_ms'] || $only_trans['expires_date_ms'] <= $order_info['expires_date_ms']){
  269. Db::rollback();
  270. filePut($prefix.'时间对不上,返回成功,finish掉');
  271. $this->success(1);
  272. }
  273. //逻辑开始
  274. //先充值
  275. $user_info = Db::name('user_wallet')->where('user_id',$order_info['user_id'])->lock(true)->find();
  276. if($user_info['vip_endtime'] < time()){
  277. //过期了
  278. $vip_endtime = time() + (intval($order_info['days']) * 86400);
  279. }else{
  280. //追加vip
  281. $vip_endtime = $user_info['vip_endtime'] + (intval($order_info['days']) * 86400);
  282. }
  283. $update_data = [
  284. 'vip_endtime'=>$vip_endtime,
  285. ];
  286. $result = Db::name('user_wallet')->where('user_id',$order_info['user_id'])->update($update_data);
  287. if($result === false)
  288. {
  289. filePut($prefix.'逻辑续费vip时间失败');
  290. Db::rollback();
  291. $this->error('充值失败');
  292. }
  293. //逻辑结束
  294. //添加新的一个task
  295. $task_data = $order_info;
  296. unset($task_data['id']);
  297. $task_data['createtime'] = time();
  298. $task_data['apple_receipt'] = $receipt_data;
  299. $task_data['in_app_one'] = json_encode($only_trans);
  300. $task_data['transaction_id'] = $transaction_id;
  301. $task_data['times'] = $order_info['times'] + 1;
  302. $task_data['original_purchase_date_ms'] = $only_trans['original_purchase_date_ms'];
  303. $task_data['purchase_date_ms'] = $only_trans['purchase_date_ms'];
  304. $task_data['expires_date_ms'] = $only_trans['expires_date_ms'];
  305. if($only_trans['product_id'] != $order_info_bundle_id){
  306. $task_data['times'] = 1;//回归1
  307. }
  308. $task_id = Db::name('user_vipxufei_task')->insertGetId($task_data);
  309. if(!$task_id)
  310. {
  311. filePut($prefix.'用户添加vipxufei_task失败');
  312. Db::rollback();
  313. $this->error('充值失败');
  314. }
  315. Db::commit();
  316. filePut($prefix.'充值成功');
  317. $this->success('充值成功');
  318. //逻辑结束
  319. }
  320. //金币+vip,苹果内购支付回调,app请求的接口
  321. public function gold_notify_iosnew(){
  322. $original_transaction_id = input('original_transaction_id','','trim');
  323. if(!empty($original_transaction_id)){
  324. //订阅信息
  325. $this->expires();
  326. }
  327. //苹果内购的验证收据
  328. $receipt_data = input('apple_receipt', '', 'trim');
  329. $out_trade_no = input('out_trade_no', '', 'trim');
  330. $transaction_id = input('transaction_id', '', 'trim');
  331. if (!$receipt_data || !$out_trade_no || !$transaction_id) {
  332. $this->error('缺少参数');
  333. }
  334. filePut("\r\n\r\n".'新请求');
  335. $prefix = 'ios充值,登录user_id:'.$this->auth->id.',out_trade_no:'.$out_trade_no.',传入transaction_id:'.$transaction_id.'。';
  336. filePut($prefix.'参数apple_receipt:'.$receipt_data);
  337. Db::startTrans();
  338. // 查找订单
  339. $order_map = [
  340. // 'user_id' => $this->auth->id,
  341. 'out_trade_no' => $out_trade_no,
  342. ];
  343. $order_info = Db::name('pay_order')->where($order_map)->lock(true)->find();
  344. if (!$order_info) {
  345. Db::rollback();
  346. filePut($prefix.'不存在的订单');
  347. $this->error('不存在的订单');
  348. }
  349. if ($order_info['order_status'] == 1) {
  350. Db::rollback();
  351. filePut($prefix.'充值早已完成');
  352. $this->success('充值已完成');
  353. }
  354. // 验证支付状态
  355. $result = $this->validate_apple_pay($receipt_data);
  356. if (!$result['status']) {// 验证不通过
  357. Db::rollback();
  358. filePut($prefix.'验证'.$result['message']);
  359. $this->error($result['message']);
  360. }
  361. $in_app = $result['data']['receipt']['in_app'];
  362. $only_trans = [];
  363. foreach($in_app as $key => $trans){
  364. //非订阅信息,原始信息
  365. // $trans['transaction_id'] == $transaction_id 这个不重要,重要的是后面2个。应该也重要,保证这次处理的是初始交易
  366. if($trans['transaction_id'] == $transaction_id && $transaction_id == $trans['original_transaction_id'] && $trans['product_id'] == $order_info['bundle_id']){
  367. $only_trans = $trans;
  368. break;
  369. }
  370. }
  371. if(empty($only_trans)){
  372. Db::rollback();
  373. filePut($prefix.'未找到匹配的交易,产品id'.$order_info['bundle_id'].',原始交易id'.$transaction_id);
  374. $this->error('未找到匹配的交易,产品id'.$order_info['bundle_id'].',原始交易id'.$transaction_id);
  375. }
  376. /*
  377. $count = count($result['data']['receipt']['in_app']);
  378. $use_count = $count - 1;
  379. $product_id = $result['data']['receipt']['in_app'][$use_count]['product_id'];
  380. $my_transaction_id = $result['data']['receipt']['in_app'][$use_count]['transaction_id'];
  381. if($product_id != $order_info['bundle_id']){
  382. Db::rollback();
  383. filePut($prefix.'验证'.'非法请求,请立刻停止product_id:'.$product_id.'!='.$order_info['bundle_id']);
  384. $this->error('非法请求,请立刻停止,产品id错误');
  385. }
  386. if($my_transaction_id != $transaction_id){
  387. Db::rollback();
  388. filePut($prefix.'验证'.'非法请求,请立刻停止transaction_id:'.$my_transaction_id.'!='.$transaction_id);
  389. $this->error('非法请求,请立刻停止,交易id错误');
  390. }*/
  391. //逻辑开始
  392. $args = json_decode($order_info['args'],true);
  393. //先充值
  394. if($order_info['table_name'] == 'gold_recharge'){
  395. $result = model('Wallet')->lockChangeAccountRemain($order_info['user_id'],'gold',$args['gold'],10, '金币充值','pay_order',$order_info['id']);
  396. if($result['status']===false)
  397. {
  398. filePut($prefix.'逻辑添加金币失败');
  399. Db::rollback();
  400. $this->error('充值失败');
  401. }
  402. }
  403. //先充值
  404. if($order_info['table_name'] == 'vip_recharge'){
  405. $user_info = Db::name('user_wallet')->where('user_id',$order_info['user_id'])->lock(true)->find();
  406. if($user_info['vip_endtime'] < time()){
  407. //过期了
  408. $vip_endtime = time() + (intval($args['days']) * 86400);
  409. }else{
  410. //追加vip
  411. $vip_endtime = $user_info['vip_endtime'] + (intval($args['days']) * 86400);
  412. }
  413. $update_data = [
  414. 'vip_endtime'=>$vip_endtime,
  415. ];
  416. $result = Db::name('user_wallet')->where('user_id',$order_info['user_id'])->update($update_data);
  417. if($result === false)
  418. {
  419. filePut($prefix.'逻辑续费vip时间失败');
  420. Db::rollback();
  421. $this->error('充值失败');
  422. }
  423. //订阅vip需要多加的逻辑
  424. if($order_info['is_lianxu'] == 1){
  425. $task_data = [
  426. 'order_id' => $order_info['id'],
  427. 'user_id' => $order_info['user_id'],
  428. 'bundle_id' => $order_info['bundle_id'],
  429. 'days' => $args['days'],
  430. 'createtime' => time(),
  431. 'apple_receipt' => $receipt_data,
  432. 'in_app_one' => json_encode($only_trans),
  433. 'original_transaction_id' => $only_trans['original_transaction_id'],
  434. 'transaction_id' => $only_trans['transaction_id'],
  435. 'status' => 1,
  436. 'times' => 1,
  437. //'notification_type' => 'INITIAL_BUY'.',首次订阅',
  438. 'original_purchase_date_ms' => $only_trans['original_purchase_date_ms'],
  439. 'purchase_date_ms' => $only_trans['purchase_date_ms'],
  440. 'expires_date_ms' => $only_trans['purchase_date_ms'], //第一次借用购买时间戳
  441. ];
  442. $task_id = Db::name('user_vipxufei_task')->insertGetId($task_data);
  443. if(!$task_id)
  444. {
  445. filePut($prefix.'用户添加vipxufei_task失败');
  446. Db::rollback();
  447. $this->error('充值失败');
  448. }
  449. }
  450. }
  451. // 修改订单状态
  452. $update_order = [
  453. 'notifytime'=>time(),
  454. 'order_status'=>1,
  455. 'original_transaction_id' => $only_trans['original_transaction_id'],//理论上与 transaction_id 相等
  456. 'in_app_one' => json_encode($only_trans),
  457. ];
  458. $ros = Db::name('pay_order')->where(['id' => $order_info['id']])->update($update_order);
  459. if($ros === false) {
  460. filePut($prefix.'修改订单状态失败');
  461. Db::rollback();
  462. $this->error('充值失败');
  463. }
  464. Db::commit();
  465. filePut($prefix.'充值成功');
  466. $this->success('充值成功');
  467. //逻辑结束
  468. }
  469. //因前端不传入订单号,作废
  470. public function gold_notify_ios(){
  471. //苹果内购的验证收据
  472. $receipt_data = input('apple_receipt', '', 'trim');
  473. $order_no = input('order_no', '', 'trim');
  474. filePut('金币 ios充值:参数apple_receipt='.$receipt_data.',order_no='.$order_no);
  475. if (!$receipt_data || !$order_no) {
  476. $this->error('缺少参数');
  477. }
  478. Db::startTrans();
  479. // 查找订单
  480. $order_info = Db::name('pay_order')->where(['out_trade_no' => $order_no])->lock(true)->find();
  481. if (!$order_info) {
  482. Db::rollback();
  483. filePut('金币 ios充值:订单丢失out_trade_no='.$order_no);
  484. $this->error('订单丢失');
  485. }
  486. if ($order_info['order_status'] == 1) {
  487. Db::rollback();
  488. $this->success('充值成功');
  489. }
  490. // 验证支付状态
  491. $result = $this->validate_apple_pay($receipt_data);
  492. if (!$result['status']) {// 验证不通过
  493. Db::rollback();
  494. filePut('金币 ios充值:验证out_trade_no='.$order_no.','.$result['message']);
  495. $this->error($result['message']);
  496. }
  497. $count = count($result['data']['receipt']['in_app']);
  498. $use_count = $count - 1;
  499. $args = json_decode($order_info['args'],true);
  500. if ($result['data']['receipt']['in_app'][$use_count]['product_id'] != $order_info['bundle_id']) {
  501. Db::rollback();
  502. filePut('金币 ios充值:非法请求,请立刻停止out_trade_no='.$order_no.','.$result['message'].','.$result['data']['receipt']['in_app'][$use_count]['product_id'].'!='.$order_info['bundle_id']);
  503. $this->error('非法请求,请立刻停止');
  504. }
  505. //逻辑开始
  506. //先充值
  507. $result = model('Wallet')->lockChangeAccountRemain($order_info['user_id'],'gold',$args['gold'],10, '金币充值','pay_order',$order_info['id']);
  508. if($result['status']===false)
  509. {
  510. filePut('金币 ios充值:验证out_trade_no='.$order_no.',逻辑添加金币失败');
  511. Db::rollback();
  512. return false;
  513. }
  514. // 修改订单状态
  515. $ros = Db::name('pay_order')->where(['out_trade_no' => $order_no])->update(['order_status'=>1,'notifytime'=>time()]);
  516. if($ros === false) {
  517. filePut('金币 ios充值:充值失败out_trade_no='.$order_no.','.$result['message'].','.'修改订单状态失败');
  518. Db::rollback();
  519. $this->error('充值失败');
  520. }
  521. Db::commit();
  522. $this->success('充值成功');
  523. //逻辑结束
  524. }
  525. ////////////////////////////////////
  526. //苹果自动扣费回调
  527. public function auto_renewal_vip_notify(){
  528. $this->notify_log_start('ios');
  529. }
  530. ///////////////////////////////////////////////////////////////////////////////////////////////
  531. //异步日志
  532. private function notify_log_start($paytype = 'ios'){
  533. //记录支付回调数据
  534. ignore_user_abort(); // run script in background
  535. set_time_limit(30);
  536. // 日志文件 start
  537. $log_base_dir = '../paylog/'.$paytype.'/';
  538. if (!is_dir($log_base_dir))
  539. {
  540. mkdir($log_base_dir, 0770, true);
  541. @chmod($log_base_dir, 0770);
  542. }
  543. $notify_file = $log_base_dir.'notify.txt';
  544. if(!file_exists($notify_file)) {
  545. @touch($notify_file);
  546. @chmod($notify_file, 0770);
  547. }
  548. if(filesize($notify_file)>5242880)//大于5M自动切换
  549. {
  550. rename($notify_file, $log_base_dir.'notify_'.date('Y_m_d_H_i_s').'.txt');
  551. }
  552. if(!file_exists($notify_file)) {
  553. @touch($notify_file);
  554. @chmod($notify_file, 0770);
  555. }
  556. // 日志文件 end
  557. //开始写入
  558. $_REQUEST = isset($_REQUEST) ? $_REQUEST : array();
  559. if($_REQUEST && $paytype == 'alipay') {
  560. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收request]".json_encode($_REQUEST), FILE_APPEND);
  561. } else {
  562. $xml = file_get_contents("php://input");
  563. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流原始数据] \n".$xml, FILE_APPEND);
  564. $xmlObj = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
  565. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流] ".json_encode($xmlObj), FILE_APPEND);
  566. }
  567. ini_set('display_errors','On');
  568. return $notify_file;
  569. }
  570. public function test(){
  571. $a = input('apple_receipt');
  572. $b = $this->validate_apple_pay($a);
  573. dump($b);
  574. }
  575. /**
  576. * 验证AppStore内付
  577. * @param string $receipt_data 付款后凭证
  578. * @return array 验证是否成功
  579. */
  580. function validate_apple_pay($receipt_data = '') {
  581. // 验证参数
  582. if (strlen($receipt_data) < 20) {
  583. $result = array(
  584. 'status' => false,
  585. 'message' => '非法参数'
  586. );
  587. return $result;
  588. }
  589. // 请求验证
  590. $html = $this->curl($receipt_data);
  591. $data = json_decode($html, true);
  592. $data['sandbox'] = '0';
  593. // p($data);die;
  594. if ($data['status'] == '21002') {
  595. $result = array(
  596. 'status' => false,
  597. 'message' => 'status:21002'
  598. );
  599. return $result;
  600. }
  601. // 如果是沙盒数据 则验证沙盒模式 21008;正式数据 21007
  602. if ($data['status'] == '21007') {
  603. // 请求验证
  604. $html = $this->curl($receipt_data, 1);
  605. $data = json_decode($html, true);
  606. $data['sandbox'] = '1';
  607. }
  608. if (isset($_GET['debug'])) {
  609. exit(json_encode($data));
  610. }
  611. if ($data['receipt']['bundle_id'] != 'com.huxiu.tken') {
  612. $result = array(
  613. 'status' => false,
  614. 'message' => '非法请求',
  615. 'data' => $data
  616. );
  617. return $result;
  618. }
  619. // 判断是否购买成功
  620. if (intval($data['status']) === 0) {
  621. $result = array(
  622. 'status' => true,
  623. 'message' => '购买成功',
  624. 'data' => $data
  625. );
  626. } else {
  627. $result = array(
  628. 'status' => false,
  629. 'message' => '购买失败 status:' . $data['status']
  630. );
  631. }
  632. // dump($result);
  633. return $result;
  634. }
  635. /**
  636. * 0 票据校验成功
  637. * 21000 App Store不能读取你提供的JSON对象25
  638. * 21002 receipt-data域的数据有问题
  639. * 21003 receipt无法通过验证
  640. * 21004 提供的shared secret不匹配你账号中的shared secret
  641. * 21005 receipt服务器当前不可用
  642. * 21006 receipt合法,但是订阅已过期。服务器接收到这个状态码时,receipt数据仍然会解码并一起发送
  643. * 21007 receipt是Sandbox receipt,但却发送至生产系统的验证服务
  644. * 21008 receipt是生产receipt,但却发送至Sandbox环境的验证服务
  645. */
  646. //苹果也是建议这个校验逻辑由服务端完成。服务器需要先去请求正式环境。如果receipt是正式环境的,那么这个时候苹果会返回(21007)告诉我们这个是沙盒的receipt,那么服务器再去请求sandbox环境。
  647. function curl($receipt_data,$sandbox = 0) {
  648. //小票信息
  649. $POSTFIELDS = [
  650. 'receipt-data' => $receipt_data,
  651. 'password' => '09ef214173a944808ac648b15fa02167'
  652. ];
  653. $POSTFIELDS = json_encode($POSTFIELDS, 320);
  654. //正式购买地址 沙盒购买地址
  655. $url_buy = "https://buy.itunes.apple.com/verifyReceipt";
  656. $url_sandbox = "https://sandbox.itunes.apple.com/verifyReceipt";
  657. //默认后台控制
  658. if (config('site.ios_pay_sandbox') > 0 ) {
  659. $url = $url_buy;
  660. } else {
  661. $url = $url_sandbox;
  662. }
  663. //强制沙盒
  664. if($sandbox == 1){
  665. $url = $url_sandbox;
  666. }
  667. $ch = curl_init($url);
  668. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  669. curl_setopt($ch, CURLOPT_POST, true);
  670. curl_setopt($ch, CURLOPT_POSTFIELDS, $POSTFIELDS);
  671. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //这两行一定要加,不加会报SSL 错误
  672. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  673. $response = curl_exec($ch);
  674. $errno = curl_errno($ch);
  675. curl_close($ch);
  676. if ($errno != 0) {
  677. return $errno;
  678. } else {
  679. return $response;
  680. }
  681. }
  682. }