Payios.php 14 KB

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