ext.paycenter.PayService.java Source code

Java tutorial

Introduction

Here is the source code for ext.paycenter.PayService.java

Source

/*
 * Copyright (c) 2013, Helome and/or its affiliates. All rights reserved.
 * Helome PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 * Created on 2013-12-5
 */
package ext.paycenter;

import java.math.BigDecimal;

import org.apache.commons.lang3.StringUtils;

import play.Logger;
import play.Logger.ALogger;

/**
 * 
 * 
 * @ClassName: PayService
 * @Description: ?
 * @date 2013-12-5 ?6:49:18
 * @author ShenTeng
 * 
 */
public class PayService {

    private static final ALogger LOGGER = Logger.of(PayService.class);

    /**
     * ?
     */
    private PayService() {
    }

    /**
     * ?token
     * 
     * @param token token
     * @param email ?
     * @param payment ????
     * @param serveduration ?
     * @param isIgnoreLocked ??  true
     * @return
     */
    public static TransferResult transferByToken(String token, String email, BigDecimal payment, Long serveduration,
            boolean isIgnoreLocked) {
        if (StringUtils.isBlank(token) || StringUtils.isBlank(email) || null == payment || null == serveduration) {
            throw new IllegalArgumentException("??token = " + token + ", email = " + email
                    + ", payment = " + payment + ", serveduration = " + serveduration);
        }

        String paymentString = payment.setScale(2, BigDecimal.ROUND_HALF_UP).toString();

        PCResult<Void> result = PCClient.transferax(token, email, paymentString, String.valueOf(serveduration),
                isIgnoreLocked);
        if (result.balanceNotEnough()) {
            return TransferResult.BALANCE_NOT_ENOUGH;
        } else if (result.isSuccess()) {
            return TransferResult.SUCCESS;
        } else if (result.noMatchData()) {
            return TransferResult.INVALID_TOKEN;
        } else if (result.accountLocked()) {
            return TransferResult.ACCOUNT_LOCKED;
        } else {
            LOGGER.error("call transferax error result = " + result);
        }

        return TransferResult.UNKNOWN_ERROR;
    }

    /**
     * ?email
     * 
     * @param email 
     * @param toEmail ?
     * @param payment ????
     * @param serveduration ?
     * @param isIgnoreLocked ??    true
     * @return
     */
    public static TransferResult transferByEmail(String email, String toEmail, BigDecimal payment,
            Long serveduration, boolean isIgnoreLocked) {
        if (StringUtils.isBlank(email) || StringUtils.isBlank(toEmail) || null == payment
                || null == serveduration) {
            throw new IllegalArgumentException("??email = " + email + ", toEmail = " + toEmail
                    + ", payment = " + payment + ", serveduration = " + serveduration);
        }

        String paymentString = payment.setScale(2, BigDecimal.ROUND_HALF_UP).toString();

        PCResult<Void> result = PCClient.transferByEmail(email, toEmail, paymentString,
                String.valueOf(serveduration), isIgnoreLocked);
        if (result.balanceNotEnough()) {
            return TransferResult.BALANCE_NOT_ENOUGH;
        } else if (result.isSuccess()) {
            return TransferResult.SUCCESS;
        } else if (result.noMatchData()) {
            return TransferResult.INVALID_TOKEN;
        } else if (result.accountLocked()) {
            return TransferResult.ACCOUNT_LOCKED;
        } else {
            LOGGER.error("call transferByEmail error result = " + result);
        }

        return TransferResult.UNKNOWN_ERROR;
    }

    /**
     * ?token??
     * 
     * @param token
     * @return
     */
    public static GetBalanceResult getBalanceByToken(String token) {
        if (StringUtils.isBlank(token)) {
            throw new IllegalArgumentException("?? token = " + token);
        }
        PCResult<String> result = PCClient.getBalance(token);
        if (result.noMatchData()) {
            return new GetBalanceResult(GetBalanceResult.STATE.INVALID_TOKEN, null);
        } else if (result.isSuccess()) {
            return new GetBalanceResult(GetBalanceResult.STATE.SUCCESS, result.data);
        } else {
            LOGGER.error("call getBalance error result = " + result);
        }
        return new GetBalanceResult(GetBalanceResult.STATE.UNKNOWN_ERROR, null);
    }

