Payios.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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->auth->is_auth != 2){
  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'],$args['jewel'],'+',0,'金币充值',33,'jewel');
  124. if($result['status']===false)
  125. {
  126. filePut($prefix.'逻辑添加金币失败');
  127. Db::rollback();
  128. $this->error('充值失败');
  129. }
  130. }
  131. //先充值
  132. if($order_info['table_name'] == 'vip_recharge'){
  133. //没有,如果需要去tken找
  134. }
  135. // 修改订单状态
  136. $update_order = [
  137. 'notifytime'=>time(),
  138. 'order_status'=>1,
  139. 'original_transaction_id' => $only_trans['original_transaction_id'],//理论上与 transaction_id 相等
  140. 'in_app_one' => json_encode($only_trans),
  141. ];
  142. $ros = Db::name('pay_order')->where(['id' => $order_info['id']])->update($update_order);
  143. if($ros === false) {
  144. filePut($prefix.'修改订单状态失败');
  145. Db::rollback();
  146. $this->error('充值失败');
  147. }
  148. Db::commit();
  149. filePut($prefix.'充值成功');
  150. $this->success('充值成功');
  151. //逻辑结束
  152. }
  153. ////////////////////////////////私有方法///////////////////////////////////////////////////////////////
  154. public function test(){
  155. $a = input('apple_receipt');
  156. $b = $this->validate_apple_pay($a);
  157. echo json_encode($b);
  158. }
  159. /**
  160. * 验证AppStore内付
  161. * @param string $receipt_data 付款后凭证
  162. * @return array 验证是否成功
  163. * https://juejin.cn/post/7049626884765646884 报错代码
  164. */
  165. function validate_apple_pay($receipt_data = '') {
  166. // 验证参数
  167. if (strlen($receipt_data) < 20) {
  168. $result = array(
  169. 'status' => false,
  170. 'message' => '非法参数'
  171. );
  172. return $result;
  173. }
  174. // 请求验证
  175. $html = $this->curl($receipt_data);
  176. $data = json_decode($html, true);
  177. $data['sandbox'] = '0';
  178. // p($data);die;
  179. if ($data['status'] == '21002') {
  180. $result = array(
  181. 'status' => false,
  182. 'message' => 'status:21002'
  183. );
  184. return $result;
  185. }
  186. // 如果是沙盒数据 则验证沙盒模式 21008;正式数据 21007
  187. if ($data['status'] == '21007') {
  188. // 请求验证
  189. $html = $this->curl($receipt_data, 1);
  190. $data = json_decode($html, true);
  191. $data['sandbox'] = '1';
  192. }
  193. if (isset($_GET['debug'])) {
  194. exit(json_encode($data));
  195. }
  196. if ($data['receipt']['bundle_id'] != 'com.huxiu.tken') {
  197. $result = array(
  198. 'status' => false,
  199. 'message' => '非法请求',
  200. 'data' => $data
  201. );
  202. return $result;
  203. }
  204. // 判断是否购买成功
  205. if (intval($data['status']) === 0) {
  206. $result = array(
  207. 'status' => true,
  208. 'message' => '购买成功',
  209. 'data' => $data
  210. );
  211. } else {
  212. $result = array(
  213. 'status' => false,
  214. 'message' => '购买失败 status:' . $data['status']
  215. );
  216. }
  217. // dump($result);
  218. return $result;
  219. }
  220. /**
  221. * 0 票据校验成功
  222. * 21000 App Store不能读取你提供的JSON对象25
  223. * 21002 receipt-data域的数据有问题
  224. * 21003 receipt无法通过验证
  225. * 21004 提供的shared secret不匹配你账号中的shared secret
  226. * 21005 receipt服务器当前不可用
  227. * 21006 receipt合法,但是订阅已过期。服务器接收到这个状态码时,receipt数据仍然会解码并一起发送
  228. * 21007 receipt是Sandbox receipt,但却发送至生产系统的验证服务
  229. * 21008 receipt是生产receipt,但却发送至Sandbox环境的验证服务
  230. */
  231. //苹果也是建议这个校验逻辑由服务端完成。服务器需要先去请求正式环境。如果receipt是正式环境的,那么这个时候苹果会返回(21007)告诉我们这个是沙盒的receipt,那么服务器再去请求sandbox环境。
  232. function curl($receipt_data,$sandbox = 0) {
  233. //小票信息
  234. $POSTFIELDS = [
  235. 'receipt-data' => $receipt_data,
  236. 'password' => '09ef214173a944808ac648b15fa02167'
  237. ];
  238. $POSTFIELDS = json_encode($POSTFIELDS, 320);
  239. //正式购买地址 沙盒购买地址
  240. $url_buy = "https://buy.itunes.apple.com/verifyReceipt";
  241. $url_sandbox = "https://sandbox.itunes.apple.com/verifyReceipt";
  242. //默认后台控制
  243. if (config('site.ios_pay_sandbox') > 0 ) {
  244. $url = $url_buy;
  245. } else {
  246. $url = $url_sandbox;
  247. }
  248. //强制沙盒
  249. if($sandbox == 1){
  250. $url = $url_sandbox;
  251. }
  252. $ch = curl_init($url);
  253. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  254. curl_setopt($ch, CURLOPT_POST, true);
  255. curl_setopt($ch, CURLOPT_POSTFIELDS, $POSTFIELDS);
  256. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //这两行一定要加,不加会报SSL 错误
  257. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  258. $response = curl_exec($ch);
  259. $errno = curl_errno($ch);
  260. curl_close($ch);
  261. if ($errno != 0) {
  262. return $errno;
  263. } else {
  264. return $response;
  265. }
  266. }
  267. }