Payios.php 11 KB

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