com.eywa.impl.app.mongo.entities.items.payment.ItemPayTransaction.java Source code

Java tutorial

Introduction

Here is the source code for com.eywa.impl.app.mongo.entities.items.payment.ItemPayTransaction.java

Source

/*
 * EYWA.COM (Eywa Commerce)
 * This program is an integrated platform with E-Commerce and Configurator system.
 * Support: Please, contact the Author on http://www.smartfeeling.org.
 * Copyright (C) 2014  Gian Angelo Geminiani
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.eywa.impl.app.mongo.entities.items.payment;

import com.eywa.impl.app.IAppConstants;
import com.eywa.impl.app.controllers.paypal.IPayPalConstants;
import com.eywa.impl.app.controllers.paypal.wrappers.PayPalResponse;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import org.ly.commons.util.CollectionUtils;
import org.ly.commons.util.DateUtils;
import org.ly.packages.mongo.impl.util.MongoUtils;

import java.util.List;

/**
 * Transaction Payment.
 */
public class ItemPayTransaction extends BasicDBObject {

    // ------------------------------------------------------------------------
    //                      Constants
    // ------------------------------------------------------------------------

    public static final String STATUS_CREATED = IPayPalConstants.STATUS_CREATED;
    public static final String STATUS_COMPLETED = IPayPalConstants.STATUS_COMPLETED;
    public static final String STATUS_INCOMPLETE = IPayPalConstants.STATUS_INCOMPLETE;
    public static final String STATUS_ERROR = IPayPalConstants.STATUS_ERROR;
    public static final String STATUS_REVERSALERROR = IPayPalConstants.STATUS_REVERSALERROR;
    public static final String STATUS_PROCESSING = IPayPalConstants.STATUS_PROCESSING;
    public static final String STATUS_PENDING = IPayPalConstants.STATUS_PENDING;

    // ------------------------------------------------------------------------
    //                      fields
    // ------------------------------------------------------------------------

    public static final String UID = IAppConstants.UID; // transaction uid
    public static final String PAYMENT_ID = IAppConstants.PAYMENT_ID; // original payment
    public static final String CREATION_DATE = IAppConstants.CREATION_DATE; // milliseconds
    public static final String SELLER_ID = IAppConstants.SELLER_ID;
    public static final String SELLER_EMAIL = IAppConstants.SELLER_EMAIL;
    public static final String SELLER_TYPE = IAppConstants.SELLER_TYPE; // billing type
    public static final String CUSTOMER_ID = IAppConstants.CUSTOMER_ID;
    public static final String CUSTOMER_EMAIL = IAppConstants.CUSTOMER_EMAIL;
    public static final String PAY_RESPONSE = IAppConstants.PAY_RESPONSE; // native full pay response
    public static final String PAY_KEY = PayPalResponse.PAY_KEY;
    public static final String PAY_EXEC_STATUS = PayPalResponse.PAY_EXEC_STATUS;
    public static final String PAY_URL = PayPalResponse.PAY_URL;
    public static final String ERROR = IAppConstants.ERROR;

    public static final String CALLBACK_PARAMS = IAppConstants.CALLBACK_PARAMS; // optional callback params
    public static final String CART = IAppConstants.CART; // item session data cart

    // ------------------------------------------------------------------------
    //                      c o n s t r u c t o r
    // ------------------------------------------------------------------------

    public ItemPayTransaction(final DBObject raw) {
        this.init(raw);
    }

    public ItemPayTransaction() {
        this.init(null);
    }

    // ------------------------------------------------------------------------
    //                      p r i v a t e
    // ------------------------------------------------------------------------

    private void init(final DBObject raw) {
        final long now = DateUtils.now().getTime();
        this.put(CREATION_DATE, now);
        this.put(UID, MongoUtils.createUUID());
        if (null != raw) {
            setUid(this, getUid(raw));
            setPaymentId(this, getPaymentId(raw));
            setCreationDate(this, getCreationDate(raw));
            setError(this, getError(raw));
            setPayResponse(this, getPayResponse(raw));
            setPayKey(this, getPayKey(raw));
            setPayExecStatus(this, getPayExecStatus(raw));
            setPayUrl(this, getPayUrl(raw));
            setCart(this, getCart(raw));
            setCustomerEmail(this, getCustomerEmail(raw));
            setCustomerId(this, getCustomerId(raw));
            setSellerId(this, getSellerId(raw));
            setSellerEmail(this, getSellerEmail(raw));
            setSellerType(this, getSellerType(raw));

        }
    }

