Ticket.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <?php
  2. namespace app\api\controller;
  3. use think\facade\Db;
  4. use think\facade\Log;
  5. class Ticket extends MobileMember
  6. {
  7. protected $domain = "https://ilike-cloud-mapi.zmaxfilm.com";
  8. public function initialize()
  9. {
  10. parent::initialize();
  11. }
  12. public function login()
  13. {
  14. if (empty($this->member_info['member_mobile'])) {
  15. $this->output(10001, '请先绑定手机号!');
  16. }
  17. $url = $this->domain . '/api/public/thirdLogin';
  18. $data = [];
  19. $data['account'] = 'bbfuli1117';
  20. $data['mobile'] = $this->member_info['member_mobile'];
  21. $data['openId'] = $this->member_info['member_token'];
  22. $data['type'] = 'getToken';
  23. $data['requestTime'] = date('Y-m-d H:i:s');
  24. $data['verifyInfo'] = $this->makesign($data);
  25. $result = $this->curl($url, $data);
  26. $result = json_decode($result, true);
  27. if ($result['status'] != 0) {
  28. $this->output(10001, $result['content']);
  29. }
  30. $rdata = $result['data'];
  31. $this->output(10000, '登录成功', ['jump_url' => $rdata['jumpUrl']]);
  32. }
  33. public function order_film_pay($order_info)
  34. {
  35. $url = $this->domain . '/pay/pay/orderFilmPay';
  36. $data = [];
  37. $data['account'] = 'bbfuli1117';
  38. $data['payType'] = 'bbflpay';
  39. $data['amount'] = $order_info['order_amount'];
  40. $data['outTradeNo'] = $order_info['order_sn'];
  41. $data['merchantOrderNo'] = $order_info['third_pay_order_id'];
  42. $data['sign'] = $this->makesign($data);
  43. $result = $this->curl($url, $data);
  44. Log::info('电影订单支付成功接口返回消息:' . $result);
  45. $result = json_decode($result, true);
  46. if ($result['status'] != 0) {
  47. return false;
  48. }
  49. return true;
  50. }
  51. public function order()
  52. {
  53. $member_info = $this->member_info;
  54. // buyer_msg: ""
  55. // buyer_phone: "13788854878"
  56. // card_id: "101"
  57. // goods_id: 905
  58. // quantity: 1
  59. // voucher: "undefined|5|undefined"
  60. $third_order_id = input('param.third_order_id');
  61. $third_pay_order_id = input('param.third_pay_order_id');
  62. $TicketOrder = model('TicketOrder');
  63. $ticket_check = $TicketOrder->where('third_order_id', $third_order_id)->where('third_pay_order_id', $third_pay_order_id)->find();
  64. if (!empty($ticket_check)) {
  65. $this->output(10000, '订单保存成功', ['order_id', $ticket_check['id'], 'order_sn' => $ticket_check['order_sn'], 'pay_type' => 'ticket_pay']);
  66. }
  67. $ticket_price = input('param.ticket_price');
  68. $ticket_price = floatval($ticket_price);
  69. if ($ticket_price <= 0) {
  70. $this->output(10001, '票价必须大于0');
  71. }
  72. $ticket_num = input('param.ticket_num');
  73. $ticket_num = intval($ticket_num);
  74. if ($ticket_num <= 0) {
  75. $this->output(10001, '票数必须大于0');
  76. }
  77. $order_amount = input('param.total_price');
  78. $order = [];
  79. $order['order_state'] = 10;
  80. $order['deduction_amount'] = 0;
  81. $orderSn = makePaySn($member_info['member_id']);
  82. $logic_payment = model('payment', 'logic'); //推送消息记录
  83. $card_id = input('param.card_id');
  84. if (!empty($card_id) || $card_id != 0) {
  85. if (strpos($card_id, ',') !== false) {
  86. $card_ids = explode(',', $card_id);
  87. } else {
  88. $card_ids[] = $card_id;
  89. }
  90. $SubCard = model('Subcard');
  91. foreach ($card_ids as $key => $value) {
  92. $where = [];
  93. $where['id'] = $value;
  94. $where['member_id'] = $member_info['member_id'];
  95. $cardInfo = $SubCard->where($where)->find();
  96. if (!$cardInfo && $cardInfo['card_status'] != 3 && $cardInfo['balance'] != 0) {
  97. continue;
  98. }
  99. if (!empty($cardInfo['bind_cate']) && strpos($cardInfo['bind_cate'],'9999') === false) {
  100. continue;
  101. }
  102. $balance = $cardInfo['balance'];
  103. //1.卡的余额要变, 订单金额也要变
  104. if ($balance < $order_amount - $order['deduction_amount']) {
  105. //余额小于 先冻结起来
  106. $pay_amount = $balance;
  107. $order['deduction_amount'] += $pay_amount;
  108. // $unpaid_amount =
  109. // $order['card_id'] = $value;
  110. $updateInfo = [
  111. 'balance' => 0,
  112. 'card_status' => 3,//用完了
  113. 'freeze' => $cardInfo['freeze'] + $pay_amount, //余额冻结
  114. 'used_count' => $cardInfo['used_count'] + 1,
  115. 'update_time' => time(),
  116. ];
  117. $info = [
  118. 'cardNo' => $cardInfo['card_no'],
  119. 'memberId' => $member_info['member_id'],
  120. 'amount' => $pay_amount,
  121. 'orderNo' => $orderSn,
  122. ];
  123. $logic_payment->payRecordPushNoAmount($info, 1);
  124. } else {
  125. //余额大于等于订单金额
  126. $pay_amount = $order_amount - $order['deduction_amount'];
  127. $order['deduction_amount'] += $pay_amount;
  128. // $order['card_id'] = $cardId;
  129. $order['payment_time'] = TIMESTAMP;
  130. $order['payment_code'] = 'card';
  131. $updateInfo = [
  132. 'balance' => $balance - $pay_amount,
  133. 'card_status' => 2, //状态为使用中
  134. 'freeze' => $cardInfo['freeze'] + $pay_amount, //余额冻结
  135. 'used_count' => $cardInfo['used_count'] + 1,
  136. 'update_time' => time(),
  137. ];
  138. $order['order_state'] = 20; //直接就到了待发货阶段
  139. $info = [
  140. 'cardNo' => $cardInfo['card_no'],
  141. 'memberId' => $member_info['member_id'],
  142. 'amount' => $pay_amount,
  143. 'orderNo' => $orderSn,
  144. ];
  145. $logic_payment->payRecordPushNoAmount($info, 1);
  146. }
  147. $res = Db::name('sub_card')->where([
  148. ['id', '=', $value],
  149. ])->update($updateInfo);
  150. $expenseData[$key] = [
  151. 'member_id' => $member_info['member_id'],
  152. 'card_id' => $value,
  153. 'expense_amount' => $pay_amount, //抵消金额
  154. 'order_amount' => $order_amount,
  155. 'expense_time' => time(),
  156. ];
  157. }
  158. }
  159. $order['order_sn'] = $orderSn;
  160. $order['account'] = 'bbfuli1117';
  161. $order['buyer_id'] = $member_info['member_id'];
  162. $order['buyer_name'] = $member_info['member_name'];
  163. $order['add_time'] = TIMESTAMP;
  164. $order['order_amount'] = $order_amount;
  165. $order['pd_amount'] = 0;
  166. $order['store_name'] = '电影票';
  167. $order['third_order_id'] = $third_order_id;
  168. $order['third_pay_order_id'] = $third_pay_order_id;
  169. $order['ticket_price'] = $ticket_price;
  170. $order['ticket_num'] = $ticket_num;
  171. $TicketOrder->startTrans();
  172. $result = $TicketOrder->save($order);
  173. if (!$result) {
  174. $TicketOrder->rollback();
  175. $this->output(10001, '订单保存失败');
  176. }
  177. $order_id = $TicketOrder->id;
  178. if (!empty($expenseData)) {
  179. foreach ($expenseData as $key => $value) {
  180. //插入消费记录
  181. $value['order_id'] = $order_id;
  182. $value['is_vir'] = 2;
  183. $addExpense = Db::name('sub_card_expense')->insert($value);
  184. }
  185. if ($order['deduction_amount'] == $order['order_amount']) {
  186. $order['order_id'] = $order_id;
  187. $logic_payment->paysuccesspush($order, 2);
  188. }
  189. }
  190. $TicketOrder->commit();
  191. $data = [];
  192. $data['order_id'] = $order_id;
  193. $data['order_sn'] = $orderSn;
  194. $data['pay_type'] = 'ticket_pay';
  195. $this->output(10000, '订单保存成功', $data);
  196. }
  197. public function pay()
  198. {
  199. $pay_sn = input('param.pay_sn');
  200. $condition = [];
  201. $condition[] = ['order_sn', '=', $pay_sn];
  202. $order_info = model('TicketOrder')->detail($condition);
  203. if ($order_info['order_state'] != 10) {
  204. $this->output(12001, lang('pay_repeat'));
  205. }
  206. // var_dump($order_info);exit();
  207. $condition = [];
  208. $condition[] = ['payment_platform', '=', 'h5'];
  209. $payment_list = model('payment')->getPaymentOpenList($condition);
  210. $pay_info['pay_amount'] = $order_info['order_amount'];
  211. $pay_info['member_available_pd'] = $this->member_info['available_predeposit'];
  212. $pay_info['member_available_rcb'] = $this->member_info['available_rc_balance'];
  213. $pay_info['member_available_point'] = $this->member_info['point'];
  214. $pay_info['member_paypwd'] = true;
  215. if (empty($this->member_info['member_paypwd'])) {
  216. $pay_info['member_paypwd'] = false;
  217. }
  218. $pay_info['pay_sn'] = $order_info['order_sn'];
  219. $pay_info['payed_amount'] = $order_info['pd_amount'] + $order_info['rcb_amount'] + $order_info['deduction_amount'];
  220. if ($pay_info['payed_amount'] > '0.00') {
  221. $pay_info['pay_amount'] = $pay_info['pay_amount'] - $pay_info['payed_amount'];
  222. }
  223. //如果线上线下支付金额都为0,转到支付成功页
  224. if (empty($pay_info['pay_amount'])) {
  225. $this->output(12001, lang('pay_repeat'));
  226. }
  227. $pay_in["pay_info"] = $pay_info;
  228. $pay_in["pay_info"]["payment_list"] = $payment_list;
  229. $this->output(10000, '', $pay_in);
  230. }
  231. protected function output($code, $msg, $data = [], $count = 0)
  232. {
  233. try {
  234. // 返回JSON数据格式到客户端 包含状态信息
  235. $count = $count == 0 ? count($data) : $count;
  236. $data = ['code' => $code, 'msg' => $msg, 'data' => $data, 'count' => $count, 'time' => $this->request->time()];
  237. if ($data === false) {
  238. throw new \InvalidArgumentException(json_last_error_msg());
  239. }
  240. $response = \think\Response::create($data, 'json');
  241. throw new \think\exception\HttpResponseException($response);
  242. } catch (\Exception $e) {
  243. if ($e->getPrevious()) {
  244. throw $e->getPrevious();
  245. }
  246. throw $e;
  247. }
  248. }
  249. private function makesign($data)
  250. {
  251. ksort($data);
  252. $string = $this->tourlparams($data);
  253. $string = $string . 'OiwYehcnkeflRjVN';
  254. $string = md5(sha1($string));
  255. return substr($string, 8, 16);
  256. }
  257. private function tourlparams($data)
  258. {
  259. $buff = "";
  260. foreach ($data as $k => $v) {
  261. if ($k != "sign" && $v != "" && !is_array($v)) {
  262. $buff .= $k . $v;
  263. }
  264. }
  265. return $buff;
  266. }
  267. private function curl($url, $data = '', $headers = [], $timeout = 60, $issplit = false)
  268. {
  269. $ch = curl_init();
  270. curl_setopt($ch, CURLOPT_URL, $url);
  271. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  272. @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  273. curl_setopt($ch, CURLOPT_HEADER, 0);
  274. @curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
  275. if ($data) {
  276. curl_setopt($ch, CURLOPT_POST, 1);
  277. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  278. }
  279. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  280. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  281. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  282. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  283. curl_setopt($ch, CURLOPT_SSLVERSION, 1);
  284. curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1");
  285. if (!empty($headers) && is_array($headers)) {
  286. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  287. }
  288. ob_start();
  289. $res = curl_exec($ch);
  290. $headersize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
  291. $header = substr($res, 0, $headersize);
  292. $body = substr($res, $headersize);
  293. ob_end_clean();
  294. curl_close($ch);
  295. unset($ch);
  296. if ($issplit) {
  297. return ['header' => $header, 'body' => $body];
  298. }
  299. return $res;
  300. }
  301. }