Payios.php 11 KB

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