Payios.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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 = [];
  12. protected $noNeedRight = ['*'];
  13. //vip ios用的
  14. public function vip_config_ios(){
  15. $list = Db::name('payvip_config_ios')->field('id,money,days,title,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. $orderid = Db::name('pay_order')->insertGetId($data);
  57. $this->success('success',$data['out_trade_no']);
  58. }
  59. //vip,苹果内购支付回调
  60. public function vip_notify_ios(){
  61. //苹果内购的验证收据
  62. $receipt_data = input('apple_receipt', '', 'trim');
  63. $order_no = input('order_no', '', 'trim');
  64. filePut('VIP ios充值:参数apple_receipt='.$receipt_data.',order_no='.$order_no);
  65. if (!$receipt_data || !$order_no) {
  66. $this->error('缺少参数');
  67. }
  68. Db::startTrans();
  69. // 查找订单
  70. $order_info = Db::name('pay_order')->where(['out_trade_no' => $order_no])->lock(true)->find();
  71. if (!$order_info) {
  72. Db::rollback();
  73. filePut('VIP ios充值:订单丢失out_trade_no='.$order_no);
  74. $this->error('订单丢失');
  75. }
  76. if ($order_info['order_status'] == 1) {
  77. Db::rollback();
  78. $this->success('充值成功');
  79. }
  80. // 验证支付状态
  81. $result = $this->validate_apple_pay($receipt_data);
  82. if (!$result['status']) {// 验证不通过
  83. Db::rollback();
  84. filePut('VIP ios充值:验证out_trade_no='.$order_no.','.$result['message']);
  85. $this->error($result['message']);
  86. }
  87. $count = count($result['data']['receipt']['in_app']);
  88. $use_count = $count - 1;
  89. $args = json_decode($order_info['args'],true);
  90. if ($result['data']['receipt']['in_app'][$use_count]['product_id'] != $order_info['bundle_id']) {
  91. Db::rollback();
  92. filePut('VIP ios充值:非法请求,请立刻停止out_trade_no='.$order_no.','.$result['message'].','.$result['data']['receipt']['in_app'][$use_count]['product_id'].'!='.$args['bundle_id']);
  93. $this->error('非法请求,请立刻停止');
  94. }
  95. //逻辑开始
  96. //先充值
  97. $user_info = Db::name('user_wallet')->where('user_id',$order_info['user_id'])->lock(true)->find();
  98. if($user_info['vip_endtime'] < time()){
  99. //过期了
  100. $vip_endtime = time() + (intval($args['days']) * 86400);
  101. }else{
  102. //追加vip
  103. $vip_endtime = $user_info['vip_endtime'] + (intval($args['days']) * 86400);
  104. }
  105. $update_data = [
  106. 'vip_endtime'=>$vip_endtime,
  107. ];
  108. $result = Db::name('user_wallet')->where('user_id',$order_info['user_id'])->update($update_data);
  109. if($result === false)
  110. {
  111. Db::rollback();
  112. filePut('VIP ios充值:验证out_trade_no='.$order_no.',逻辑续费vip时间失败');
  113. $this->error('充值失败');
  114. }
  115. // 修改订单状态
  116. $ros = Db::name('pay_order')->where(['out_trade_no' => $order_no])->update(['order_status'=>1,'notifytime'=>time()]);
  117. if($ros === false) {
  118. filePut('VIP ios充值:充值失败out_trade_no='.$order_no.','.$result['message'].','.'修改订单状态失败');
  119. Db::rollback();
  120. $this->error('充值失败');
  121. }
  122. Db::commit();
  123. $this->success('充值成功');
  124. //逻辑结束
  125. }
  126. //金币充值
  127. public function gold_config_ios(){
  128. $list = Db::name('paygold_webcon_ios')->field('id,money,gold,bundle_id')->where('is_show',1)->order('weigh asc,id asc')->select();
  129. $data['goldconfig'] = $list;
  130. $wallet = model('wallet')->getWallet($this->auth->id);
  131. $data['wallet'] = $wallet;
  132. $data['money_to_gold'] = config('site.money_to_gold');
  133. $this->success('success',$data);
  134. }
  135. //充值金币 创建订单
  136. public function gold_recharge_ios(){
  137. $rc_id = input_post('rc_id',0);
  138. $pay_type = 'ios';
  139. $platform = 'app';
  140. $uid = $this->auth->id;
  141. if(!$rc_id){
  142. $this->error('请选择充值金额');
  143. }
  144. if(!$this->user_auth_limit()){
  145. $this->error('请先完成实名认证');
  146. }
  147. //赋值money
  148. $recharge_config = Db::name('paygold_webcon_ios')->where('id',$rc_id)->find();
  149. $money = $recharge_config['money'] ?: 0;
  150. $gold = $recharge_config['gold'] ?: 0;
  151. //
  152. if($money<=0)
  153. {
  154. $this->error('支付金额必须大于0');
  155. }
  156. if($money > 10000){
  157. $this->error('支付金额太大');
  158. }
  159. //创建订单
  160. $data['user_id'] = $uid;
  161. $data['out_trade_no'] = createUniqueNo('P',$uid); // 数据库订单号加密
  162. $data['order_amount'] = $money;
  163. $data['createtime'] = time();
  164. $data['pay_type'] = $pay_type;
  165. $data['platform'] = $platform;
  166. $data['order_status'] = 0;
  167. $data['table_name'] = 'gold_recharge';
  168. $data['table_id'] = 0;
  169. $data['args'] = json_encode(['gold'=>$gold]);
  170. $data['bundle_id'] = $recharge_config['bundle_id'];
  171. $orderid = Db::name('pay_order')->insertGetId($data);
  172. $this->success('success',$data['out_trade_no']);
  173. }
  174. //vip,苹果内购支付回调,app请求的接口
  175. public function gold_notify_iosnew(){
  176. //苹果内购的验证收据
  177. $receipt_data = input('apple_receipt', '', 'trim');
  178. $order_no = input('order_no', '', 'trim');
  179. filePut('金币 ios充值:参数apple_receipt='.$receipt_data.',order_no='.$order_no);
  180. if (!$receipt_data || !$order_no) {
  181. $this->error('缺少参数');
  182. }
  183. Db::startTrans();
  184. // 查找订单
  185. $order_info = Db::name('pay_order')->where(['out_trade_no' => $order_no])->lock(true)->find();
  186. if (!$order_info) {
  187. Db::rollback();
  188. filePut('金币 ios充值:订单丢失out_trade_no='.$order_no);
  189. $this->error('订单丢失');
  190. }
  191. if ($order_info['order_status'] == 1) {
  192. Db::rollback();
  193. $this->success('充值成功');
  194. }
  195. // 验证支付状态
  196. $result = $this->validate_apple_pay($receipt_data);
  197. if (!$result['status']) {// 验证不通过
  198. Db::rollback();
  199. filePut('金币 ios充值:验证out_trade_no='.$order_no.','.$result['message']);
  200. $this->error($result['message']);
  201. }
  202. $count = count($result['data']['receipt']['in_app']);
  203. $use_count = $count - 1;
  204. $args = json_decode($order_info['args'],true);
  205. if ($result['data']['receipt']['in_app'][$use_count]['product_id'] != $order_info['bundle_id']) {
  206. Db::rollback();
  207. filePut('金币 ios充值:非法请求,请立刻停止out_trade_no='.$order_no.','.$result['message'].','.$result['data']['receipt']['in_app'][$use_count]['product_id'].'!='.$args['bundle_id']);
  208. $this->error('非法请求,请立刻停止');
  209. }
  210. //逻辑开始
  211. //先充值
  212. $result = model('Wallet')->lockChangeAccountRemain($order_info['user_id'],'gold',$args['gold'],10, '金币充值','pay_order',$order_info['id']);
  213. if($result['status']===false)
  214. {
  215. filePut('金币 ios充值:验证out_trade_no='.$order_no.',逻辑添加金币失败');
  216. Db::rollback();
  217. return false;
  218. }
  219. // 修改订单状态
  220. $ros = Db::name('pay_order')->where(['out_trade_no' => $order_no])->update(['order_status'=>1,'notifytime'=>time()]);
  221. if($ros === false) {
  222. filePut('金币 ios充值:充值失败out_trade_no='.$order_no.','.$result['message'].','.'修改订单状态失败');
  223. Db::rollback();
  224. $this->error('充值失败');
  225. }
  226. Db::commit();
  227. $this->success('充值成功');
  228. //逻辑结束
  229. }
  230. public function gold_notify_ios(){
  231. //苹果内购的验证收据
  232. $receipt_data = input('apple_receipt', '', 'trim');
  233. $order_no = input('order_no', '', 'trim');
  234. filePut('金币 ios充值:参数apple_receipt='.$receipt_data.',order_no='.$order_no);
  235. if (!$receipt_data || !$order_no) {
  236. $this->error('缺少参数');
  237. }
  238. Db::startTrans();
  239. // 查找订单
  240. $order_info = Db::name('pay_order')->where(['out_trade_no' => $order_no])->lock(true)->find();
  241. if (!$order_info) {
  242. Db::rollback();
  243. filePut('金币 ios充值:订单丢失out_trade_no='.$order_no);
  244. $this->error('订单丢失');
  245. }
  246. if ($order_info['order_status'] == 1) {
  247. Db::rollback();
  248. $this->success('充值成功');
  249. }
  250. // 验证支付状态
  251. $result = $this->validate_apple_pay($receipt_data);
  252. if (!$result['status']) {// 验证不通过
  253. Db::rollback();
  254. filePut('金币 ios充值:验证out_trade_no='.$order_no.','.$result['message']);
  255. $this->error($result['message']);
  256. }
  257. $count = count($result['data']['receipt']['in_app']);
  258. $use_count = $count - 1;
  259. $args = json_decode($order_info['args'],true);
  260. if ($result['data']['receipt']['in_app'][$use_count]['product_id'] != $order_info['bundle_id']) {
  261. Db::rollback();
  262. filePut('金币 ios充值:非法请求,请立刻停止out_trade_no='.$order_no.','.$result['message'].','.$result['data']['receipt']['in_app'][$use_count]['product_id'].'!='.$args['bundle_id']);
  263. $this->error('非法请求,请立刻停止');
  264. }
  265. //逻辑开始
  266. //先充值
  267. $result = model('Wallet')->lockChangeAccountRemain($order_info['user_id'],'gold',$args['gold'],10, '金币充值','pay_order',$order_info['id']);
  268. if($result['status']===false)
  269. {
  270. filePut('金币 ios充值:验证out_trade_no='.$order_no.',逻辑添加金币失败');
  271. Db::rollback();
  272. return false;
  273. }
  274. // 修改订单状态
  275. $ros = Db::name('pay_order')->where(['out_trade_no' => $order_no])->update(['order_status'=>1,'notifytime'=>time()]);
  276. if($ros === false) {
  277. filePut('金币 ios充值:充值失败out_trade_no='.$order_no.','.$result['message'].','.'修改订单状态失败');
  278. Db::rollback();
  279. $this->error('充值失败');
  280. }
  281. Db::commit();
  282. $this->success('充值成功');
  283. //逻辑结束
  284. }
  285. ///////////////////////////////////////////////////////////////////////////////////////////////
  286. //异步日志
  287. private function notify_log_start($paytype = 'ios'){
  288. //记录支付回调数据
  289. ignore_user_abort(); // run script in background
  290. set_time_limit(30);
  291. // 日志文件 start
  292. $log_base_dir = '../paylog/'.$paytype.'/';
  293. if (!is_dir($log_base_dir))
  294. {
  295. mkdir($log_base_dir, 0770, true);
  296. @chmod($log_base_dir, 0770);
  297. }
  298. $notify_file = $log_base_dir.'notify.txt';
  299. if(!file_exists($notify_file)) {
  300. @touch($notify_file);
  301. @chmod($notify_file, 0770);
  302. }
  303. if(filesize($notify_file)>5242880)//大于5M自动切换
  304. {
  305. rename($notify_file, $log_base_dir.'notify_'.date('Y_m_d_H_i_s').'.txt');
  306. }
  307. if(!file_exists($notify_file)) {
  308. @touch($notify_file);
  309. @chmod($notify_file, 0770);
  310. }
  311. // 日志文件 end
  312. //开始写入
  313. $_REQUEST = isset($_REQUEST) ? $_REQUEST : array();
  314. if($_REQUEST && $paytype == 'alipay') {
  315. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收request]".json_encode($_REQUEST), FILE_APPEND);
  316. } else {
  317. $xml = file_get_contents("php://input");
  318. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流原始数据] \n".$xml, FILE_APPEND);
  319. $xmlObj = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
  320. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流] ".json_encode($xmlObj), FILE_APPEND);
  321. }
  322. ini_set('display_errors','On');
  323. return $notify_file;
  324. }
  325. /**
  326. * 验证AppStore内付
  327. * @param string $receipt_data 付款后凭证
  328. * @return array 验证是否成功
  329. */
  330. function validate_apple_pay($receipt_data = '') {
  331. // 验证参数
  332. if (strlen($receipt_data) < 20) {
  333. $result = array(
  334. 'status' => false,
  335. 'message' => '非法参数'
  336. );
  337. return $result;
  338. }
  339. // 请求验证
  340. $html = $this->curl($receipt_data);
  341. $data = json_decode($html, true);
  342. // p($data);die;
  343. if ($data['status'] == '21002') {
  344. $this->error('21002');
  345. }
  346. // 如果是沙盒数据 则验证沙盒模式 21008;正式数据 21007
  347. // if ($data['status'] == '21007') {
  348. // // 请求验证
  349. // $html = $this->curl($receipt_data, 1);
  350. // $data = json_decode($html, true);
  351. // $data['sandbox'] = '1';
  352. // }
  353. if (isset($_GET['debug'])) {
  354. exit(json_encode($data));
  355. }
  356. // if ($data['receipt']['bundle_id'] != 'com.liuniukeji.mayivideo') {
  357. if ($data['receipt']['bundle_id'] != 'com.huxiu.tken') {
  358. $result = array(
  359. 'status' => false,
  360. 'message' => '非法请求',
  361. 'data' => $data
  362. );
  363. return $result;
  364. }
  365. // 判断是否购买成功
  366. if (intval($data['status']) === 0) {
  367. $result = array(
  368. 'status' => true,
  369. 'message' => '购买成功',
  370. 'data' => $data
  371. );
  372. } else {
  373. $result = array(
  374. 'status' => false,
  375. 'message' => '购买失败 status:' . $data['status']
  376. );
  377. }
  378. return $result;
  379. }
  380. /**
  381. * 21000 App Store不能读取你提供的JSON对象25
  382. * 21002 receipt-data域的数据有问题
  383. * 21003 receipt无法通过验证
  384. * 21004 提供的shared secret不匹配你账号中的shared secret
  385. * 21005 receipt服务器当前不可用
  386. * 21006 receipt合法,但是订阅已过期。服务器接收到这个状态码时,receipt数据仍然会解码并一起发送
  387. * 21007 receipt是Sandbox receipt,但却发送至生产系统的验证服务
  388. * 21008 receipt是生产receipt,但却发送至Sandbox环境的验证服务
  389. */
  390. function curl($receipt_data, $sandbox = 0) {
  391. //小票信息
  392. $POSTFIELDS = array("receipt-data" => $receipt_data);
  393. $POSTFIELDS = json_encode($POSTFIELDS, 320);
  394. // $POSTFIELDS = '{' . '"receipt-data":' . '"' . $receipt_data . '"' .'}';
  395. // p($POSTFIELDS);die;
  396. //正式购买地址 沙盒购买地址
  397. $url_buy = "https://buy.itunes.apple.com/verifyReceipt";
  398. $url_sandbox = "https://sandbox.itunes.apple.com/verifyReceipt";
  399. // if ($sandbox > 0) {
  400. if (config('site.ios_pay_sandbox') > 0) {
  401. $url = $url_buy;
  402. } else {
  403. $url = $url_sandbox;
  404. }
  405. // 上架通过直接使用正式地址
  406. // $url = $url_sandbox;
  407. $ch = curl_init($url);
  408. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  409. curl_setopt($ch, CURLOPT_POST, true);
  410. curl_setopt($ch, CURLOPT_POSTFIELDS, $POSTFIELDS);
  411. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //这两行一定要加,不加会报SSL 错误
  412. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  413. $response = curl_exec($ch);
  414. $errno = curl_errno($ch);
  415. curl_close($ch);
  416. if ($errno != 0) {
  417. return $errno;
  418. } else {
  419. return $response;
  420. }
  421. }
  422. }