123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use think\Db;
- use app\common\library\Transaction;
- class Payios extends Api
- {
- protected $noNeedLogin = [];
- protected $noNeedRight = ['*'];
-
- public function jewel_config_ios(){
- $list = Db::name('payjewel_config_ios')->field('id,money,jewel,bundle_id')->where('is_show',1)->order('weigh asc,id asc')->select();
- $data['jewelconfig'] = $list;
- $wallet = model('wallet')->getWallet($this->auth->id);
- $data['wallet'] = $wallet;
- $data['money_to_jewel'] = config('rmb_to_jewel');
- $this->success('success',$data);
- }
-
- public function jewel_recharge_ios(){
- $rc_id = input('rc_id',0);
- $pay_type = 'ios';
- $platform = 'app';
- $cityname = input('cityname','','trim');
- $uid = $this->auth->id;
- if(!$rc_id){
- $this->error('请选择充值金额');
- }
- if($this->auth->is_auth != 2){
- $this->error('请先完成实名认证');
- }
-
- $recharge_config = Db::name('payjewel_config_ios')->where('id',$rc_id)->find();
- $money = $recharge_config['money'] ?: 0;
- $jewel = $recharge_config['jewel'] ?: 0;
-
- if($money<=0)
- {
- $this->error('支付金额必须大于0');
- }
- if($money > 10000){
- $this->error('支付金额太大');
- }
-
- $data['user_id'] = $uid;
- $data['out_trade_no'] = createUniqueNo('P',$uid);
- $data['order_amount'] = $money;
- $data['createtime'] = time();
- $data['pay_type'] = $pay_type;
- $data['platform'] = $platform;
- $data['order_status'] = 0;
- $data['table_name'] = 'jewel_recharge';
- $data['table_id'] = 0;
- $data['args'] = json_encode(['jewel'=>$jewel,'cityname'=>$cityname]);
- $data['bundle_id'] = $recharge_config['bundle_id'];
- $orderid = Db::name('pay_order')->insertGetId($data);
- $this->success('success',$data['out_trade_no']);
- }
-
- public function jewel_notify_iosnew(){
- $original_transaction_id = input('original_transaction_id','','trim');
- if(!empty($original_transaction_id)){
-
-
- }
-
- $receipt_data = input('apple_receipt', '', 'trim');
- $out_trade_no = input('out_trade_no', '', 'trim');
- $transaction_id = input('transaction_id', '', 'trim');
- if (!$receipt_data || !$out_trade_no || !$transaction_id) {
- $this->error('缺少参数');
- }
- filePut("\r\n\r\n".'新请求');
- $prefix = 'ios充值,登录user_id:'.$this->auth->id.',out_trade_no:'.$out_trade_no.',传入transaction_id:'.$transaction_id.'。';
- filePut($prefix.'参数apple_receipt:'.$receipt_data);
- Db::startTrans();
-
- $order_map = [
- 'out_trade_no' => $out_trade_no,
- ];
- $order_info = Db::name('pay_order')->where($order_map)->lock(true)->find();
- if (!$order_info) {
- Db::rollback();
- filePut($prefix.'不存在的订单');
- $this->error('不存在的订单');
- }
- if ($order_info['order_status'] == 1) {
- Db::rollback();
- filePut($prefix.'充值早已完成');
- $this->success('充值已完成');
- }
-
- $result = $this->validate_apple_pay($receipt_data);
- if (!$result['status']) {
- Db::rollback();
- filePut($prefix.'验证'.$result['message']);
- $this->error($result['message']);
- }
- $in_app = $result['data']['receipt']['in_app'];
- $only_trans = [];
- foreach($in_app as $key => $trans){
-
-
- if($trans['transaction_id'] == $transaction_id && $transaction_id == $trans['original_transaction_id'] && $trans['product_id'] == $order_info['bundle_id']){
- $only_trans = $trans;
- break;
- }
- }
- if(empty($only_trans)){
- Db::rollback();
- filePut($prefix.'未找到匹配的交易,产品id'.$order_info['bundle_id'].',原始交易id'.$transaction_id);
- $this->error('未找到匹配的交易,产品id'.$order_info['bundle_id'].',原始交易id'.$transaction_id);
- }
-
- $args = json_decode($order_info['args'],true);
- $extend = [
- 'cityname' => $args['cityname']
- ];
-
- if($order_info['table_name'] == 'jewel_recharge'){
- $result = model('Wallet')->lockChangeAccountRemain($order_info['user_id'],$args['jewel'],'+',0,'金币充值',33,'jewel',$extend);
- if($result['status']===false)
- {
- filePut($prefix.'逻辑添加金币失败');
- Db::rollback();
- $this->error('充值失败');
- }
-
- \app\common\model\Message::addMessage($order_info['user_id'],"充值金币","已充值金币".$args['jewel']);
-
- $userinfo = Db::name('user')->where('id',$order_info['user_id'])->field('id,nickname,pre_userid')->find();
- if($userinfo['pre_userid']){
- $bili = config('site.introsite_recharge_intro_bili') ?: 3;
- $jewel = bcdiv(bcmul($args['jewel'],$bili,0),100,0);
- if($jewel > 0){
- $result = model('Wallet')->lockChangeAccountRemain($userinfo['pre_userid'],$jewel,'+',0,$userinfo['nickname'].'充值金币返奖励',2,'jewel');
- if($result['status']===false)
- {
- Db::rollback();
- return false;
- }
-
- \app\common\model\Message::addMessage($userinfo['pre_userid'],"邀请收益","已获得邀请收益".$jewel);
- }
- }
- }
-
- if($order_info['table_name'] == 'vip_recharge'){
-
- }
-
- $update_order = [
- 'notifytime'=>time(),
- 'order_status'=>1,
- 'original_transaction_id' => $only_trans['original_transaction_id'],
- 'in_app_one' => json_encode($only_trans),
- ];
- $ros = Db::name('pay_order')->where(['id' => $order_info['id']])->update($update_order);
- if($ros === false) {
- filePut($prefix.'修改订单状态失败');
- Db::rollback();
- $this->error('充值失败');
- }
- Db::commit();
- filePut($prefix.'充值成功');
- $this->success('充值成功');
-
- }
- public function test(){
- $a = input('apple_receipt');
- $b = $this->validate_apple_pay($a);
- echo json_encode($b);
- }
-
- function validate_apple_pay($receipt_data = '') {
-
- if (strlen($receipt_data) < 20) {
- $result = array(
- 'status' => false,
- 'message' => '非法参数'
- );
- return $result;
- }
-
- $html = $this->curl($receipt_data);
- $data = json_decode($html, true);
- $data['sandbox'] = '0';
- if ($data['status'] == '21002') {
- $result = array(
- 'status' => false,
- 'message' => 'status:21002'
- );
- return $result;
- }
-
- if ($data['status'] == '21007') {
-
- $html = $this->curl($receipt_data, 1);
- $data = json_decode($html, true);
- $data['sandbox'] = '1';
- }
- if (isset($_GET['debug'])) {
- exit(json_encode($data));
- }
- if ($data['receipt']['bundle_id'] != 'com.huxiu.tken') {
- $result = array(
- 'status' => false,
- 'message' => '非法请求',
- 'data' => $data
- );
- return $result;
- }
-
- if (intval($data['status']) === 0) {
- $result = array(
- 'status' => true,
- 'message' => '购买成功',
- 'data' => $data
- );
- } else {
- $result = array(
- 'status' => false,
- 'message' => '购买失败 status:' . $data['status']
- );
- }
- return $result;
- }
-
-
- function curl($receipt_data,$sandbox = 0) {
-
- $POSTFIELDS = [
- 'receipt-data' => $receipt_data,
- 'password' => '09ef214173a944808ac648b15fa02167'
- ];
- $POSTFIELDS = json_encode($POSTFIELDS, 320);
-
- $url_buy = "https://buy.itunes.apple.com/verifyReceipt";
- $url_sandbox = "https://sandbox.itunes.apple.com/verifyReceipt";
-
- if (config('site.ios_pay_sandbox') > 0 ) {
- $url = $url_buy;
- } else {
- $url = $url_sandbox;
- }
-
- if($sandbox == 1){
- $url = $url_sandbox;
- }
- $ch = curl_init($url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $POSTFIELDS);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
- $response = curl_exec($ch);
- $errno = curl_errno($ch);
- curl_close($ch);
- if ($errno != 0) {
- return $errno;
- } else {
- return $response;
- }
- }
- }
|