Payios.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use app\common\library\Transaction;
  6. /**
  7. * 充值配置与充值订单
  8. */
  9. class Payios extends Api
  10. {
  11. protected $noNeedLogin = [];
  12. protected $noNeedRight = ['*'];
  13. ////////////////////////////////
  14. //金币充值
  15. public function jewel_config_ios(){
  16. $list = Db::name('payjewel_config_ios')->field('id,money,jewel,bundle_id')->where('is_show',1)->order('weigh asc,id asc')->select();
  17. $data['jewelconfig'] = $list;
  18. $wallet = model('wallet')->getWallet($this->auth->id);
  19. $data['wallet'] = $wallet;
  20. $data['money_to_jewel'] = config('rmb_to_jewel');
  21. $this->success('success',$data);
  22. }
  23. //充值金币 创建订单
  24. public function jewel_recharge_ios(){
  25. $rc_id = input('rc_id',0);
  26. $pay_type = 'ios';
  27. $platform = 'app';
  28. $cityname = input('cityname','','trim');
  29. $uid = $this->auth->id;
  30. if(!$rc_id){
  31. $this->error('请选择充值金额');
  32. }
  33. if($this->auth->is_auth != 2){
  34. $this->error('请先完成实名认证');
  35. }
  36. //赋值money
  37. $recharge_config = Db::name('payjewel_config_ios')->where('id',$rc_id)->find();
  38. $money = $recharge_config['money'] ?: 0;
  39. $jewel = $recharge_config['jewel'] ?: 0;
  40. //
  41. if($money<=0)
  42. {
  43. $this->error('支付金额必须大于0');
  44. }
  45. if($money > 10000){
  46. $this->error('支付金额太大');
  47. }
  48. //创建订单
  49. $data['user_id'] = $uid;
  50. $data['out_trade_no'] = createUniqueNo('P',$uid); // 数据库订单号加密
  51. $data['order_amount'] = $money;
  52. $data['createtime'] = time();
  53. $data['pay_type'] = $pay_type;
  54. $data['platform'] = $platform;
  55. $data['order_status'] = 0;
  56. $data['table_name'] = 'jewel_recharge';
  57. $data['table_id'] = 0;
  58. $data['args'] = json_encode(['jewel'=>$jewel,'cityname'=>$cityname]);
  59. $data['bundle_id'] = $recharge_config['bundle_id'];
  60. $orderid = Db::name('pay_order')->insertGetId($data);
  61. $this->success('success',$data['out_trade_no']);
  62. }
  63. ////////////////////////////////////////
  64. //金币+vip,苹果内购支付回调,app请求的接口
  65. public function jewel_notify_iosnew(){
  66. $original_transaction_id = input('original_transaction_id','','trim');
  67. if(!empty($original_transaction_id)){
  68. //订阅信息
  69. //$this->expires();
  70. }
  71. //苹果内购的验证收据
  72. $receipt_data = input('apple_receipt', '', 'trim');
  73. $out_trade_no = input('out_trade_no', '', 'trim');
  74. $transaction_id = input('transaction_id', '', 'trim');
  75. if (!$receipt_data || !$out_trade_no || !$transaction_id) {
  76. $this->error('缺少参数');
  77. }
  78. filePut("\r\n\r\n".'新请求');
  79. $prefix = 'ios充值,登录user_id:'.$this->auth->id.',out_trade_no:'.$out_trade_no.',传入transaction_id:'.$transaction_id.'。';
  80. filePut($prefix.'参数apple_receipt:'.$receipt_data);
  81. Db::startTrans();
  82. // 查找订单
  83. $order_map = [
  84. // 'user_id' => $this->auth->id,
  85. 'out_trade_no' => $out_trade_no,
  86. ];
  87. $order_info = Db::name('pay_order')->where($order_map)->lock(true)->find();
  88. if (!$order_info) {
  89. Db::rollback();
  90. filePut($prefix.'不存在的订单');
  91. $this->error('不存在的订单');
  92. }
  93. if ($order_info['order_status'] == 1) {
  94. Db::rollback();
  95. filePut($prefix.'充值早已完成');
  96. $this->success('充值已完成');
  97. }
  98. // 验证支付状态
  99. $result = $this->validate_apple_pay($receipt_data);
  100. if (!$result['status']) {// 验证不通过
  101. Db::rollback();
  102. filePut($prefix.'验证'.$result['message']);
  103. $this->error($result['message']);
  104. }
  105. $in_app = $result['data']['receipt']['in_app'];
  106. $only_trans = [];
  107. foreach($in_app as $key => $trans){
  108. //非订阅信息,原始信息
  109. // $trans['transaction_id'] == $transaction_id 这个不重要,重要的是后面2个。应该也重要,保证这次处理的是初始交易
  110. if($trans['transaction_id'] == $transaction_id && $transaction_id == $trans['original_transaction_id'] && $trans['product_id'] == $order_info['bundle_id']){
  111. $only_trans = $trans;
  112. break;
  113. }
  114. }
  115. if(empty($only_trans)){
  116. Db::rollback();
  117. filePut($prefix.'未找到匹配的交易,产品id'.$order_info['bundle_id'].',原始交易id'.$transaction_id);
  118. $this->error('未找到匹配的交易,产品id'.$order_info['bundle_id'].',原始交易id'.$transaction_id);
  119. }
  120. //逻辑开始
  121. $args = json_decode($order_info['args'],true);
  122. $extend = [
  123. 'cityname' => $args['cityname']
  124. ];
  125. //先充值
  126. if($order_info['table_name'] == 'jewel_recharge'){
  127. $result = model('Wallet')->lockChangeAccountRemain($order_info['user_id'],$args['jewel'],'+',0,'金币充值',33,'jewel',$extend);
  128. if($result['status']===false)
  129. {
  130. filePut($prefix.'逻辑添加金币失败');
  131. Db::rollback();
  132. $this->error('充值失败');
  133. }
  134. //充值通知
  135. \app\common\model\Message::addMessage($order_info['user_id'],"充值金币","已充值金币".$args['jewel']);
  136. //找到上级用户并加钱
  137. $userinfo = Db::name('user')->where('id',$order_info['user_id'])->field('id,nickname,pre_userid')->find();
  138. if($userinfo['pre_userid']){
  139. $bili = config('site.introsite_recharge_intro_bili') ?: 3;
  140. $jewel = bcdiv(bcmul($args['jewel'],$bili,0),100,0);
  141. if($jewel > 0){
  142. $result = model('Wallet')->lockChangeAccountRemain($userinfo['pre_userid'],$jewel,'+',0,$userinfo['nickname'].'充值金币返奖励',2,'jewel');
  143. if($result['status']===false)
  144. {
  145. Db::rollback();
  146. return false;
  147. }
  148. //充值通知
  149. \app\common\model\Message::addMessage($userinfo['pre_userid'],"邀请收益","已获得邀请收益".$jewel);
  150. }
  151. }
  152. }
  153. //先充值
  154. if($order_info['table_name'] == 'vip_recharge'){
  155. //没有,如果需要去tken找
  156. }
  157. // 修改订单状态
  158. $update_order = [
  159. 'notifytime'=>time(),
  160. 'order_status'=>1,
  161. 'original_transaction_id' => $only_trans['original_transaction_id'],//理论上与 transaction_id 相等
  162. 'in_app_one' => json_encode($only_trans),
  163. ];
  164. $ros = Db::name('pay_order')->where(['id' => $order_info['id']])->update($update_order);
  165. if($ros === false) {
  166. filePut($prefix.'修改订单状态失败');
  167. Db::rollback();
  168. $this->error('充值失败');
  169. }
  170. Db::commit();
  171. filePut($prefix.'充值成功');
  172. $this->success('充值成功');
  173. //逻辑结束
  174. }
  175. ////////////////////////////////私有方法///////////////////////////////////////////////////////////////
  176. public function test(){
  177. $a = input('apple_receipt');
  178. $b = $this->validate_apple_pay($a);
  179. echo json_encode($b);
  180. }
  181. /**
  182. * 验证AppStore内付
  183. * @param string $receipt_data 付款后凭证
  184. * @return array 验证是否成功
  185. * https://juejin.cn/post/7049626884765646884 报错代码
  186. */
  187. function validate_apple_pay($receipt_data = '') {
  188. // 验证参数
  189. if (strlen($receipt_data) < 20) {
  190. $result = array(
  191. 'status' => false,
  192. 'message' => '非法参数'
  193. );
  194. return $result;
  195. }
  196. // 请求验证
  197. $html = $this->curl($receipt_data);
  198. $data = json_decode($html, true);
  199. $data['sandbox'] = '0';
  200. // p($data);die;
  201. if ($data['status'] == '21002') {
  202. $result = array(
  203. 'status' => false,
  204. 'message' => 'status:21002'
  205. );
  206. return $result;
  207. }
  208. // 如果是沙盒数据 则验证沙盒模式 21008;正式数据 21007
  209. if ($data['status'] == '21007') {
  210. // 请求验证
  211. $html = $this->curl($receipt_data, 1);
  212. $data = json_decode($html, true);
  213. $data['sandbox'] = '1';
  214. }
  215. if (isset($_GET['debug'])) {
  216. exit(json_encode($data));
  217. }
  218. if ($data['receipt']['bundle_id'] != 'com.huxiu.tken') {
  219. $result = array(
  220. 'status' => false,
  221. 'message' => '非法请求',
  222. 'data' => $data
  223. );
  224. return $result;
  225. }
  226. // 判断是否购买成功
  227. if (intval($data['status']) === 0) {
  228. $result = array(
  229. 'status' => true,
  230. 'message' => '购买成功',
  231. 'data' => $data
  232. );
  233. } else {
  234. $result = array(
  235. 'status' => false,
  236. 'message' => '购买失败 status:' . $data['status']
  237. );
  238. }
  239. // dump($result);
  240. return $result;
  241. }
  242. /**
  243. * 0 票据校验成功
  244. * 21000 App Store不能读取你提供的JSON对象25
  245. * 21002 receipt-data域的数据有问题
  246. * 21003 receipt无法通过验证
  247. * 21004 提供的shared secret不匹配你账号中的shared secret
  248. * 21005 receipt服务器当前不可用
  249. * 21006 receipt合法,但是订阅已过期。服务器接收到这个状态码时,receipt数据仍然会解码并一起发送
  250. * 21007 receipt是Sandbox receipt,但却发送至生产系统的验证服务
  251. * 21008 receipt是生产receipt,但却发送至Sandbox环境的验证服务
  252. */
  253. //苹果也是建议这个校验逻辑由服务端完成。服务器需要先去请求正式环境。如果receipt是正式环境的,那么这个时候苹果会返回(21007)告诉我们这个是沙盒的receipt,那么服务器再去请求sandbox环境。
  254. function curl($receipt_data,$sandbox = 0) {
  255. //小票信息
  256. $POSTFIELDS = [
  257. 'receipt-data' => $receipt_data,
  258. 'password' => '09ef214173a944808ac648b15fa02167'
  259. ];
  260. $POSTFIELDS = json_encode($POSTFIELDS, 320);
  261. //正式购买地址 沙盒购买地址
  262. $url_buy = "https://buy.itunes.apple.com/verifyReceipt";
  263. $url_sandbox = "https://sandbox.itunes.apple.com/verifyReceipt";
  264. //默认后台控制
  265. if (config('site.ios_pay_sandbox') > 0 ) {
  266. $url = $url_buy;
  267. } else {
  268. $url = $url_sandbox;
  269. }
  270. //强制沙盒
  271. if($sandbox == 1){
  272. $url = $url_sandbox;
  273. }
  274. $ch = curl_init($url);
  275. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  276. curl_setopt($ch, CURLOPT_POST, true);
  277. curl_setopt($ch, CURLOPT_POSTFIELDS, $POSTFIELDS);
  278. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //这两行一定要加,不加会报SSL 错误
  279. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  280. $response = curl_exec($ch);
  281. $errno = curl_errno($ch);
  282. curl_close($ch);
  283. if ($errno != 0) {
  284. return $errno;
  285. } else {
  286. return $response;
  287. }
  288. }
  289. }