123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368 |
- <?php
- /**
- * Order.php UTF-8
- *
- *
- * @date : 2018/1/19 21:17
- *
- * @license 这不是一个自由软件,未经授权不许任何使用和传播。
- * @author : wuyonghong <wyh@huosdk.com>
- * @version : HUOSDK 8.0
- */
- namespace huo\controller\pay;
- use huo\controller\agent\AgentCache;
- use huo\controller\common\Base;
- use huo\controller\common\CommonFunc;
- use huo\controller\member\MemCache;
- use huo\controller\rate\Rate;
- use huo\controller\request\Channel;
- use huo\controller\request\Device;
- use huo\controller\request\Game as GameRq;
- use huo\controller\request\Mem;
- use huo\controller\request\Order;
- use huo\controller\request\Order as OrderRq;
- use huo\controller\request\Role;
- use huo\controller\wallet\GmCache;
- use huo\model\order\OrderExtModel;
- use huo\model\order\OrderModel;
- use huoAgentSwitch\controller\AgentSwitchController;
- use huolib\constant\CommonConst;
- use huolib\constant\OrderConst;
- use huolib\status\DeviceStatus;
- use huolib\status\GameStatus;
- use huolib\status\MemberStatus;
- use huolib\status\OrderStatus;
- use huolib\status\RoleStatus;
- use huolib\tool\Rand;
- use huolib\tool\StrUtils;
- use huolib\utils\DeviceUtils;
- use huolib\utils\GameUtils;
- use huolib\utils\OrderUtils;
- use huolib\utils\RoleUtils;
- use think\Log;
- class Sdk extends Base {
- protected function retSucMsg($code, $data = []) {
- $_msg = OrderStatus::getMsg($code);
- return $this->huoSuccess($code, $_msg, $data);
- }
- protected function retErrMsg($code) {
- $_err_msg = OrderStatus::getMsg($code);
- return $this->huoError($code, $_err_msg);
- }
- /**
- * @param OrderRq $order
- * @param Role $role
- * @param Mem $mem
- * @param GameRq $game_rq
- * @param Channel $channel
- * @param Device $device
- *
- * @return array|bool
- */
- public function checkParam(
- OrderRq $order, Role $role, Mem $mem,
- GameRq $game_rq, Channel $channel,
- Device $device
- ) {
- // TODO: wuyonghong 2018/5/24 校验参数未完成
- $_chk_order_rs = OrderUtils::checkSdkOrder($order->toArray());
- if (OrderStatus::NO_ERROR != $_chk_order_rs) {
- return $this->retErrMsg($_chk_order_rs);
- }
- $_chk_role_rs = RoleUtils::checkRole($role->toArray());
- if (RoleStatus::NO_ERROR != $_chk_role_rs) {
- return $this->huoError($_chk_role_rs, RoleStatus::getMsg($_chk_role_rs));
- }
- $_chk_device_rs = DeviceUtils::checkDevice($device->toArray());
- if (DeviceStatus::NO_ERROR != $_chk_device_rs) {
- return $this->huoError($_chk_device_rs, DeviceStatus::getMsg($_chk_device_rs));
- }
- $_chk_game_rs = GameUtils::checkGame($game_rq->toArray());
- if (GameStatus::NO_ERROR != $_chk_game_rs) {
- return $this->huoError($_chk_game_rs, GameStatus::getMsg($_chk_game_rs));
- }
- $_code = OrderStatus::NO_ERROR;
- return $this->retErrMsg($_code);
- }
- /**
- * 预下单
- *
- * @param OrderRq $order
- * @param Role $role
- * @param Mem $mem
- * @param GameRq $game_rq
- * @param Channel $channel
- * @param Device $device
- *
- * @return array|bool|string
- */
- public function preorder(OrderRq $order, Role $role, Mem $mem, GameRq $game_rq, Channel $channel, Device $device) {
- $_check_rs = $this->checkParam($order, $role, $mem, $game_rq, $channel, $device);
- if (OrderStatus::NO_ERROR != $_check_rs['code']) {
- return $_check_rs;
- }
- $_order_id = StrUtils::genOrderId($mem->getAgentId(), $channel->getAgentId(), $mem->getMemId());
- $order->setOrderId($_order_id);
- $_rdata = $this->buildOrder($order, $role, $mem, $game_rq, $device);
- if (empty($_rdata)) {
- $_err_msg = 'func='.__FUNCTION__.'&class='.__CLASS__.'&step=2&game'.json_encode($game_rq->toArray())
- .'&channel'
- .json_encode($channel->toArray()).'&device'.json_encode($device->toArray()).'&member'
- .json_encode($mem->toArray());
- Log::write($_err_msg, Log::ERROR);
- return $this->retErrMsg(OrderStatus::ORDER_CREATE_FAIL);
- }
- // TODO: wuyonghong 2018/4/27 订单扩展处理
- $_rdata['order_id'] = $_order_id;
- /* 异步处理数据 */
- (new \huo\controller\queue\Order($game_rq->getHAppId()))->create(
- $device, $game_rq, $channel, $mem, $role, $order
- );
- return $this->retSucMsg(OrderStatus::NO_ERROR, $_rdata);
- }
- /**
- * @param OrderRq $order
- * @param Role $role
- * @param Mem $mem
- * @param GameRq $game_rq
- *
- * @param Device $device
- *
- * @return bool|mixed
- */
- public function buildOrder(OrderRq $order, Role $role, Mem $mem, GameRq $game_rq, Device $device) {
- /* 订单主表 */
- $_rate_class = new Rate($game_rq->getHAppId(), $mem->getAgentId());
- $order->setProductRealPrice(
- StrUtils::formatNumber($order->getProductPrice() * $_rate_class->getMemRate($mem->getMemId()))
- );
- $order->setRebateAmount(
- StrUtils::formatNumber($order->getProductPrice() * $_rate_class->getMemRebate($mem->getMemId()))
- );
- $_data = $order->toArray();
- $_data['mem_id'] = $mem->getMemId();
- $_data['mg_mem_id'] = $mem->getMgMemId();
- $_data['agent_id'] = $mem->getAgentId();
- $_data['app_id'] = $game_rq->getHAppId();
- $_data['vb_id'] = $game_rq->getVbId();
- $_data['rate'] = $_rate_class->getMemRate($mem->getMemId());
- /* 切量处理 */
- $_data['ip'] = $device->getIp();
- $_data = (new AgentSwitchController())->orderSwitch($_data);
- $_order_model = new OrderModel();
- $_pay_data = $_order_model->createOrder($_data);
- if (false == $_pay_data) {
- return false;
- }
- /* 2 订单扩展表 */
- $_data['pay_id'] = $_pay_data['id'];
- $_role_data = $role->toArray();
- $_device_data = $device->toArray();
- $_oe_data = array_merge($_data, $_role_data, $_device_data);
- $_oe_rs = (new OrderExtModel())->addOeLog($_oe_data);
- if (false == $_oe_rs) {
- return false;
- }
- $_pay_data['ext'] = $_oe_rs;
- $_rdata = SdkOrderCache::ins()->getInfoByOrderId($order->getOrderId());
- return $_rdata;
- }
- /**
- * @param $order_id
- *
- * @return bool
- */
- public function getStatus($order_id = '') {
- if (empty($order_id)) {
- return false;
- }
- $_order_data = SdkOrderCache::ins()->getInfoByOrderId($order_id);
- if (empty($_order_data)) {
- return false;
- }
- $_rdata['status'] = $_order_data['status'];
- $_rdata['cp_status'] = $_order_data['cp_status'];
- $_rdata['product_name'] = $_order_data['product_name'];
- $_rdata['product_id'] = $_order_data['product_id'];
- $_rdata['real_amount'] = $_order_data['real_amount'];
- $_rdata['product_cnt'] = $_order_data['product_cnt'];
- $_rdata['payway'] = $_order_data['payway'];
- return $_rdata;
- }
- /**
- * @param $order_id
- *
- * @param bool $is_client
- *
- * @return array|mixed
- */
- public function getPayInfo($order_id, $is_client = false) {
- $_order_id = $order_id;
- if (empty($order_id)) {
- return $this->retErrMsg(OrderStatus::ORDER_ID_EMPTY);
- }
- $_order_data = SdkOrderCache::ins()->getInfoByOrderId($_order_id);
- if (empty($_order_data)) {
- return $this->retErrMsg(OrderStatus::ORDER_NOT_EXISTS);
- }
- $_me_data = MemCache::ins()->getMeInfoById($_order_data['mem_id']);
- if (empty($_me_data)) {
- $_code = MemberStatus::UID_NOT_EXISTS;
- return $this->huoError($_code, MemberStatus::getMsg($_code));
- }
- /* 获取玩家游戏币余额 */
- $_gmc_class = GmCache::ins();
- $_gm_cnt = $_gmc_class->getRemainByMemGame($_order_data['mem_id'], $_order_data['app_id']);
- if (empty($_gm_cnt)) {
- $_gm_cnt = 0;
- }
- if ($is_client) {
- $_rdata['pay_token'] = (new order())->genPayToken($_order_id);
- $_rdata['order_id'] = $_order_data['order_id'];
- $_rdata['product_name'] = $_order_data['product_name'];
- $_rdata['product_price'] = $_order_data['amount'];
- $_rdata['real_amount'] = $_order_data['real_amount'];
- $_rdata['ptb_cnt'] = $_me_data['ptb_cnt'];
- $_rdata['gm_cnt'] = $_gm_cnt;
- $_rdata['ptb_rate'] = CommonFunc::getPtbRmbRate();
- $_rdata['gm_rate'] = CommonFunc::getGmRmbRate();
- $_rate_class = new Rate($_order_data['app_id'], $_order_data['agent_id']);
- $_rdata['benefit_type'] = $_rate_class->getBenefitType();
- $_rdata['mem_rate'] = $_order_data['rate'];
- $_rdata['rebate_cnt'] = $_order_data['rebate_amount'];
- $_rdata['pay_type'] = $this->getClientPayWays(
- $_order_data['app_id'], $_rdata['product_price'], $_order_data['rate'],
- $_order_data['rebate_amount'], $_rdata['gm_cnt']
- );
- } else {
- $_rdata['order_id'] = $_order_data['order_id'];
- $_rdata['product_name'] = $_order_data['product_name'];
- $_rdata['product_price'] = $_order_data['amount'];
- //$_rdata['product_cnt'] = $_order_data['product_cnt']; //屏蔽数量不显示 2018/11/17 运营需求
- $_rdata['product_cnt'] = '';
- $_rdata['real_amount'] = $_order_data['real_amount'];
- $_rdata['ptb_cnt'] = $_me_data['ptb_cnt'];
- $_rdata['gm_cnt'] = isset($_me_data['gm_cnt']) ? $_me_data['gm_cnt'] : 0;
- $_rdata['ptb_rate'] = CommonFunc::getPtbRmbRate();
- $_rdata['gm_rate'] = CommonFunc::getGmRmbRate();
- $_rate_class = new Rate($_order_data['app_id'], $_order_data['agent_id']);
- $_rdata['benefit_type'] = $_rate_class->getBenefitType();
- $_rdata['mem_rate'] = $_order_data['rate'];
- $_rdata['rebate_cnt'] = $_order_data['rebate_amount'];
- $_rdata['pay_type'] = $this->getPayWays(
- $_order_data['app_id'], $_rdata['product_price'], $_rdata['ptb_cnt']
- );
- }
- return $this->retSucMsg(OrderStatus::NO_ERROR, $_rdata);
- }
- /**
- * @param string $app_id
- *
- * @param int $amount
- * @param int $ptb_cnt
- *
- * @return array
- */
- public function getPayWays($app_id, $amount = 0, $ptb_cnt = 0) {
- // TODO: wuyonghong 2018/4/28 从数据库读取
- if (empty($app_id)) {
- /* 获取支付方式 */
- $_rdata[0]['payway'] = 'alipay';
- $_rdata[0]['name'] = '支付宝';
- $_rdata[0]['icon'] = STATICSITE.'/h5sdk/images/arwec.png';
- $_rdata[0]['url'] = '支付宝';
- $_rdata[1]['payway'] = 'wxpay';
- $_rdata[1]['name'] = '微信';
- $_rdata[1]['icon'] = STATICSITE.'/h5sdk/images/wergcder.png';
- $_rdata[1]['url'] = '微信';
- }
- /* 获取支付方式 */
- $_rdata[0]['payway'] = 'alipay';
- $_rdata[0]['name'] = '支付宝';
- $_rdata[0]['icon'] = STATICSITE.'/h5sdk/images/arwec.png';
- $_rdata[0]['url'] = '支付宝';
- $_rdata[1]['payway'] = 'wxpay';
- $_rdata[1]['name'] = '微信';
- $_rdata[1]['icon'] = STATICSITE.'/h5sdk/images/wergcder.png';
- $_rdata[1]['url'] = '微信';
- // $_rdata[2]['payway'] = 'shengpay';
- // $_rdata[2]['name'] = '银行卡';
- // $_rdata[2]['icon'] = STATICSITE.'/h5sdk/images/upcash.png';
- // $_rdata[2]['url'] = '银行卡';
- if ($amount <= $ptb_cnt) {
- $_rdata[2]['payway'] = 'ptbpay';
- $_rdata[2]['name'] = config('CURRENCY_NAME');
- $_rdata[2]['icon'] = STATICSITE.'/h5sdk/images/ptbpay.png';
- $_rdata[2]['url'] = config('CURRENCY_NAME');
- }
- return $_rdata;
- }
- /**
- * @param string $app_id
- *
- * @param int $amount 支付金额
- * @param float $rate 折扣
- * @param float $rebate_cnt 返利
- * @param int $gm_cnt 游戏币数量
- *
- * @return array
- */
- public function getClientPayWays($app_id, $amount = 0, $rate = 1.0, $rebate_cnt = 0.0, $gm_cnt = 0) {
- // TODO: wuyonghong 2018/4/28 从数据库读取
- /* 获取支付方式 */
- $_rdata[0]['payway'] = 'alipay';
- $_rdata[0]['name'] = '支付宝';
- $_rdata[0]['mem_rate'] = $rate;
- $_rdata[0]['rebate_cnt'] = $rebate_cnt;
- $_rdata[0]['icon'] = 'http:'.STATICSITE.'/h5sdk/images/huosdk_uc_zfb.png';
- $_rdata[0]['url'] = '支付宝';
- $_rdata[1]['payway'] = 'wxpay';
- $_rdata[1]['name'] = '微信';
- $_rdata[1]['icon'] = 'http:'.STATICSITE.'/h5sdk/images/huosdk_uc_wxzf.png';
- $_rdata[1]['url'] = '微信';
- $_rdata[1]['mem_rate'] = $rate;
- $_rdata[1]['rebate_cnt'] = $rebate_cnt;
- $_rdata[2]['payway'] = 'gamepay';
- $_rdata[2]['name'] = config('GM_CURRENCY_NAME');
- if ($gm_cnt > 0) {
- $_rdata[2]['icon'] = 'http:'.STATICSITE.'/h5sdk/images/huosdk_uc_youxibi.png';
- } else {
- $_rdata[2]['icon'] = 'http:'.STATICSITE.'/h5sdk/images/huosdk_uc_youxibi_disable.png';
- }
- $_rdata[2]['url'] = config('GM_CURRENCY_NAME');
- $_rdata[2]['mem_rate'] = 1;
- $_rdata[2]['rebate_cnt'] = 0;
- return $_rdata;
- }
- /**
- * 查询微信订单
- *
- * @param $order_id
- */
- public function queryWxOrder($order_id) {
- // TODO: wuyonghong 2018/5/7 查询订单完成情况
- }
- }
|