123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681 |
- <?php
- namespace App\Http\Controllers\QuickApp\Order;
- use App\Modules\Statistic\Services\AdVisitStatService;
- use App\Http\Controllers\QuickApp\BaseController;
- use Illuminate\Http\Request;
- use App\Modules\Subscribe\Services\BookOrderService;
- use App\Modules\Subscribe\Services\ChapterOrderService;
- use App\Modules\Subscribe\Services\OrderService;
- use App\Http\Controllers\QuickApp\Order\Transformers\BookOrderTransformer;
- use App\Http\Controllers\QuickApp\Order\Transformers\ChapterOrderTransformer;
- use App\Http\Controllers\QuickApp\Order\Transformers\ChargeListTransformer;
- use App\Libs\Pay\WechatPay;
- use App\Modules\Subscribe\Services\YearOrderService;
- use App\Modules\User\Services\UserService;
- use DB;
- use Hashids;
- use EasyWeChat\Foundation\Application;
- use App\Modules\Product\Services\ProductService;
- use App\Modules\Book\Services\BookConfigService;
- use App\Modules\Book\Services\BookService;
- use App\Modules\Channel\Services\PayTemplateService;
- use Log;
- class OrdersController extends BaseController
- {
- /**
- * @apiDefine Order 订单
- */
- /**
- *
- */
- private $chargeList;
- public function exchangeList()
- {
- foreach ($this->chargeList as &$item) {
- if (!$item->switch_to) continue;
- $order = Order::where('uid', $this->uid)->where('status', 'PAID')->where('product_id', $item->id)->first();
- if ($order) {
- $change = ProductService::getProductSingle($item->switch_to);
- $item->id = $change->id;
- $item->price = $change->price;
- $item->given = $change->given;
- }
- }
- }
- /**
- * @apiVersion 1.0.0
- * @apiDescription 充值列表
- * @api {get} order/chargeList 充值列表
- * @apiHeader {String} [Authorization] token
- * @apiGroup Order
- * @apiName chargeList
- * @apiSuccess {int} code 状态码
- * @apiSuccess {String} msg 信息
- * @apiSuccess {object} data 结果集
- * @apiSuccessExample {json} Success-Response:
- * HTTP/1.1 200 OK
- * {
- * code: 0,
- * msg: "",
- * data: [
- * {
- * product_id: 1,
- * price: "30.00元",
- * vip: 0,
- * intro: [
- * {
- * label: 3000,
- * important: false
- * },
- * {
- * label: "书币",
- * important: true
- * }
- * ]
- * },
- * {
- * product_id: 2,
- * price: "50.00元",
- * vip: 1,
- * intro: [
- * {
- * label: 5000,
- * important: false
- * },
- * {
- * label: "1000+",
- * important: true
- * },
- * {
- * label: "书币",
- * important: false
- * }
- * ]
- * },
- * {
- * product_id: 5,
- * price: "365.00元",
- * vip: 0,
- * intro: [
- * {
- * label: "年费VIP会员",
- * important: true
- * }
- * ]
- * }
- * ]
- * }
- */
- public function chargeList(Request $request)
- {
- $bid = $request->input('bid', '');
- $temp = $bid;
- $template_id = PayTemplateService::getPayTemplate($this->distribution_channel_id);
- $book_config = null;
- if ($bid) {
- $bid = Hashids::decode($bid)[0];
- $book_config = BookConfigService::getBookById($bid);
- }
- if ($template_id == 2) { //模板2只有在长篇小说过来的用户才显示
- //部分渠道需要2元模板不管哪个入口进来都展示
- $exclude_channels = explode(',', env('PRICE_TWO_SHOW_ALL_CHANNEL'));
- if (!in_array($this->distribution_channel_id, $exclude_channels)) {
- if ($book_config) {
- if ($book_config->charge_type == 'BOOK') $template_id = 1;
- }
- }
- }
- $res = ProductService::getChargeProduct($template_id);
- if (!$res) {
- return response()->error('WAP_SYS_ERROR');
- }
- $this->chargeList = $res;
- $this->exchangeList();
- //TODO 长篇小数才有模板2
- $uid = $this->uid;
- $is_first_recharge = OrderService::judgeUserFirstRecharge($uid);
- $data = [];
- $appad = 0;
- foreach ($res as $v) {
- if ($template_id == 7 && $book_config && $book_config->charge_type == 'BOOK' && $v->price == 2) {
- continue;
- }
- if ($template_id == 2 && $v->type == 'NEW_USER' && !$bid) {
- //2元模版,直接进充值,不出现
- continue;
- }
- if ($v->type == 'NEW_USER' && $is_first_recharge) {
- if (
- env('NO_NEW_USER_CHARGE') &&
- in_array(
- $this->distribution_channel_id,
- explode(',', env('NO_NEW_USER_CHARGE'))
- )
- ) {
- continue;
- }
- $temp = [
- 'price' => (float) $v->price . '元',
- 'is_year_order' => 0,
- 'is_month_order' => 0,
- 'text' => sprintf('%s+%s书币', $v->price * 100, $v->given),
- 'today_special' => $v->is_default,
- 'first_charge' => true,
- 'save_text' => round($v->given / 100, 1) . '元',
- 'product_id' => $v->id,
- ];
- $data[] = $temp;
- } elseif ($v->type == 'YEAR_ORDER') {
- if ($v->type == 'NEW_USER') {
- continue;
- }
- $save_text = '年费vip会员';
- $text = '每天1元,全年免费看';
- $temp = [
- 'price' => (int) $v->price . '元',
- 'is_year_order' => 1,
- 'is_month_order' => 0,
- 'text' => $text,
- 'today_special' => $v->is_default,
- 'first_charge' => false,
- 'save_text' => $save_text,
- 'product_id' => $v->id,
- ];
- $data[] = $temp;
- }else {
- if ($v->type == 'NEW_USER') {
- continue;
- }
- $save_text = '';
- if ($v->given) {
- $save_text = round($v->given / 100, 1) . '元';
- $text = sprintf('%s+%s书币', $v->price * 100, $v->given);
- } else {
- $text = sprintf('%s书币', $v->price * 100);
- }
- $temp = [
- 'price' => (float) $v->price . '元',
- 'is_year_order' => 0,
- 'is_month_order' => 0,
- 'text' => $text,
- 'today_special' => $v->is_default,
- 'first_charge' => false,
- 'save_text' => $save_text,
- 'product_id' => $v->id,
- ];
- $data[] = $temp;
- }
- }
- return response()->success($data);
- }
- /**
- * @apiVersion 1.0.0
- * @apiDescription 单本消费记录
- * @api {get} order/bookOrderList 单本消费记录
- * @apiHeader {String} [Authorization] token
- * @apiGroup Order
- * @apiName bookOrderList
- * @apiParam {String} [page_size] 分页大小
- * @apiParam {String} [page] 页码
- * @apiSuccess {int} code 状态码
- * @apiSuccess {String} msg 信息
- * @apiSuccess {object} data 结果集
- * @apiSuccess {Int} uid uid
- * @apiSuccess {Int} bid bid
- * @apiSuccess {Int} book_name 书名
- * @apiSuccess {Int} fee 钱
- * @apiSuccess {String} created_at 时间
- * @apiSuccessExample {json} Success-Response:
- * HTTP/1.1 200 OK
- * {
- * code: 0,
- * msg: "",
- * data: list:[
- * {
- * uid: 4,
- * bid: 1,
- * book_name: "dfsedfertrwet",
- * fee: 100,
- * created_at: "2017-12-02 16:24:54"
- * }
- * ]
- * meta: {
- * total: 1,
- * per_page: 15,
- * current_page: 1,
- * last_page: 1,
- * next_page_url: "",
- * prev_page_url: ""
- * }
- * }
- * }
- */
- public function bookOrderList(Request $request)
- {
- $page_size = $request->input('page_size', 15);
- $book_order = BookOrderService::getRecord($this->uid, $page_size);
- return response()->pagination(new BookOrderTransformer(), $book_order);
- }
- /**
- * @apiVersion 1.0.0
- * @apiDescription 章节消费记录
- * @api {get} order/chapterOrderList 章节消费记录
- * @apiHeader {String} [Authorization] token
- * @apiGroup Order
- * @apiName chapterOrderList
- * @apiParam {String} [page_size] 分页大小
- * @apiParam {String} [page] 页码
- * @apiSuccess {int} code 状态码
- * @apiSuccess {String} msg 信息
- * @apiSuccess {object} data 结果集
- * @apiSuccess {Int} uid uid
- * @apiSuccess {Int} bid bid
- * @apiSuccess {Int} cid cid
- * @apiSuccess {Int} chapter_name 章节名
- * @apiSuccess {Int} book_name 书名
- * @apiSuccess {Int} fee 钱
- * @apiSuccess {String} created_at 时间
- * @apiSuccessExample {json} Success-Response:
- * HTTP/1.1 200 OK
- * {
- * code: 0,
- * msg: "",
- * data: list:[
- * {
- * uid: 4,
- * bid: 1,
- * cid: 1,
- * chapter_name: "sdfsd",
- * book_name: "dfsedfertrwet",
- * fee: 100,
- * created_at: "2017-12-02 16:24:54"
- * }
- * ]
- * meta: {
- * total: 1,
- * per_page: 15,
- * current_page: 1,
- * last_page: 1,
- * next_page_url: "",
- * prev_page_url: ""
- * }
- * }
- */
- public function chapterOrderList(Request $request)
- {
- $chapter_model = new ChapterOrderService();
- $page_size = $request->input('page_size', 15);
- $chapter_order = $chapter_model->getByUid($this->uid, $page_size);
- foreach ($chapter_order as $item) {
- if ($item->fee == 0) {
- $result = AdVisitStatService::getInfoV2($this->uid, $item->cid, ['UNLOCK', 'UNLOCK_2']);
- $item->fee = '解锁';
- }
- }
- return response()->pagination(new ChapterOrderTransformer(), $chapter_order);
- }
- /**
- * @apiVersion 1.0.0
- * @apiDescription 充值记录
- * @api {get} order/chargeRecordLists 充值记录
- * @apiParam {String} [token] token
- * @apiHeader {String} [Authorization] token 两个token任选其一
- * @apiGroup Order
- * @apiName chargeRecordLists
- * @apiParam {String} [page_size] 分页大小
- * @apiParam {String} [page] 页码
- * @apiSuccess {int} code 状态码
- * @apiSuccess {String} msg 信息
- * @apiSuccess {object} data 结果集
- * @apiSuccess {String} data.price 价格
- * @apiSuccess {String} data.status 状态
- * @apiSuccess {String} data.trade_no 订单号
- * @apiSuccess {String} data.created_at 时间
- * @apiSuccessExample {json} Success-Response:
- * HTTP/1.1 200 OK
- * {
- * code: 0,
- * msg: "",
- * data: {
- * list: [
- * {
- * id: 134,
- * price: "1.00",
- * status: "PAID",
- * trade_no: "201712021915481585670623626232",
- * created_at: "2017-12-02 19:15:56"
- * }
- * ],
- * meta: {
- * total: 1,
- * per_page: 15,
- * current_page: 1,
- * last_page: 1,
- * next_page_url: "",
- * prev_page_url: ""
- * }
- * }
- * }
- */
- public function chargeRecordLists(Request $request)
- {
- $page_size = $request->input('page_size', 15);
- $res = OrderService::getOrderList($this->uid, $page_size);
- return response()->pagination(new ChargeListTransformer(), $res);
- }
- //订单是否成功
- public function isSuccess(Request $request)
- {
- $order = $request->input('order');
- $order_info = OrderService::getByTradeNo($order);
- if ($order_info && $order_info->status == 'PAID') {
- return response()->success();
- }
- return response()->error('QAPP_SYS_ERROR');
- }
- /**
- * @apiVersion 1.0.0
- * @apiDescription 支付
- * @api {get} goToPay 支付
- * @apiGroup pay
- * @apiName wxindex
- * @apiParam {Int} product_id product_id
- * @apiParam {Int} send_order_id send_order_id
- * @apiParam {String} bid bid
- * @apiHeader {String} [Authorization] token
- * @apiSuccess {int} code 状态码
- * @apiSuccess {String} msg 信息
- * @apiSuccess {Object} data 信息
- * @apiSuccess {Object} data.appId 唤起支付的appId
- * @apiSuccess {Object} data.mch_id 唤起支付的mch_id
- * @apiSuccess {Object} data.nonce_str 唤起支付的nonce_str
- * @apiSuccess {Object} data.prepay_id 唤起支付的prepay_id
- * @apiSuccess {Object} data.sign 唤起支付的sign
- * @apiSuccess {Object} data.trade_type 唤起支付trade_type
- * @apiSuccessExample {json} Success-Response:
- * HTTP/1.1 200 OK
- * {
- * code: 0,
- * msg: "",
- * data: {
- *
- * }
- */
- function wxindex(Request $request)
- {
- $product_id = $request->get('product_id', 0);
- $send_order_id = $request->get('send_order_id', 0);
- if (!$product_id) {
- return response()->error('QAPP_PARAM_ERROR');
- }
- $bid = $request->get('bid', 0);
- if ($bid) {
- $bid = BookService::decodeBidStatic($bid);
- }
- $trade_no = date("YmdHis") . hexdec(uniqid());
- $product_info = ProductService::getProductSingle($product_id);
- $uid = $this->uid;
- $distribution_channel_id = $this->distribution_channel_id;
- $price = $product_info->price * 100;
- if (in_array($uid, explode(',', env('TEST_UID')))) {
- $price = 1;
- }
- if ($product_info->type == 'YEAR_ORDER') {
- $order_type = 'YEAR';
- } elseif ($product_info->type == 'BOOK_ORDER') {
- $order_type = 'BOOK';
- } elseif ($product_info->type == 'TICKET_RECHARGE') {
- $order_type = 'RECHARGE';
- } else {
- $order_type = '';
- }
- $this->createUnPayOrder([
- 'distribution_channel_id' => $distribution_channel_id,
- 'uid' => $uid,
- 'product_id' => $product_id,
- 'price' => $price / 100,
- 'pay_type' => 1,
- 'trade_no' => $trade_no,
- 'pay_merchant_source' => 'QuickApp',
- 'pay_merchant_id' => 0,
- 'create_ip' => $request->getClientIp(),
- 'send_order_id' => $send_order_id,
- 'order_type' => $order_type,
- 'from_bid' => $bid,
- 'from_type' => 'QuickApp',
- 'activity_id' => 0
- ]);
- $config = [
- // 微信支付参数
- 'appid' => 'wxf065f7364b078a73', // 应用ID
- 'merchant_id' => '1500977641', // 微信支付商户号
- 'key' => '0e7SfPt3EOS0HC1GxVa4fqmCUINcN71E', // 微信支付密钥
- 'trade_type' => 'APP',
- ];
- $pay = WechatPay::Official('OFFICIALPAY', $config);
- try {
- $payOrder = [
- 'trade_no' => $trade_no, // 订单号
- 'price' => $price, // 订单金额,**单位:分**
- 'body' => '快应用 小说', // 订单描述
- 'create_ip' => _getIp(), // 支付人的 IP
- 'remark' => 'QuickApp'
- ];
- $result = $pay->send($payOrder);
- if ($result) {
- return response()->success($result);
- } else {
- Log::error("创建微信订单失败," . json_encode($result));
- return response()->error('APP_CREATE_WECHAT_ORDER_FAIL');
- }
- } catch (Exception $e) {
- Log::error("创建微信订单失败," . $e->getMessage());
- return response()->error('APP_CREATE_WECHAT_ORDER_FAIL');
- }
- }
- /**
- * @apiVersion 1.0.0
- * @apiDescription 订单查询
- * @api {get} checkOrder 订单查询
- * @apiGroup pay
- * @apiName checkOrder
- * @apiParam {String} [token] token
- * @apiHeader {String} [Authorization] token 两个token任选其一
- * @apiParam {String} order order
- * @apiSuccess {int} code 状态码
- * @apiSuccess {String} msg 信息
- * @apiSuccess {Object} data 信息
- * @apiSuccessExample {json} Success-Response:
- * HTTP/1.1 200 OK
- * {
- * code: 0,
- * msg: "",
- * data: {
- *
- * }
- */
- public function checkOrder(Request $request)
- {
- $order = $request->input('order', '');
- $i = 0;
- $uid = $this->uid;
- while ($i <= 10) {
- $order_info = OrderService::getByTradeNo($order);
- if (!$order_info) {
- return response()->error('QAPP_SYS_ERROR');
- break;
- }
- if (isset($order_info->status) && $order_info->status == 'PAID') {
- $data['balance'] = 0;
- $user = UserService::getById($uid);
- if ($user) {
- $data['balance'] = $user->balance;
- }
- return response()->success($data);
- }
- sleep(1);
- $i++;
- }
- return response()->error('QAPP_SYS_ERROR');
- }
- /**
- * 官方微信回调
- * @param Request $request
- * @return
- */
- function wxback(Request $request)
- {
- $options = [
- 'app_id' => 'wxf065f7364b078a73',
- 'payment' => [
- 'merchant_id' => 1500977641,
- 'key' => '0e7SfPt3EOS0HC1GxVa4fqmCUINcN71E',
- ]
- ];
- $app = new Application($options);
- $response = $app->payment->handleNotify(function ($notify, $successful) {
- if (!$successful) return 'fail';
- $trade_no = $notify->out_trade_no;
- $order = OrderService::getByTradeNo($trade_no);
- if (!$order) {
- return 'fail';
- }
- if (isset($order->status) && $order->status == 'PAID') {
- Log::info('has_pay:' . $trade_no);
- return true;
- }
- DB::beginTransaction();
- try {
- $transaction_id = $notify->transaction_id;
- $uid = $order->uid;
- $distribution_channel_id = $order->distribution_channel_id;
- $product_id = $order->product_id;
- $product = ProductService::getProductSingle($product_id);
- $send_order_id = 0;
- $price = $product->price;
- // 更新其他定制Order表
- if ($product->type == 'YEAR_ORDER') {
- $order_type = 'YEAR';
- $this->yearOrder($uid, $distribution_channel_id, $price, $send_order_id);
- $order->order_type = $order_type;
- $order->status = 'PAID';
- $order->pay_end_at = date('Y-m-d H:i:s');
- $order->transaction_id = $transaction_id;
- $order->save();
- } elseif ($product->type == 'BOOK_ORDER') {
- $order_type = 'BOOK';
- $this->bookOrder($product_id, $uid, $send_order_id, $price, $distribution_channel_id);
- $order->order_type = $order_type;
- $order->status = 'PAID';
- $order->pay_end_at = date('Y-m-d H:i:s');
- $order->transaction_id = $transaction_id;
- $order->save();
- } elseif ($product->type == 'TICKET_RECHARGE') {
- $order_type = 'RECHARGE';
- $this->userCharge($product, $uid);
- $order->order_type = $order_type;
- $order->status = 'PAID';
- $order->pay_end_at = date('Y-m-d H:i:s');
- $order->transaction_id = $transaction_id;
- $order->save();
- } else {
- DB::rollback();
- return 'Order not exist.';
- }
- DB::commit();
- return true;
- } catch (\Exception $e) {
- DB::rollback();
- return 'fail';
- }
- });
- return $response;
- }
- /**
- * 单本充值会掉
- * @param $product_id
- * @param $uid
- * @param $send_order_id
- * @param $fee
- */
- private function bookOrder($product_id, $uid, $send_order_id, $fee, $distribution_channel_id)
- {
- $book_conf = BookConfigService::getBookByProduct($product_id);
- $insert_data['bid'] = isset($book_conf->bid) ? $book_conf->bid : '';
- $insert_data['book_name'] = isset($book_conf->book_name) ? $book_conf->book_name : '';
- $insert_data['uid'] = $uid;
- $insert_data['distribution_channel_id'] = $distribution_channel_id;
- $insert_data['fee'] = $fee;
- $insert_data['send_order_id'] = $send_order_id;
- $insert_data['charge_balance'] = 0;
- $insert_data['reward_balance'] = 0;
- return BookOrderService::save_book_order($insert_data);
- }
- /**
- * 包年
- * @param $uid
- * @param $distribution_channel_id
- * @param $fee
- * @param $send_order_id
- * @return mixed
- */
- private function yearOrder($uid, $distribution_channel_id, $fee, $send_order_id)
- {
- $insert_data['uid'] = $uid;
- $insert_data['distribution_channel_id'] = $distribution_channel_id;
- $insert_data['fee'] = $fee;
- $insert_data['send_order_id'] = $send_order_id;
- return YearOrderService::save_year_order($insert_data);
- }
- /**
- * 用户充值
- * @param $product
- * @param $uid\
- */
- private function userCharge($product, $uid)
- {
- $total = $product->price * 100 + $product->given;
- UserService::addBalance($uid, $total, $product->price * 100, $product->given);
- }
- /**
- * 添加位置付订单
- * @param $data
- * @return mixed
- */
- private function createUnPayOrder($data)
- {
- $data['status'] = 'UNPAID';
- $data['transaction_id'] = '';
- $data['pay_end_at'] = '0000-00-00 00:00:00';
- return OrderService::save_order($data);
- }
- }
|