Payios.php 13 KB

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