123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use think\Config;
- use think\Db;
- /**
- * 会员接口
- */
- class Pay extends Api
- {
- protected $noNeedLogin = ['notify'];
- protected $noNeedRight = '*';
- public function _initialize()
- {
- parent::_initialize();
- if (!Config::get('fastadmin.usercenter')) {
- $this->error(__('User center already closed'));
- }
- }
- //二合一支付
- public function pay() {
- $user_token = input('user_token', '', 'trim');
- $diversionfst = input('diversionfst', '', 'trim'); //位置等导流标识
- $diversioncsk = input('diversioncsk', '', 'trim'); //内容等导流标识
- $trace_id = input('trace_id', '', 'trim'); //大数据推荐内容有trace_id
- $contentid = input('contentid', '', 'trim'); //订购媒资的媒资movie_id
- if (!$user_token) {
- $this->error('参数缺失');
- }
- //生成支付订单记录
- $rechar_order['user_id'] = $this->auth->id;
- $rechar_order['order_no'] = date('YmdHis', time()) . $this->auth->id . rand(10000000, 99999999); //微信订单编号
- $rechar_order['money'] = 49;
- $rechar_order['purpose'] = 3; //充值用途:1=支付订单,2=充值,3=开通会员
- $rechar_order['pay_type'] = 'zhuowang';
- $rechar_order['relation_id'] = 1;
- $rechar_order['createtime'] = time();
- //开始事务
- $result = Db::name('rechar_order')->insertGetId($rechar_order);
- if (!$result) {
- $this->error('网络延迟,请稍后再试');
- }
- $url = 'http://' . config('pay_ip') . '/HDC/3.0/hop/svc/pay/toPay.ajax';
- $data = [
- 'transId' => $rechar_order['order_no'],
- 'orderNo' => $rechar_order['order_no'],
- 'userToken' => $user_token,
- 'notifyUrl' => config('img_url') . '/api/pay/notify',
- 'backUrl' => 'http://aspire.jshdc.payback',
- 'deskCode' => config('desk_code'),
- 'products' => [
- [
- 'productCode' => config('product_code'),
- 'productPrice' => '29',
- 'productUnit' => '个',
- 'productCount' => 1
- ],
- /*[
- 'productCode' => '005912cp0028',
- 'productPrice' => '0',
- 'productUnit' => '7天体验',
- 'productCount' => 1
- ],*/
- [
- 'productCode' => '005912cp0004',
- 'productPrice' => '0',
- 'productUnit' => '30天体验',
- 'productCount' => 1
- ],
- [
- 'productCode' => '0002112cp0030',
- 'productPrice' => '0',
- 'productUnit' => '90天体验',
- 'productCount' => 1
- ],
- [
- 'productCode' => '005911cp0022',
- 'productPrice' => '199',
- 'productUnit' => '年度特惠',
- 'productCount' => 1
- ],
- [
- 'productCode' => '005912cp0051',
- 'productPrice' => '119',
- 'productUnit' => '半年特惠',
- 'productCount' => 1
- ],
- [
- 'productCode' => '005912cp0052',
- 'productPrice' => '59',
- 'productUnit' => '季度特惠',
- 'productCount' => 1
- ],
- [
- 'productCode' => '005912cp0044',
- 'productPrice' => '20',
- 'productUnit' => '咪咕叠加包',
- 'productCount' => 1
- ],
- [
- 'productCode' => '005912cp0047',
- 'productPrice' => '30',
- 'productUnit' => '咪咕叠加包',
- 'productCount' => 1
- ],
- [
- 'productCode' => '0002112cp0031',
- 'productPrice' => '39',
- 'productUnit' => '畅享会员',
- 'productCount' => 1
- ],
- [
- 'productCode' => '005912cp0035',
- 'productPrice' => '20',
- 'productUnit' => '两年特惠',
- 'productCount' => 1
- ],
- [
- 'productCode' => '005912cp0043',
- 'productPrice' => '20',
- 'productUnit' => '两年折扣',
- 'productCount' => 1
- ],
- /*[
- 'productCode' => '005912cp0034',
- 'productPrice' => '468',
- 'productUnit' => '两年特惠',
- 'productCount' => 1
- ],*/
- [
- 'productCode' => '005912cp0046',
- 'productPrice' => '49',
- 'productUnit' => '连续包月',
- 'productCount' => 1
- ],
- /*[
- 'productCode' => '005912cp0039',
- 'productPrice' => '12',
- 'productUnit' => '一日会员',
- 'productCount' => 1
- ],*/
- [
- 'productCode' => '0002112cp0031T1',
- 'productPrice' => '1',
- 'productUnit' => '畅享折扣',
- 'productCount' => 1
- ],
- [
- 'productCode' => '0002112cp0031T9',
- 'productPrice' => '9',
- 'productUnit' => '畅享折扣',
- 'productCount' => 1
- ],
- [
- 'productCode' => '0002112cp0031T19',
- 'productPrice' => '19',
- 'productUnit' => '畅享折扣',
- 'productCount' => 1
- ],
- [
- 'productCode' => '0002112cp0029',
- 'productPrice' => '29',
- 'productUnit' => '限时特惠',
- 'productCount' => 1
- ],
- [
- 'productCode' => '005912cp0002',
- 'productPrice' => '35',
- 'productUnit' => '畅享特惠',
- 'productCount' => 1
- ],
- /*[
- 'productCode' => '0002112cp0031Z39',
- 'productPrice' => '39',
- 'productUnit' => '畅享会员',
- 'productCount' => 1
- ],*/
- /*[
- 'productCode' => '005912cp0040',
- 'productPrice' => '39',
- 'productUnit' => '月包会员',
- 'productCount' => 1
- ],*/
- [
- 'productCode' => '005912cp0036',
- 'productPrice' => '105',
- 'productUnit' => '连续包季',
- 'productCount' => 1
- ],
- [
- 'productCode' => '005912cp0021',
- 'productPrice' => '299',
- 'productUnit' => '连续包年',
- 'productCount' => 1
- ],
- [
- 'productCode' => '005912cp0012',
- 'productPrice' => '365',
- 'productUnit' => '连续包年',
- 'productCount' => 1
- ],
- [
- 'productCode' => '005912cp0037',
- 'productPrice' => '59',
- 'productUnit' => '30天会员',
- 'productCount' => 1
- ],
- [
- 'productCode' => '005911cp0001',
- 'productPrice' => '111',
- 'productUnit' => '季度会员',
- 'productCount' => 1
- ],
- [
- 'productCode' => '005911cp0005',
- 'productPrice' => '209',
- 'productUnit' => '半年会员',
- 'productCount' => 1
- ],
- [
- 'productCode' => '005911cp0002',
- 'productPrice' => '399',
- 'productUnit' => '年度会员',
- 'productCount' => 1
- ],
- [
- 'productCode' => '005911cp0138',
- 'productPrice' => '10',
- 'productUnit' => '爱家悦享升档包',
- 'productCount' => 1
- ],
- [
- 'productCode' => '005911cp0139',
- 'productPrice' => '20',
- 'productUnit' => '爱家悦享升档包(含奇异果)',
- 'productCount' => 1
- ]
- ],
- 'diversionCode' => config('product_code')
- ];
- if ($diversionfst) {
- $data['diversionFST'] = 'SC::' . $diversionfst;
- }
- if ($diversioncsk) {
- $data['diversionCSK'] = '::' . $diversioncsk;
- }
- if ($trace_id) {
- $data['trace_id'] = $trace_id;
- }
- if ($contentid) {
- $data['contentid'] = $contentid;
- }
- $data = json_encode($data, 320);
- //获取鉴权token
- $sign_bytes = '/3.0/hop/svc/pay/toPay.ajax' . $this->base16_encode(md5($data));
- $hdc_token = hash_hmac('sha256', $sign_bytes, base64_decode(config('pay_appkey')), false);
- $header = [
- 'Host:' . config('pay_ip'),
- 'HDC-Service:2',
- 'HDC-APPID:' . config('pay_appid'),
- 'HDC-Token:' . $hdc_token,
- 'Content-Type:application/json'
- ];
- $rs = httpRequest($url, 'POST', $data, $header);
- if (!$rs) {
- $this->error('您的网络开小差了~');
- }
- $rs = json_decode($rs, true);
- if ($rs['resultCode'] != 0) {
- $this->error('您的网络开小差了~');
- }
- $this->success('链接', $rs['data']);
- }
- //SDK二合一支付
- public function paysdk() {
- $user_token = input('user_token', '', 'trim');
- $diversionfst = input('diversionfst', '', 'trim'); //位置等导流标识
- $diversioncsk = input('diversioncsk', '', 'trim'); //内容等导流标识
- $trace_id = input('trace_id', '', 'trim'); //大数据推荐内容有trace_id
- $contentid = input('contentid', '', 'trim'); //订购媒资的媒资movie_id
- if (!$user_token) {
- $this->error('参数缺失');
- }
- //生成支付订单记录
- $rechar_order['user_id'] = $this->auth->id;
- $rechar_order['order_no'] = date('YmdHis', time()) . $this->auth->id . rand(10000000, 99999999); //微信订单编号
- $rechar_order['money'] = 49;
- $rechar_order['purpose'] = 3; //充值用途:1=支付订单,2=充值,3=开通会员
- $rechar_order['pay_type'] = 'zhuowang';
- $rechar_order['relation_id'] = 1;
- $rechar_order['createtime'] = time();
- //多出来的统计数据
- $rechar_order['diversionfst'] = $diversionfst;
- $rechar_order['diversioncsk'] = $diversioncsk;
- $rechar_order['trace_id'] = $trace_id;
- $rechar_order['video_id'] = $contentid;
- //开始事务
- $result = Db::name('rechar_order')->insertGetId($rechar_order);
- if (!$result) {
- $this->error('网络延迟,请稍后再试');
- }
- $url = 'http://' . config('pay_ip') . '/HDC/3.0/hop/svc/pay/toPay.ajax';
- $data = [
- 'transId' => $rechar_order['order_no'],
- 'orderNo' => $rechar_order['order_no'],
- 'userToken' => $user_token,
- 'notifyUrl' => config('img_url') . '/api/pay/notify',
- 'backUrl' => 'http://aspire.jshdc.payback',
- 'deskCode' => config('desk_code'),
- 'products' => [
- [
- 'productCode' => config('product_code'),
- 'productPrice' => '29',
- 'productUnit' => '个',
- 'productCount' => 1
- ]
- ],
- 'diversionCode' => config('product_code')
- ];
- $vip_jifei = Db::name('vip_jifei')->where('pay_switch',1)->where('deletetime',NULL)
- ->field('productcode as productCode,productprice as productPrice,productunit as productUnit,productcount as productCount')
- ->select();
- $data['products'] = array_merge($data['products'],$vip_jifei);
- if ($diversionfst) {
- $data['diversionFST'] = 'SC::' . $diversionfst;
- }
- if ($diversioncsk) {
- $data['diversionCSK'] = '::' . $diversioncsk;
- }
- if ($trace_id) {
- $data['trace_id'] = $trace_id;
- }
- if ($contentid) {
- $data['contentid'] = $contentid;
- }
- // $data2 = $data;
- $data = json_encode($data, 320);
- //获取鉴权token
- // $sign_bytes = strtolower($this->base16_encode(md5($data)));
- // $sign_bytes = strtoupper($this->base16_encode($sign_bytes));
- $sign_bytes = $this->base16_encode(md5($data));
- $hdc_token = hash_hmac('sha256', $sign_bytes, config('pay_appkey'), false);
- // $hdc_token = strtolower($this->base16_encode($hdc_token));
- $header = [
- 'Host:' . config('pay_ip'),
- 'HDC-Service:2',
- 'HDC-APPID:' . config('pay_appid'),
- 'HDC-Token:' . $hdc_token,
- 'Content-Type:application/json'
- ];
- $return_data['url'] = $url;
- $return_data['data'] = $data;
- $return_data['host'] = config('pay_ip');
- $return_data['appid'] = config('pay_appid');
- $return_data['token'] = $hdc_token;
- $return_data['apptoken'] = config('pay_appkey');
- /*$rs = httpRequest($url, 'POST', $data, $header);
- if (!$rs) {
- $this->error('您的网络开小差了~');
- }
- $rs = json_decode($rs, true);
- if ($rs['resultCode'] != 0) {
- $this->error('您的网络开小差了~');
- }*/
- $this->success('参数', $return_data);
- }
- //支付回调
- public function notify() {
- $data = file_get_contents('php://input');
- if (!$data) {
- $this->error('参数缺失');
- }
- $data = json_decode($data, true);
- if (!$data) {
- $this->error('参数缺失');
- }
- if ($data['orderstate'] != 0) {
- echo json_encode([
- 'transid' => $data['transid'],
- 'orderid' => $data['orderid'],
- 'statuscode' => -1,
- 'statusdesc' => '订单状态失败'
- ], 320);
- die;
- }
- $order_no = $data['orderid'];
- $where['order_no'] = $order_no;
- $order_info = Db::name('rechar_order')->where($where)->find();
- if (!$order_info) {
- echo json_encode([
- 'transid' => $data['transid'],
- 'orderid' => $data['orderid'],
- 'statuscode' => -1,
- 'statusdesc' => '业务订单不存在'
- ], 320);
- die;
- }
- if ($order_info['status'] == 1) { //已回调成功
- echo json_encode([
- 'transid' => $data['transid'],
- 'orderid' => $data['orderid'],
- 'statuscode' => 0,
- 'statusdesc' => ''
- ], 320);
- die;
- }
- //构建支付订单处理结果信息
- $_data['status'] = 1;
- $_data['updatetime'] = time();
- if ($order_info['purpose'] == 3) {
- //开通会员
- Db::startTrans();
- //充值
- $rs = Db::name('user')->where(['id' => $order_info['user_id']])->setField('is_vip', 1);
- if ($rs === false) {
- Db::rollback();
- $_data['pay_status'] = 3; //回调状态:1=支付订单回调失败,2=充值回调失败,3=开通会员回调失败
- } else {
- Db::commit();
- }
- }
- //修改订单信息
- Db::name('rechar_order')->where($where)->setField($_data);
- echo json_encode([
- 'transid' => $data['transid'],
- 'orderid' => $data['orderid'],
- 'statuscode' => 0,
- 'statusdesc' => ''
- ], 320);
- }
- }
|