123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350 |
- <?php
- namespace app\api\controller;
- use think\facade\Db;
- use think\facade\Log;
- class Ticket extends MobileMember
- {
- protected $domain = "https://ilike-cloud-mapi.zmaxfilm.com";
- public function initialize()
- {
- parent::initialize();
- }
- public function login()
- {
- if (empty($this->member_info['member_mobile'])) {
- $this->output(10001, '请先绑定手机号!');
- }
- $url = $this->domain . '/api/public/thirdLogin';
- $data = [];
- $data['account'] = 'bbfuli1117';
- $data['mobile'] = $this->member_info['member_mobile'];
- $data['openId'] = $this->member_info['member_token'];
- $data['type'] = 'getToken';
- $data['requestTime'] = date('Y-m-d H:i:s');
- $data['verifyInfo'] = $this->makesign($data);
- $result = $this->curl($url, $data);
- $result = json_decode($result, true);
- if ($result['status'] != 0) {
- $this->output(10001, $result['content']);
- }
- $rdata = $result['data'];
- $this->output(10000, '登录成功', ['jump_url' => $rdata['jumpUrl']]);
- }
- public function order_film_pay($order_info)
- {
- $url = $this->domain . '/pay/pay/orderFilmPay';
- $data = [];
- $data['account'] = 'bbfuli1117';
- $data['payType'] = 'bbflpay';
- $data['amount'] = $order_info['order_amount'];
- $data['outTradeNo'] = $order_info['order_sn'];
- $data['merchantOrderNo'] = $order_info['third_pay_order_id'];
- $data['sign'] = $this->makesign($data);
- $result = $this->curl($url, $data);
- Log::info('电影订单支付成功接口返回消息:' . $result);
- $result = json_decode($result, true);
- if ($result['status'] != 0) {
- return false;
- }
- return true;
- }
- public function order()
- {
- $member_info = $this->member_info;
- // buyer_msg: ""
- // buyer_phone: "13788854878"
- // card_id: "101"
- // goods_id: 905
- // quantity: 1
- // voucher: "undefined|5|undefined"
- $third_order_id = input('param.third_order_id');
- $third_pay_order_id = input('param.third_pay_order_id');
- $TicketOrder = model('TicketOrder');
- $ticket_check = $TicketOrder->where('third_order_id', $third_order_id)->where('third_pay_order_id', $third_pay_order_id)->find();
- if (!empty($ticket_check)) {
- $this->output(10000, '订单保存成功', ['order_id', $ticket_check['id'], 'order_sn' => $ticket_check['order_sn'], 'pay_type' => 'ticket_pay']);
- }
- $ticket_price = input('param.ticket_price');
- $ticket_price = floatval($ticket_price);
- if ($ticket_price <= 0) {
- $this->output(10001, '票价必须大于0');
- }
- $ticket_num = input('param.ticket_num');
- $ticket_num = intval($ticket_num);
- if ($ticket_num <= 0) {
- $this->output(10001, '票数必须大于0');
- }
- $order_amount = input('param.total_price');
- $order = [];
- $order['order_state'] = 10;
- $order['deduction_amount'] = 0;
- $orderSn = makePaySn($member_info['member_id']);
- $logic_payment = model('payment', 'logic'); //推送消息记录
- $card_id = input('param.card_id');
- if (!empty($card_id) || $card_id != 0) {
- if (strpos($card_id, ',') !== false) {
- $card_ids = explode(',', $card_id);
- } else {
- $card_ids[] = $card_id;
- }
- $SubCard = model('Subcard');
- foreach ($card_ids as $key => $value) {
- $where = [];
- $where['id'] = $value;
- $where['member_id'] = $member_info['member_id'];
- $cardInfo = $SubCard->where($where)->find();
- if (!$cardInfo && $cardInfo['card_status'] != 3 && $cardInfo['balance'] != 0) {
- continue;
- }
- if (!empty($cardInfo['bind_cate']) && strpos($cardInfo['bind_cate'],'9999') === false) {
- continue;
- }
- $balance = $cardInfo['balance'];
- //1.卡的余额要变, 订单金额也要变
- if ($balance < $order_amount - $order['deduction_amount']) {
- //余额小于 先冻结起来
- $pay_amount = $balance;
- $order['deduction_amount'] += $pay_amount;
- // $unpaid_amount =
- // $order['card_id'] = $value;
- $updateInfo = [
- 'balance' => 0,
- 'card_status' => 3,//用完了
- 'freeze' => $cardInfo['freeze'] + $pay_amount, //余额冻结
- 'used_count' => $cardInfo['used_count'] + 1,
- 'update_time' => time(),
- ];
- $info = [
- 'cardNo' => $cardInfo['card_no'],
- 'memberId' => $member_info['member_id'],
- 'amount' => $pay_amount,
- 'orderNo' => $orderSn,
- ];
- $logic_payment->payRecordPushNoAmount($info, 1);
- } else {
- //余额大于等于订单金额
- $pay_amount = $order_amount - $order['deduction_amount'];
- $order['deduction_amount'] += $pay_amount;
- // $order['card_id'] = $cardId;
- $order['payment_time'] = TIMESTAMP;
- $order['payment_code'] = 'card';
- $updateInfo = [
- 'balance' => $balance - $pay_amount,
- 'card_status' => 2, //状态为使用中
- 'freeze' => $cardInfo['freeze'] + $pay_amount, //余额冻结
- 'used_count' => $cardInfo['used_count'] + 1,
- 'update_time' => time(),
- ];
- $order['order_state'] = 20; //直接就到了待发货阶段
- $info = [
- 'cardNo' => $cardInfo['card_no'],
- 'memberId' => $member_info['member_id'],
- 'amount' => $pay_amount,
- 'orderNo' => $orderSn,
- ];
- $logic_payment->payRecordPushNoAmount($info, 1);
- }
- $res = Db::name('sub_card')->where([
- ['id', '=', $value],
- ])->update($updateInfo);
- $expenseData[$key] = [
- 'member_id' => $member_info['member_id'],
- 'card_id' => $value,
- 'expense_amount' => $pay_amount, //抵消金额
- 'order_amount' => $order_amount,
- 'expense_time' => time(),
- ];
- }
- }
- $order['order_sn'] = $orderSn;
- $order['account'] = 'bbfuli1117';
- $order['buyer_id'] = $member_info['member_id'];
- $order['buyer_name'] = $member_info['member_name'];
- $order['add_time'] = TIMESTAMP;
- $order['order_amount'] = $order_amount;
- $order['pd_amount'] = 0;
- $order['store_name'] = '电影票';
- $order['third_order_id'] = $third_order_id;
- $order['third_pay_order_id'] = $third_pay_order_id;
- $order['ticket_price'] = $ticket_price;
- $order['ticket_num'] = $ticket_num;
- $TicketOrder->startTrans();
- $result = $TicketOrder->save($order);
- if (!$result) {
- $TicketOrder->rollback();
- $this->output(10001, '订单保存失败');
- }
- $order_id = $TicketOrder->id;
- if (!empty($expenseData)) {
- foreach ($expenseData as $key => $value) {
- //插入消费记录
- $value['order_id'] = $order_id;
- $value['is_vir'] = 2;
- $addExpense = Db::name('sub_card_expense')->insert($value);
- }
- if ($order['deduction_amount'] == $order['order_amount']) {
- $order['order_id'] = $order_id;
- $logic_payment->paysuccesspush($order, 2);
- }
- }
- $TicketOrder->commit();
- $data = [];
- $data['order_id'] = $order_id;
- $data['order_sn'] = $orderSn;
- $data['pay_type'] = 'ticket_pay';
- $this->output(10000, '订单保存成功', $data);
- }
- public function pay()
- {
- $pay_sn = input('param.pay_sn');
- $condition = [];
- $condition[] = ['order_sn', '=', $pay_sn];
- $order_info = model('TicketOrder')->detail($condition);
- if ($order_info['order_state'] != 10) {
- $this->output(12001, lang('pay_repeat'));
- }
- // var_dump($order_info);exit();
- $condition = [];
- $condition[] = ['payment_platform', '=', 'h5'];
- $payment_list = model('payment')->getPaymentOpenList($condition);
- $pay_info['pay_amount'] = $order_info['order_amount'];
- $pay_info['member_available_pd'] = $this->member_info['available_predeposit'];
- $pay_info['member_available_rcb'] = $this->member_info['available_rc_balance'];
- $pay_info['member_available_point'] = $this->member_info['point'];
- $pay_info['member_paypwd'] = true;
- if (empty($this->member_info['member_paypwd'])) {
- $pay_info['member_paypwd'] = false;
- }
- $pay_info['pay_sn'] = $order_info['order_sn'];
- $pay_info['payed_amount'] = $order_info['pd_amount'] + $order_info['rcb_amount'] + $order_info['deduction_amount'];
- if ($pay_info['payed_amount'] > '0.00') {
- $pay_info['pay_amount'] = $pay_info['pay_amount'] - $pay_info['payed_amount'];
- }
- //如果线上线下支付金额都为0,转到支付成功页
- if (empty($pay_info['pay_amount'])) {
- $this->output(12001, lang('pay_repeat'));
- }
- $pay_in["pay_info"] = $pay_info;
- $pay_in["pay_info"]["payment_list"] = $payment_list;
- $this->output(10000, '', $pay_in);
- }
- protected function output($code, $msg, $data = [], $count = 0)
- {
- try {
- // 返回JSON数据格式到客户端 包含状态信息
- $count = $count == 0 ? count($data) : $count;
- $data = ['code' => $code, 'msg' => $msg, 'data' => $data, 'count' => $count, 'time' => $this->request->time()];
- if ($data === false) {
- throw new \InvalidArgumentException(json_last_error_msg());
- }
-
- $response = \think\Response::create($data, 'json');
- throw new \think\exception\HttpResponseException($response);
- } catch (\Exception $e) {
- if ($e->getPrevious()) {
- throw $e->getPrevious();
- }
- throw $e;
- }
- }
- private function makesign($data)
- {
- ksort($data);
- $string = $this->tourlparams($data);
- $string = $string . 'OiwYehcnkeflRjVN';
- $string = md5(sha1($string));
- return substr($string, 8, 16);
- }
- private function tourlparams($data)
- {
- $buff = "";
- foreach ($data as $k => $v) {
- if ($k != "sign" && $v != "" && !is_array($v)) {
- $buff .= $k . $v;
- }
- }
- return $buff;
- }
- private function curl($url, $data = '', $headers = [], $timeout = 60, $issplit = false)
- {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- @curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
- if ($data) {
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
- }
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
- curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
- curl_setopt($ch, CURLOPT_SSLVERSION, 1);
- curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1");
- if (!empty($headers) && is_array($headers)) {
- curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
- }
- ob_start();
- $res = curl_exec($ch);
- $headersize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
- $header = substr($res, 0, $headersize);
- $body = substr($res, $headersize);
- ob_end_clean();
- curl_close($ch);
- unset($ch);
- if ($issplit) {
- return ['header' => $header, 'body' => $body];
- }
- return $res;
- }
- }
|