Cash.php 4.13 KB
<?php

/**
 * 提现相关
 * @author tongdesheng
 *
 */
class YHMApi_App_V1_Cash extends YHMApi_App_V1_Base
{

    /**
     * 提现明细
     * @param array $params
     * @param string $fields
     */
    public static function detail(array $params, $fields = '*')
    {
        $uid = $params['uid'];
        $retData = self::getWithdrawData($uid);
        return self::result(200, '现金账户明细', $retData);
    }

    /**
     * 绑定支付宝
     * @param array $params
     * @param string $fields
     * @return array
     */
    public static function bindAlipay(array $params, $fields = '*')
    {
        if (empty($params['uid'])) {
            return self::result(400, 'uid不能为空');
        }
        if (empty($params['open_id'])) {
            return self::result(400, '绑定账号不能为空');
        }
        if (empty($params['open_token'])) {
            return self::result(400, 'open_token不能为空');
        }
        $uid = $params['uid'];
        $openID = $params['open_id'];
        $accountName = empty($params['alipay_name']) ? '' : $params['alipay_name'];
        $account = '';
        YHMPassport_Models_ShroffAccount_Client::setUserShroffAccount($uid, $openID, $account, $accountName, YHMConfig_Passport::SHROFF_ACCOUNT_ALIPAY);
        return self::result(200, "绑定成功");
    }

    static public function checkBindingAlipay(array $params, $fields = '*')
    {
        if (empty($params['uid'])) {
            return self::result(400, 'uid不能为空');
        }
        $accountData = YHMPassport_Models_ShroffAccount_Client::getAccountByUid($params['uid'], YHMConfig_Passport::SHROFF_ACCOUNT_ALIPAY);
        if (empty($accountData)) {
            return self::result(400, '没有绑定');
        }
        return self::result(200, '绑定信息', $accountData);
    }


    /**
     * 获取提现数据
     * @param integer $uid
     * @return multitype:Integer String mixed string
     */
    private static function getWithdrawData($uid)
    {
        $accountData = YHMPassport_Models_ShroffAccount_Client::getAccountByUid($uid, YHMConfig_Passport::SHROFF_ACCOUNT_ALIPAY);
        //已提现金额
        $withdrewAmount = YHMPassport_Models_Withdraw_Client::getWithdrewByUid($uid);
        //获取可提现金额(包含已经提现的超过退换货期的)
        $canWithdrawAmount = YHMOrders_Models_Orders_Client::getCanWithdrawBySelleruid($uid, 8);
        //冻结金额(未达到退换货期的)
        $freezeAmount = YHMOrders_Models_Orders_Client::getFreezeBySelleruid($uid);
        //获取退款金额
        $refundAmount = YHMOrders_Models_Refund_Client::getRefundAmountByRefunduid($uid);
        $isBindAlipay = 'Y';
        $bind = YHMPassport_Models_ShroffAccount_Client::getAccountByUid($uid, YHMConfig_Passport::SHROFF_ACCOUNT_ALIPAY);
        if (empty($bind)) {
            $isBindAlipay = 'N';
        }
       $str="";
       if ( $accountData['account'])
       {
       $arr= explode('@', $accountData['account']);
       $len=strlen($arr[0]);
       $str=  substr($arr[0], 0,1)."***".substr($arr[0], $len-2,1).'@'.$arr[1];
       }
       $amount=$freezeAmount - $refundAmount-$canWithdrawAmount;
        return array(
            'is_bind_alipay' => $isBindAlipay, //是否绑定了支付宝
            'alipay_account' => $str, //支付宝账户
            'can_withdraw_amount' => $amount - $withdrewAmount , //可提现金额
            'not_withdraw_amount' => $amount - $withdrewAmount, //未提现金额
            'freeze_amount' => ($canWithdrawAmount)?$canWithdrawAmount:0, //冻结金额
            'total_amount' =>  $amount, //累计收入
        );
    }

    /**
     *
     * @param array $params
     * @param string $fields
     */
    public static function withdraw(array $params, $fields = '*')
    {
        $uid = $params['uid'];
        $withdraw_amount = $params['withdraw_amount'];
        $ret = YHMPassport_Models_Withdraw_Client::setWithdraw($uid, $withdraw_amount);
        if (empty($ret)) {
            return self::result(412, '添加提现失败');
        }
        $retData = self::getWithdrawData($uid);
        return self::result(200, '提现成功', $retData);
    }

}