Payios.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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')->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(){
  119. $list = Db::name('paygold_webcon')->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(){
  128. $rc_id = input_post('rc_id',0);
  129. $pay_type = input_post('pay_type','wechat');
  130. $platform = 'app';
  131. $freemoney = input_post('freemoney',0);
  132. $uid = $this->auth->id;
  133. if(!$rc_id && !$freemoney){
  134. $this->error('请选择或填写充值金额');
  135. }
  136. //赋值money
  137. if($rc_id){
  138. $recharge_config = Db::name('paygold_webcon')->where('id',$rc_id)->find();
  139. $money = $recharge_config['money'] ?: 0;
  140. $gold = $recharge_config['gold'] ?: 0;
  141. }
  142. //自由输入覆盖
  143. if(!empty($freemoney)){
  144. $rc_id = 0;
  145. $money = floatval($freemoney);
  146. $bili = config('site.money_to_gold') ?: 10;
  147. $gold = bcmul($money,$bili,0);
  148. }
  149. //
  150. if($money<=0)
  151. {
  152. $this->error('支付金额必须大于0');
  153. }
  154. if($money > 10000){
  155. $this->error('支付金额太大');
  156. }
  157. //创建订单
  158. $data['user_id'] = $uid;
  159. $data['out_trade_no'] = createUniqueNo('P',$uid); // 数据库订单号加密
  160. $data['order_amount'] = $money;
  161. $data['createtime'] = time();
  162. $data['pay_type'] = $pay_type;
  163. $data['platform'] = $platform;
  164. $data['order_status'] = 0;
  165. $data['table_name'] = 'gold_recharge';
  166. $data['table_id'] = 0;
  167. $data['args'] = json_encode(['gold'=>$gold]);
  168. $orderid = Db::name('pay_order')->insertGetId($data);
  169. // $openid = $this->auth->mini_openid;
  170. //下单
  171. $params = [
  172. 'type' => $pay_type,
  173. 'orderid' => $data['out_trade_no'],
  174. 'title' => '支付订单',
  175. 'amount' => $data['order_amount'],
  176. 'method' => $platform,
  177. // 'openid' => $openid,
  178. 'notifyurl' => config('pay_notify_url').'/api/notify/recharge_notify_base/paytype/'.$pay_type,
  179. 'returnurl' => '',
  180. ];
  181. $res = Service::submitOrder($params);
  182. if($pay_type == 'wechat'){
  183. $this->success('success',json_decode($res,true));
  184. }else{
  185. $this->success('success',$res);
  186. }
  187. }
  188. //异步日志
  189. private function notify_log_start($paytype = 'ios'){
  190. //记录支付回调数据
  191. ignore_user_abort(); // run script in background
  192. set_time_limit(30);
  193. // 日志文件 start
  194. $log_base_dir = '../paylog/'.$paytype.'/';
  195. if (!is_dir($log_base_dir))
  196. {
  197. mkdir($log_base_dir, 0770, true);
  198. @chmod($log_base_dir, 0770);
  199. }
  200. $notify_file = $log_base_dir.'notify.txt';
  201. if(!file_exists($notify_file)) {
  202. @touch($notify_file);
  203. @chmod($notify_file, 0770);
  204. }
  205. if(filesize($notify_file)>5242880)//大于5M自动切换
  206. {
  207. rename($notify_file, $log_base_dir.'notify_'.date('Y_m_d_H_i_s').'.txt');
  208. }
  209. if(!file_exists($notify_file)) {
  210. @touch($notify_file);
  211. @chmod($notify_file, 0770);
  212. }
  213. // 日志文件 end
  214. //开始写入
  215. $_REQUEST = isset($_REQUEST) ? $_REQUEST : array();
  216. if($_REQUEST && $paytype == 'alipay') {
  217. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收request]".json_encode($_REQUEST), FILE_APPEND);
  218. } else {
  219. $xml = file_get_contents("php://input");
  220. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流原始数据] \n".$xml, FILE_APPEND);
  221. $xmlObj = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
  222. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流] ".json_encode($xmlObj), FILE_APPEND);
  223. }
  224. ini_set('display_errors','On');
  225. return $notify_file;
  226. }
  227. /**
  228. * 验证AppStore内付
  229. * @param string $receipt_data 付款后凭证
  230. * @return array 验证是否成功
  231. */
  232. function validate_apple_pay($receipt_data = '') {
  233. // 验证参数
  234. if (strlen($receipt_data) < 20) {
  235. $result = array(
  236. 'status' => false,
  237. 'message' => '非法参数'
  238. );
  239. return $result;
  240. }
  241. // 请求验证
  242. $html = $this->curl($receipt_data);
  243. $data = json_decode($html, true);
  244. // p($data);die;
  245. if ($data['status'] == '21002') {
  246. $this->error('21002');
  247. }
  248. // 如果是沙盒数据 则验证沙盒模式 21008;正式数据 21007
  249. // if ($data['status'] == '21007') {
  250. // // 请求验证
  251. // $html = $this->curl($receipt_data, 1);
  252. // $data = json_decode($html, true);
  253. // $data['sandbox'] = '1';
  254. // }
  255. if (isset($_GET['debug'])) {
  256. exit(json_encode($data));
  257. }
  258. // if ($data['receipt']['bundle_id'] != 'com.liuniukeji.mayivideo') {
  259. if ($data['receipt']['bundle_id'] != 'com.huxiu.tken') {
  260. $result = array(
  261. 'status' => false,
  262. 'message' => '非法请求',
  263. 'data' => $data
  264. );
  265. return $result;
  266. }
  267. // 判断是否购买成功
  268. if (intval($data['status']) === 0) {
  269. $result = array(
  270. 'status' => true,
  271. 'message' => '购买成功',
  272. 'data' => $data
  273. );
  274. } else {
  275. $result = array(
  276. 'status' => false,
  277. 'message' => '购买失败 status:' . $data['status']
  278. );
  279. }
  280. return $result;
  281. }
  282. /**
  283. * 21000 App Store不能读取你提供的JSON对象25
  284. * 21002 receipt-data域的数据有问题
  285. * 21003 receipt无法通过验证
  286. * 21004 提供的shared secret不匹配你账号中的shared secret
  287. * 21005 receipt服务器当前不可用
  288. * 21006 receipt合法,但是订阅已过期。服务器接收到这个状态码时,receipt数据仍然会解码并一起发送
  289. * 21007 receipt是Sandbox receipt,但却发送至生产系统的验证服务
  290. * 21008 receipt是生产receipt,但却发送至Sandbox环境的验证服务
  291. */
  292. function curl($receipt_data, $sandbox = 0) {
  293. //小票信息
  294. $POSTFIELDS = array("receipt-data" => $receipt_data);
  295. $POSTFIELDS = json_encode($POSTFIELDS, 320);
  296. // $POSTFIELDS = '{' . '"receipt-data":' . '"' . $receipt_data . '"' .'}';
  297. // p($POSTFIELDS);die;
  298. //正式购买地址 沙盒购买地址
  299. $url_buy = "https://buy.itunes.apple.com/verifyReceipt";
  300. $url_sandbox = "https://sandbox.itunes.apple.com/verifyReceipt";
  301. // if ($sandbox > 0) {
  302. if (config('site.ios_pay_sandbox') > 0) {
  303. $url = $url_buy;
  304. } else {
  305. $url = $url_sandbox;
  306. }
  307. // 上架通过直接使用正式地址
  308. // $url = $url_sandbox;
  309. $ch = curl_init($url);
  310. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  311. curl_setopt($ch, CURLOPT_POST, true);
  312. curl_setopt($ch, CURLOPT_POSTFIELDS, $POSTFIELDS);
  313. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //这两行一定要加,不加会报SSL 错误
  314. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  315. $response = curl_exec($ch);
  316. $errno = curl_errno($ch);
  317. curl_close($ch);
  318. if ($errno != 0) {
  319. return $errno;
  320. } else {
  321. return $response;
  322. }
  323. }
  324. }