    // ------------------------------------------------------------------------
    //                      S T A T I C
    // ------------------------------------------------------------------------

    public static boolean isCreated(final DBObject item) {
        return STATUS_CREATED.equalsIgnoreCase(getPayExecStatus(item));
    }

    public static boolean isCompleted(final DBObject item) {
        return STATUS_COMPLETED.equalsIgnoreCase(getPayExecStatus(item));
    }

    public static String getUid(final DBObject item) {
        return MongoUtils.getString(item, UID);
    }

    public static void setUid(final DBObject item, final String value) {
        MongoUtils.put(item, UID, value);
    }

    public static String getPaymentId(final DBObject item) {
        return MongoUtils.getString(item, PAYMENT_ID);
    }

    public static void setPaymentId(final DBObject item, final String value) {
        MongoUtils.put(item, PAYMENT_ID, value);
    }

    public static long getCreationDate(final DBObject item) {
        return MongoUtils.getLong(item, CREATION_DATE);
    }

    public static void setCreationDate(final DBObject item, final long value) {
        MongoUtils.put(item, CREATION_DATE, value);
    }

    public static String getSellerId(final DBObject item) {
        return MongoUtils.getString(item, SELLER_ID);
    }

    public static void setSellerId(final DBObject item, final String value) {
        MongoUtils.put(item, SELLER_ID, value);
    }

    public static String getSellerEmail(final DBObject item) {
        return MongoUtils.getString(item, SELLER_EMAIL);
    }

    public static void setSellerEmail(final DBObject item, final String value) {
        MongoUtils.put(item, SELLER_EMAIL, value);
    }

    public static String getSellerType(final DBObject item) {
        return MongoUtils.getString(item, SELLER_TYPE);
    }

    public static void setSellerType(final DBObject item, final String value) {
        MongoUtils.put(item, SELLER_TYPE, value);
    }

    public static String getCustomerId(final DBObject item) {
        return MongoUtils.getString(item, CUSTOMER_ID);
    }

    public static void setCustomerId(final DBObject item, final String value) {
        MongoUtils.put(item, CUSTOMER_ID, value);
    }

    public static String getCustomerEmail(final DBObject item) {
        return MongoUtils.getString(item, CUSTOMER_EMAIL);
    }

    public static void setCustomerEmail(final DBObject item, final String value) {
        MongoUtils.put(item, CUSTOMER_EMAIL, value);
    }

    public static DBObject getPayResponse(final DBObject item) {
        return MongoUtils.getDBObject(item, PAY_RESPONSE);
    }

    public static void setPayResponse(final DBObject item, final DBObject value) {
        MongoUtils.put(item, PAY_RESPONSE, value);
        if (null != value) {
            setPayKey(item, getPayKey(value));
            setPayExecStatus(item, getPayExecStatus(value));
            setPayUrl(item, getPayUrl(value));
            final List errors = MongoUtils.getList(value, "error");
            if (!CollectionUtils.isEmpty(errors)) {
                final DBObject error = (DBObject) CollectionUtils.getFirst(errors);
                setError(item, MongoUtils.getString(error, "message"));
            }
        }

    }

    public static String getPayKey(final DBObject item) {
        return MongoUtils.getString(item, PAY_KEY);
    }

    public static void setPayKey(final DBObject item, final String value) {
        MongoUtils.put(item, PAY_KEY, value);
    }

    public static String getPayExecStatus(final DBObject item) {
        return MongoUtils.getString(item, PAY_EXEC_STATUS);
    }

    public static void setPayExecStatus(final DBObject item, final String value) {
        MongoUtils.put(item, PAY_EXEC_STATUS, value);
    }

    public static String getPayUrl(final DBObject item) {
        return MongoUtils.getString(item, PAY_URL);
    }

    public static void setPayUrl(final DBObject item, final String value) {
        MongoUtils.put(item, PAY_URL, value);
    }

    public static DBObject getCallbackParams(final DBObject item) {
        return MongoUtils.getDBObject(item, CALLBACK_PARAMS);
    }

    public static void setCallbackParams(final DBObject item, final DBObject value) {
        MongoUtils.put(item, CALLBACK_PARAMS, value);
    }

    public static DBObject getCart(final DBObject item) {
        return MongoUtils.getDBObject(item, CART);
    }

    public static void setCart(final DBObject item, final DBObject value) {
        MongoUtils.put(item, CART, value);
    }

    public static String getError(final DBObject item) {
        return MongoUtils.getString(item, ERROR);
    }

    public static void setError(final DBObject item, final String value) {
        MongoUtils.put(item, ERROR, value);
    }

}