    /**
     * ?email??
     * 
     * @param email email
     * @return
     */
    public static GetBalanceResult getBalanceByEmail(String email) {
        if (StringUtils.isBlank(email)) {
            throw new IllegalArgumentException("?? email = " + email);
        }

        PCResult<String> result = null;
        try {
            result = PCClient.getBalanceBySystem(email);
        } catch (PayCenterException e) {
            LOGGER.error("call getBalanceBySystem error.", e);
            return new GetBalanceResult(GetBalanceResult.STATE.UNKNOWN_ERROR, null);
        }

        if (result.noMatchData()) {
            return new GetBalanceResult(GetBalanceResult.STATE.INVALID_TOKEN, null);
        } else if (result.isSuccess()) {
            return new GetBalanceResult(GetBalanceResult.STATE.SUCCESS, result.data);
        } else {
            LOGGER.error("call getBalanceBySystem error result = " + result);
        }

        return new GetBalanceResult(GetBalanceResult.STATE.UNKNOWN_ERROR, null);
    }

    /**
     * ?
     * 
     * @param email email
     */
    public static LockAccountResult lockAccount(String email) {
        if (StringUtils.isBlank(email)) {
            throw new IllegalArgumentException("?? email = " + email);
        }

        PCResult<String> result = PCClient.lock(email);

        if (result.isSuccess()) {
            return new LockAccountResult(LockAccountResult.STATE.SUCCESS, result.data);
        } else {
            LOGGER.error("call lock error result = " + result);
        }

        return new LockAccountResult(LockAccountResult.STATE.UNKNOWN_ERROR, null);
    }

    /**
     * ?
     * 
     * @param email email
     */
    public static UNLockAccountResult unlockAccount(String email) {
        if (StringUtils.isBlank(email)) {
            throw new IllegalArgumentException("?? email = " + email);
        }

        PCResult<String> result = PCClient.unlock(email);

        if (result.isSuccess()) {
            return new UNLockAccountResult(UNLockAccountResult.STATE.SUCCESS, result.data);
        } else {
            LOGGER.error("call unlock error result = " + result);
        }

        return new UNLockAccountResult(UNLockAccountResult.STATE.UNKNOWN_ERROR, null);
    }

    /**
     * ???
     * 
     * @param email email
     */
    public static GetLockStatusResult getLockStatus(String email) {
        if (StringUtils.isBlank(email)) {
            throw new IllegalArgumentException("?? email = " + email);
        }

        PCResult<Boolean> result = PCClient.getLockStatus(email);

        if (result.isSuccess()) {
            return result.data ? GetLockStatusResult.LOCKED : GetLockStatusResult.UNLOCKED;
        } else {
            LOGGER.error("call unlock error result = " + result);
        }

        return GetLockStatusResult.UNKNOWN_ERROR;
    }

    public enum TransferResult {
        /* ? */
        SUCCESS,
        /* ?? */
        BALANCE_NOT_ENOUGH,
        /* token */
        INVALID_TOKEN,
        /* ? */
        ACCOUNT_LOCKED, UNKNOWN_ERROR
    }

    public static class GetBalanceResult {

        public STATE state;

        /**
         * ????
         */
        public BigDecimal balance;

        private GetBalanceResult(STATE state, String balanceString) {
            this.state = state;
            if (StringUtils.isBlank(balanceString)) {
                this.balance = null;
            } else {
                this.balance = new BigDecimal(balanceString).setScale(2, BigDecimal.ROUND_HALF_UP);
            }
        }

        public enum STATE {
            /* ? */
            SUCCESS,
            /* token */
            INVALID_TOKEN, UNKNOWN_ERROR
        }
    }

    public static class LockAccountResult {

        public STATE state;

        /**
         * ????
         */
        public BigDecimal balance;

        private LockAccountResult(STATE state, String balanceString) {
            this.state = state;
            if (StringUtils.isBlank(balanceString)) {
                this.balance = null;
            } else {
                this.balance = new BigDecimal(balanceString).setScale(2, BigDecimal.ROUND_HALF_UP);
            }
        }

        public enum STATE {
            /* ? */
            SUCCESS, UNKNOWN_ERROR
        }
    }

    public static class UNLockAccountResult {

        public STATE state;

        /**
         * ????
         */
        public BigDecimal balance;

        private UNLockAccountResult(STATE state, String balanceString) {
            this.state = state;
            if (StringUtils.isBlank(balanceString)) {
                this.balance = null;
            } else {
                this.balance = new BigDecimal(balanceString).setScale(2, BigDecimal.ROUND_HALF_UP);
            }
        }

        public enum STATE {
            /* ? */
            SUCCESS, UNKNOWN_ERROR
        }
    }

    public static enum GetLockStatusResult {
        LOCKED, UNLOCKED, UNKNOWN_ERROR
    }

}