Payios.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. 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')->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,苹果内购支付回调
  168. public function gold_notify_ios(){
  169. $notify_file = $this->notify_log_start('ios');
  170. //苹果内购的验证收据
  171. $receipt_data = input('apple_receipt', '', 'trim');
  172. $order_no = input('order_no', '', 'trim');
  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. $this->error('订单丢失');
  182. }
  183. if ($order_info['order_status'] == 1) {
  184. Db::rollback();
  185. $this->success('充值成功');
  186. }
  187. // 验证支付状态
  188. $result = $this->validate_apple_pay($receipt_data);
  189. if (!$result['status']) {// 验证不通过
  190. Db::rollback();
  191. $this->error($result['message']);
  192. }
  193. $count = count($result['data']['receipt']['in_app']);
  194. $use_count = $count - 1;
  195. $args = json_decode($order_info['args'],true);
  196. if ($result['data']['receipt']['in_app'][$use_count]['product_id'] != $args['bundle_id']) {
  197. Db::rollback();
  198. $this->error('非法请求,请立刻停止');
  199. }
  200. //逻辑开始
  201. //先充值
  202. $result = model('Wallet')->lockChangeAccountRemain($order_info['user_id'],'gold',$args['gold'],10, '金币充值','pay_order',$order_info['id']);
  203. if($result['status']===false)
  204. {
  205. Db::rollback();
  206. return false;
  207. }
  208. // 修改订单状态
  209. $ros = Db::name('pay_order')->where(['out_trade_no' => $order_no])->update(['order_status'=>1,'notifytime'=>time()]);
  210. if($ros === false) {
  211. Db::rollback();
  212. $this->error('充值失败');
  213. }
  214. Db::commit();
  215. $this->success('充值成功');
  216. //逻辑结束
  217. }
  218. ///////////////////////////////////////////////////////////////////////////////////////////////
  219. //异步日志
  220. private function notify_log_start($paytype = 'ios'){
  221. //记录支付回调数据
  222. ignore_user_abort(); // run script in background
  223. set_time_limit(30);
  224. // 日志文件 start
  225. $log_base_dir = '../paylog/'.$paytype.'/';
  226. if (!is_dir($log_base_dir))
  227. {
  228. mkdir($log_base_dir, 0770, true);
  229. @chmod($log_base_dir, 0770);
  230. }
  231. $notify_file = $log_base_dir.'notify.txt';
  232. if(!file_exists($notify_file)) {
  233. @touch($notify_file);
  234. @chmod($notify_file, 0770);
  235. }
  236. if(filesize($notify_file)>5242880)//大于5M自动切换
  237. {
  238. rename($notify_file, $log_base_dir.'notify_'.date('Y_m_d_H_i_s').'.txt');
  239. }
  240. if(!file_exists($notify_file)) {
  241. @touch($notify_file);
  242. @chmod($notify_file, 0770);
  243. }
  244. // 日志文件 end
  245. //开始写入
  246. $_REQUEST = isset($_REQUEST) ? $_REQUEST : array();
  247. if($_REQUEST && $paytype == 'alipay') {
  248. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收request]".json_encode($_REQUEST), FILE_APPEND);
  249. } else {
  250. $xml = file_get_contents("php://input");
  251. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流原始数据] \n".$xml, FILE_APPEND);
  252. $xmlObj = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
  253. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流] ".json_encode($xmlObj), FILE_APPEND);
  254. }
  255. ini_set('display_errors','On');
  256. return $notify_file;
  257. }
  258. /**
  259. * 验证AppStore内付
  260. * @param string $receipt_data 付款后凭证
  261. * @return array 验证是否成功
  262. */
  263. function validate_apple_pay($receipt_data = '') {
  264. // 验证参数
  265. if (strlen($receipt_data) < 20) {
  266. $result = array(
  267. 'status' => false,
  268. 'message' => '非法参数'
  269. );
  270. return $result;
  271. }
  272. // 请求验证
  273. $html = $this->curl($receipt_data);
  274. $data = json_decode($html, true);
  275. // p($data);die;
  276. if ($data['status'] == '21002') {
  277. $this->error('21002');
  278. }
  279. // 如果是沙盒数据 则验证沙盒模式 21008;正式数据 21007
  280. // if ($data['status'] == '21007') {
  281. // // 请求验证
  282. // $html = $this->curl($receipt_data, 1);
  283. // $data = json_decode($html, true);
  284. // $data['sandbox'] = '1';
  285. // }
  286. if (isset($_GET['debug'])) {
  287. exit(json_encode($data));
  288. }
  289. // if ($data['receipt']['bundle_id'] != 'com.liuniukeji.mayivideo') {
  290. if ($data['receipt']['bundle_id'] != 'com.huxiu.tken') {
  291. $result = array(
  292. 'status' => false,
  293. 'message' => '非法请求',
  294. 'data' => $data
  295. );
  296. return $result;
  297. }
  298. // 判断是否购买成功
  299. if (intval($data['status']) === 0) {
  300. $result = array(
  301. 'status' => true,
  302. 'message' => '购买成功',
  303. 'data' => $data
  304. );
  305. } else {
  306. $result = array(
  307. 'status' => false,
  308. 'message' => '购买失败 status:' . $data['status']
  309. );
  310. }
  311. return $result;
  312. }
  313. /**
  314. * 21000 App Store不能读取你提供的JSON对象25
  315. * 21002 receipt-data域的数据有问题
  316. * 21003 receipt无法通过验证
  317. * 21004 提供的shared secret不匹配你账号中的shared secret
  318. * 21005 receipt服务器当前不可用
  319. * 21006 receipt合法,但是订阅已过期。服务器接收到这个状态码时,receipt数据仍然会解码并一起发送
  320. * 21007 receipt是Sandbox receipt,但却发送至生产系统的验证服务
  321. * 21008 receipt是生产receipt,但却发送至Sandbox环境的验证服务
  322. */
  323. function curl($receipt_data, $sandbox = 0) {
  324. //小票信息
  325. $POSTFIELDS = array("receipt-data" => $receipt_data);
  326. $POSTFIELDS = json_encode($POSTFIELDS, 320);
  327. // $POSTFIELDS = '{' . '"receipt-data":' . '"' . $receipt_data . '"' .'}';
  328. // p($POSTFIELDS);die;
  329. //正式购买地址 沙盒购买地址
  330. $url_buy = "https://buy.itunes.apple.com/verifyReceipt";
  331. $url_sandbox = "https://sandbox.itunes.apple.com/verifyReceipt";
  332. // if ($sandbox > 0) {
  333. if (config('site.ios_pay_sandbox') > 0) {
  334. $url = $url_buy;
  335. } else {
  336. $url = $url_sandbox;
  337. }
  338. // 上架通过直接使用正式地址
  339. // $url = $url_sandbox;
  340. $ch = curl_init($url);
  341. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  342. curl_setopt($ch, CURLOPT_POST, true);
  343. curl_setopt($ch, CURLOPT_POSTFIELDS, $POSTFIELDS);
  344. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //这两行一定要加,不加会报SSL 错误
  345. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  346. $response = curl_exec($ch);
  347. $errno = curl_errno($ch);
  348. curl_close($ch);
  349. if ($errno != 0) {
  350. return $errno;
  351. } else {
  352. return $response;
  353. }
  354. }
  355. }