123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487 |
- <?php
- /**
- * PtbAgent.php UTF-8
- *
- *
- * @date : 2018/5/18 21:39
- *
- * @license 这不是一个自由软件,未经授权不许任何使用和传播。
- * @author : wuyonghong <wyh@huosdk.com>
- * @version : HUOSDK 8.0
- */
- namespace huo\controller\wallet;
- use huo\controller\agent\AgentCache;
- use huo\controller\agent\AgentWallet;
- use huo\controller\common\Base;
- use huo\controller\common\CommonFunc;
- use huo\controller\common\HuoSession;
- use huo\controller\finance\Income;
- use huo\controller\member\Member;
- use huo\controller\member\MemWallet;
- use huo\controller\rate\PtbRate;
- use huo\controller\rate\Rate;
- use huo\model\finance\PtbAgentChargeModel;
- use huo\model\finance\PtbAgentOrderModel;
- use huo\model\finance\PtbBackModel;
- use huolib\constant\OrderConst;
- use huolib\constant\PaywayConst;
- use huolib\constant\SettleConst;
- use huolib\constant\WalletConst;
- use huolib\pay\Pay;
- use huolib\status\CommonStatus;
- use huolib\status\MemberStatus;
- use huolib\status\OrderStatus;
- use huolib\status\SettleStatus;
- use huolib\tool\StrUtils;
- use huolib\utils\OrderUtils;
- use think\Exception;
- class PtbAgent extends Base {
- /**
- * 给玩家代充游戏币
- *
- * @param int $agent_id
- * @param int $game_id
- * @param string $username
- * @param float $amount
- *
- * @return array
- */
- public function chargeMemGm($agent_id, $game_id, $username, $amount) {
- $_mem_id = (new Member())->getIdByName($username);
- if (empty($_mem_id)) {
- $_code = MemberStatus::UID_NOT_EXISTS;
- return $this->huoError($_code, MemberStatus::getMsg($_code));
- }
- $_wr_class = new WalletRequest();
- $_order_id = StrUtils::genOrderId($agent_id, $agent_id, $_mem_id, WalletConst::WALLET_ORDER_PREFIX_P2GM);
- $_wr_class->setOrderId($_order_id);
- $_wr_class->setAppId($game_id);
- $_wr_class->setMemId($_mem_id);
- $_wr_class->setAgentId($agent_id);
- /* 计算玩家可以获得的游戏币 */
- $_gm_cnt = $amount;
- $_wr_class->setGmCnt($_gm_cnt);
- $_wr_class->setType(WalletConst::WALLET_FROM_PROXY);
- $_wr_class->setStatus(OrderConst::PAY_STATUS_SUC);
- /*计算等值的人民币*/
- $_ptb_rmb_rate = CommonFunc::getPtbRmbRate();
- $_wr_class->setAmount(StrUtils::formatNumber($amount / $_ptb_rmb_rate));
- /*计算渠道消耗的平台币*/
- $_cost_ptb_cnt = $this->getCostPtbCnt($game_id, $agent_id, $amount);
- $_wr_class->setCostPtbCnt($_cost_ptb_cnt);
- $_wr_class->setRealAmount(StrUtils::formatNumber($_cost_ptb_cnt / $_ptb_rmb_rate));
- /* 渠道平台币消耗 */
- $_rs = $this->createPaoOrder($_wr_class);
- if (SettleStatus::NO_ERROR != $_rs) {
- return $this->huoError($_rs, SettleStatus::getMsg($_rs));
- }
- /* 玩家游戏币收入 */
- $_rs = (new MemWallet())->createGmcOrder($_wr_class);
- if (SettleStatus::NO_ERROR != $_rs) {
- // TODO: wuyonghong 2018/5/18 需要回退处理渠道平台币
- return $this->huoError($_rs, SettleStatus::getMsg($_rs));
- }
- /*父渠道收入*/
- (new Income())->incomeFroGmc($_wr_class->getOrderId());
- $_code = CommonStatus::NO_ERROR;
- return $this->huoError($_code, CommonStatus::getMsg($_code));
- }
- /**
- * 给玩家代充平台币
- *
- * @param int $agent_id
- * @param string $username
- * @param float $amount
- *
- * @return array
- */
- public function chargeMemPtb($agent_id, $username, $amount) {
- $_mem_id = (new Member())->getIdByName($username);
- if (empty($_mem_id)) {
- $_code = MemberStatus::UID_NOT_EXISTS;
- return $this->huoError($_code, MemberStatus::getMsg($_code));
- }
- $_wr_class = new WalletRequest();
- $_order_id = StrUtils::genOrderId($agent_id, $agent_id, $_mem_id, WalletConst::WALLET_ORDER_PREFIX_P2M);
- $_wr_class->setOrderId($_order_id);
- $_wr_class->setCostPtbCnt($amount);
- $_wr_class->setMemId($_mem_id);
- $_wr_class->setAgentId($agent_id);
- $_ptb_rmb_rate = CommonFunc::getPtbRmbRate();
- $_ptb_rate = (new PtbRate($agent_id))->getAgentRate();
- $_wr_class->setAmount(StrUtils::formatNumber($amount / $_ptb_rmb_rate * $_ptb_rate));
- $_wr_class->setRealAmount(0);
- /* 计算玩家可以获得的平台币 */
- $_ptb_cnt = $amount;
- $_wr_class->setPtbCnt($_ptb_cnt);
- $_wr_class->setType(WalletConst::WALLET_FROM_GIVE);
- $_wr_class->setStatus(OrderConst::PAY_STATUS_SUC);
- /* 渠道平台币消耗 */
- $_rs = $this->createPaoOrder($_wr_class);
- if (SettleStatus::NO_ERROR != $_rs) {
- return $this->huoError($_rs, SettleStatus::getMsg($_rs));
- }
- /* 玩家平台币收入 */
- $_rs = (new MemWallet())->createPcOrder($_wr_class);
- if (SettleStatus::NO_ERROR != $_rs) {
- // TODO: wuyonghong 2018/5/18 需要回退处理渠道平台币
- return $this->huoError($_rs, SettleStatus::getMsg($_rs));
- }
- $_code = CommonStatus::NO_ERROR;
- return $this->huoError($_code, CommonStatus::getMsg($_code));
- }
- /**
- * 代理平台币消耗
- *
- * @param WalletRequest $wc
- *
- * @return int
- */
- public function createPaoOrder(WalletRequest $wc) {
- /* 扣除渠道平台币 */
- $_rs = (new AgentWallet())->updatePtb(
- $wc->getAgentId(), $wc->getCostPtbCnt(), SettleConst::SETTLE_WALLET_DEDUCT
- );
- if (SettleStatus::NO_ERROR != $_rs) {
- return $_rs;
- }
- /* 插入渠道消费订单 */
- $_pao_data = $wc->getPtbAgentOrderData();
- $_rs = (new PtbAgentOrderModel())->createOrder($_pao_data);
- if (false == $_rs) {
- return SettleStatus::ORDER_CREATE_FAIL;
- }
- return SettleStatus::NO_ERROR;
- }
- /**
- * 代理平台币收入
- *
- * @param WalletRequest $wc
- *
- * @return int
- */
- public function createPacOrder(WalletRequest $wc) {
- if (OrderConst::PAY_STATUS_SUC == $wc->getStatus()) {
- /* 增加渠道平台币 */
- $_rs = (new AgentWallet())->updatePtb(
- $wc->getToAgentId(), $wc->getPtbCnt(), SettleConst::SETTLE_WALLET_ADD
- );
- if (SettleStatus::NO_ERROR != $_rs) {
- return $_rs;
- }
- }
- /* 插入渠道平台币收入订单 */
- $_pac_data = $wc->getPtbAgentChargeData();
- $_rs = (new PtbAgentChargeModel())->createOrder($_pac_data);
- if (false == $_rs) {
- return SettleStatus::ORDER_CREATE_FAIL;
- }
- return SettleStatus::NO_ERROR;
- }
- /**
- * 官方给渠道发放平台币
- *
- * @param int $cur_admin_id 当前登录的管理员id
- * @param $agent_id
- * @param $ptb_cnt
- * @param $remark
- *
- * @return array
- */
- public function gainAgentPtb($cur_admin_id, $agent_id, $ptb_cnt, $remark) {
- $_ac_class = AgentCache::ins();
- $_agent_data = $_ac_class->getInfoByAgentId($agent_id);
- $_ae_data = $_ac_class->getAgentExtByAgentId($agent_id);
- if (empty($_agent_data) || empty($_ae_data)) {
- $_code = MemberStatus::UID_INVALID;
- return $this->huoError($_code, MemberStatus::getMsg($_code));
- }
- $_wr_class = new WalletRequest();
- $_order_id = StrUtils::genOrderId($agent_id, $agent_id, 0, WalletConst::WALLET_ORDER_PREFIX_O2A);
- $_wr_class->setAgentId($cur_admin_id);
- $_wr_class->setToAgentId($agent_id);
- $_wr_class->setOrderId($_order_id);
- $_wr_class->setType(WalletConst::WALLET_FROM_GIVE);
- $_wr_class->setPtbCnt($ptb_cnt);
- $_wr_class->setStatus(OrderConst::PAY_STATUS_SUC);
- $_wr_class->setRemark($remark);
- $_rs = $this->createPacOrder($_wr_class);
- if (SettleStatus::NO_ERROR != $_rs) {
- return $this->huoError($_rs, SettleStatus::getMsg($_rs));
- }
- $_code = CommonStatus::NO_ERROR;
- return $this->huoError($_code, CommonStatus::getMsg($_code));
- }
- /**
- * 官方扣回渠道平台币
- *
- * @param int $cur_admin_id 当前登录的管理员id
- * @param $agent_id
- * @param $order_ptb_cnt
- * @param $ptb_cnt
- * @param $remark
- * @param null $order_id
- *
- * @return array
- */
- public function deductAgentPtb($cur_admin_id, $agent_id, $order_ptb_cnt, $ptb_cnt, $remark, $order_id = null) {
- $_ac_class = AgentCache::ins();
- $_agent_data = $_ac_class->getInfoByAgentId($agent_id);
- $_ae_data = $_ac_class->getAgentExtByAgentId($agent_id);
- if (empty($_agent_data) || empty($_ae_data)) {
- $_code = MemberStatus::UID_INVALID;
- return $this->huoError($_code, MemberStatus::getMsg($_code));
- }
- $_wr_class = new WalletRequest();
- $_wr_class->setAgentId($cur_admin_id);
- $_wr_class->setToAgentId($agent_id);
- $_wr_class->setPtbCnt($order_ptb_cnt);
- $_wr_class->setBackPtbCnt($ptb_cnt);
- $_wr_class->setStatus(OrderConst::PAY_STATUS_SUC);
- $_wr_class->setRemark($remark);
- if (!is_null($order_id)) {
- $_wr_class->setOrderId($order_id);
- }
- $_rs = $this->createPadOrder($_wr_class);
- if (SettleStatus::NO_ERROR != $_rs) {
- return $this->huoError($_rs, SettleStatus::getMsg($_rs));
- }
- $_code = CommonStatus::NO_ERROR;
- return $this->huoError($_code, CommonStatus::getMsg($_code));
- }
- /**
- * 代理平台币扣除
- *
- * @param WalletRequest $wc
- *
- * @return array|bool|int
- */
- private function createPadOrder(WalletRequest $wc) {
- if (OrderConst::PAY_STATUS_SUC == $wc->getStatus()) {
- /* 扣除渠道平台币 */
- $_rs = (new AgentWallet())->updatePtb(
- $wc->getToAgentId(), $wc->getBackPtbCnt(), SettleConst::SETTLE_WALLET_DEDUCT
- );
- if (SettleStatus::NO_ERROR != $_rs) {
- return $_rs;
- }
- }
- /* 插入扣除订单 */
- $_deduct_data = $wc->getPtbAgentDeductData();
- $_rs = (new PtbBackModel())->createOrder($_deduct_data);
- if (false == $_rs) {
- return SettleStatus::ORDER_CREATE_FAIL;
- }
- return SettleStatus::NO_ERROR;
- }
- /**
- * 给下级充值平台币
- *
- * @param int $agent_id
- * @param int $sub_agent_id
- * @param float $amount
- *
- * @return array
- */
- public function chargeSubAgentPtb($agent_id, $sub_agent_id, $amount) {
- if (empty($sub_agent_id)) {
- $_code = MemberStatus::UID_NOT_EXISTS;
- return $this->huoError($_code, MemberStatus::getMsg($_code));
- }
- $_wr_class = new WalletRequest();
- $_order_id = StrUtils::genOrderId($agent_id, $sub_agent_id, 0, WalletConst::WALLET_ORDER_PREFIX_P2A);
- $_wr_class->setOrderId($_order_id);
- $_wr_class->setCostPtbCnt($amount);
- $_wr_class->setToAgentId($sub_agent_id);
- $_wr_class->setAgentId($agent_id);
- $_ptb_rmb_rate = CommonFunc::getPtbRmbRate();
- $_ptb_rate = (new PtbRate($sub_agent_id))->getAgentRate();
- $_wr_class->setAmount(StrUtils::formatNumber($amount / $_ptb_rmb_rate * $_ptb_rate));
- $_wr_class->setRealAmount(0);
- /* 计算下级渠道获得的平台币 */
- $_ptb_cnt = $_wr_class->getCostPtbCnt();
- $_wr_class->setPtbCnt($_ptb_cnt);
- $_wr_class->setType(WalletConst::WALLET_FROM_EXCHANGE);
- $_wr_class->setStatus(OrderConst::PAY_STATUS_SUC);
- /* 渠道平台币消耗 */
- $_rs = $this->createPaoOrder($_wr_class);
- if (SettleStatus::NO_ERROR != $_rs) {
- return $this->huoError($_rs, SettleStatus::getMsg($_rs));
- }
- /* 玩家平台币收入 */
- $_rs = $this->createPacOrder($_wr_class);
- if (SettleStatus::NO_ERROR != $_rs) {
- // TODO: wuyonghong 2018/5/18 需要回退处理渠道平台币
- return $this->huoError($_rs, SettleStatus::getMsg($_rs));
- }
- $_code = CommonStatus::NO_ERROR;
- return $this->huoError($_code, CommonStatus::getMsg($_code));
- }
- /**
- * 预下单
- *
- * @param $agent_id
- * @param $amount
- * @param $type
- * @param $payway
- *
- * @return array|bool|string
- */
- public function preorder($agent_id, $amount, $type, $payway) {
- if (empty($agent_id)) {
- $_code = MemberStatus::LOGIN_IS_OUT;
- return $this->huoError($_code, MemberStatus::getMsg($_code));
- }
- $_chk_rs = OrderUtils::checkAmount($amount);
- if (OrderStatus::NO_ERROR != $_chk_rs) {
- return $this->huoError($_chk_rs, OrderStatus::getMsg($_chk_rs));
- }
- $_ac_class = AgentCache::ins();
- $_agent_data = $_ac_class->getInfoByAgentId($agent_id);
- $_ae_data = $_ac_class->getAgentExtByAgentId($agent_id);
- if (empty($_agent_data) || empty($_ae_data)) {
- $_code = MemberStatus::UID_INVALID;
- return $this->huoError($_code, MemberStatus::getMsg($_code));
- }
- $_wr_class = new WalletRequest();
- $_order_id = StrUtils::genOrderId($agent_id, $agent_id, $agent_id, WalletConst::WALLET_ORDER_PREFIX_PA);
- $_wr_class->setOrderId($_order_id);
- $_wr_class->setToAgentId($agent_id);
- $_wr_class->setType($type);
- $_wr_class->setPayway($payway);
- $_wr_class->setRealAmount($amount);
- /* 平台币只能充值整数 向下取整 */
- $_ptb_rmb_rate = CommonFunc::getPtbRmbRate();
- $_ptb_rate = (new PtbRate($agent_id))->getAgentRate();
- $_wr_class->setDiscount($_ptb_rate);
- $_wr_class->setAmount(StrUtils::formatNumber($amount / $_ptb_rate));
- $_wr_class->setPtbCnt($_wr_class->getAmount() * $_ptb_rmb_rate);
- $_rs = $this->createPacOrder($_wr_class);
- if (SettleStatus::NO_ERROR != $_rs) {
- return $this->huoError($_rs, SettleStatus::getMsg($_rs));
- }
- if (PaywayConst::PAYWAY_BALANCE == $payway) {
- /* 余额充值特殊流程 */
- /* 扣除渠道钱包 */
- $_aw_class = new AgentWallet();
- $_rs = $_aw_class->updateWallet($agent_id, $amount, SettleConst::SETTLE_WALLET_DEDUCT);
- if (SettleStatus::NO_ERROR != $_rs) {
- return $this->huoError($_rs, SettleStatus::getMsg($_rs));
- }
- /* 调用支付通知 */
- $_rs = (new Notify())->payNotifyAgentPtbCharge(
- $_order_id, $_order_id, $amount, PaywayConst::PAYWAY_BALANCE
- );
- if (false == $_rs) {
- $_code = OrderStatus::INNER_ERROR;
- return $this->huoSuccess($_code, OrderStatus::getMsg($_code));
- }
- $_code = OrderStatus::NO_ERROR;
- return $this->huoSuccess($_code, OrderStatus::getMsg($_code));
- }
- try {
- $_pay_class = Pay::ins()->get($payway);
- $_func = 'pcPay';
- $_show_url = url('agent/'.$payway.'/showUrl', ['order_id' => $_order_id], false, AGENTSITE);
- $_return_url = url(
- 'agent/'.$payway.'/return', ['order_id' => $_order_id], false, AGENTSITE
- );
- $_pay_class->setReturnUrl($_return_url);
- $_pay_class->setShowUrl($_show_url);
- $_pay_class->setOrderId($_order_id);
- $_product_name = config('CURRENCY_NAME').'充值'.$amount;
- $_pay_class->setProductName($_product_name);
- $_pay_class->setProductDesc($_product_name);
- $_pay_class->setRealAmount($amount);
- $_pay_class->setProductId(WalletConst::WALLET_PRODUCT_AGENT_PTB);
- $_pay_class->setIp(request()->ip());
- $_pay_class->setOpenId((new HuoSession($agent_id))->getOpenId());
- $_pay_class->setMemId($agent_id);
- // TODO: liuhongliang 2018/6/7 特殊字符是否能够支持盛付通支付?
- $_pay_class->setMemRealName($_agent_data['user_nicename']);
- $_pay_class->setMemRegTime($_agent_data['create_time']);
- $_pay_class->setMemMobile($_agent_data['mobile']);
- $_pay_rs = $_pay_class->$_func();
- if (empty($_pay_rs)) {
- $_code = OrderStatus::PAYWAY_PREORDER_ERROR;
- return $this->huoError($_code, SettleStatus::getMsg($_code));
- }
- $_code = OrderStatus::NO_ERROR;
- return $this->huoSuccess($_code, OrderStatus::getMsg($_code), $_pay_rs);
- } catch (Exception $e) {
- $_code = OrderStatus::PAYWAY_NOT_EXISTS;
- return $this->huoError($_code, OrderStatus::getMsg($_code));
- }
- }
- /**
- * @param $order_id
- *
- * @return bool
- */
- public function getStatus($order_id = '') {
- if (empty($order_id)) {
- return false;
- }
- $_o_model = new PtbAgentChargeModel();
- $_rs = $_o_model->getStatus($order_id);
- if (false == $_rs) {
- return false;
- }
- return $_rs;
- }
- /***
- * 计算实际花费平台币
- *
- * @param $game_id //游戏id
- * @param $agent_id //渠道id
- * @param $amount //游戏币
- *
- * @return float|int
- */
- public function getCostPtbCnt($game_id, $agent_id, $amount) {
- /*游戏币转换人民币*/
- $_gm_rmb_rate = CommonFunc::getGmRmbRate();
- $_money = $amount / $_gm_rmb_rate; //等值金额
- $_ptb_rmb_rate = CommonFunc::getPtbRmbRate();
- /*人民币转平台币*/
- $_ptb = $_money * $_ptb_rmb_rate;
- $_prate_class = new Rate($game_id, $agent_id);
- $_return = $_ptb * $_prate_class->getAgentRate();
- return $_return;
- }
- }
|