ph.com.globe.connect.Payment.java Source code

Java tutorial

Introduction

Here is the source code for ph.com.globe.connect.Payment.java

Source

/* 
 * The MIT License
 *
 * Copyright 2016 charleszamora.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package ph.com.globe.connect;

import java.net.URISyntaxException;
import java.util.Map;
import java.util.HashMap;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.utils.URIBuilder;

/**
 * Payment Services Class.
 * 
 * @author Charles Zamora czamora@openovate.com
 */
public class Payment extends Context {
    /* Payment API Url */
    private final String PAYMENT_URL = "https://devapi.globelabs.com.ph/payment/v1/transactions/amount";

    /* Payment last reference url */
    private final String LAST_REF_URL = "https://devapi.globelabs.com.ph/payment/v1/transactions/getLastRefCode";

    /* API app id */
    protected String appId = null;

    /* API app secret */
    protected String appSecret = null;

    /* API Access token */
    protected String accessToken = null;

    /* Payment amount */
    protected double amount = 0.00;

    /* Payment description */
    protected String description = null;

    /* User id / Subscribers number */
    protected String endUserId = null;

    /* Custom reference code */
    protected String referenceCode = null;

    /* Resource state */
    protected String transactionOperationStatus = null;

    /**
     * Create Payment class without parameters.
     */
    public Payment() {
        super();
    }

    /**
     * Create Payment class with access 
     * token parameter.
     * 
     * @param accessToken access token
     */
    public Payment(String accessToken) {
        super();

        // set access token
        this.accessToken = accessToken;
    }

    /**
     * Create Payment class with app id 
     * and app secret parameters.
     * 
     * @param appId app id
     * @param appSecret app secret
     */
    public Payment(String appId, String appSecret) {
        super();

        // set app id
        this.appId = appId;
        // set app secret
        this.appSecret = appSecret;
    }

    /**
     * Create Payment class with app id,
     * app secret and access token 
     * parameters.
     * 
     * @param appId app id
     * @param appSecret app secret
     * @param accessToken access token
     */
    public Payment(String appId, String appSecret, String accessToken) {
        super();

        // set app id
        this.appId = appId;
        // set app secret
        this.appSecret = appSecret;
        // set access token
        this.accessToken = accessToken;
    }

    /**
     * Set API app id.
     * 
     * @param appId app id
     * @return this
     */
    public Payment setAppId(String appId) {
        // set app id
        this.appId = appId;

        return this;
    }

    /**
     * Set API app secret.
     * 
     * @param appSecret app secret
     * @return this 
     */
    public Payment setAppSecret(String appSecret) {
        // set app secret
        this.appSecret = appSecret;

        return this;
    }

    /**
     * Sets access token.
     * 
     * @param  accessToken access token
     * @return this
     */
    public Payment setAccessToken(String accessToken) {
        // set access token
        this.accessToken = accessToken;

        return this;
    }

    /**
     * Sets payment amount.
     * 
     * @param  amount payment amount
     * @return this
     */
    public Payment setAmount(double amount) {
        // set amount
        this.amount = amount;

        return this;
    }

    /**
     * Set payment description.
     * 
     * @param  description payment description
     * @return this
     */
    public Payment setDescription(String description) {
        // set description
        this.description = description;

        return this;
    }

    /**
     * Set end user id.
     * 
     * @param  endUserId end user id
     * @return this
     */
    public Payment setEndUserId(String endUserId) {
        // set end user id
        this.endUserId = endUserId;

        return this;
    }

    /**
     * Set reference code.
     * 
     * @param  referenceCode reference code
     * @return this
     */
    public Payment setReferenceCode(String referenceCode) {
        // set reference code
        this.referenceCode = referenceCode;

        return this;
    }

    /**
     * Set transaction operation status.
     * 
     * @param  transactionOperationStatus resource state
     * @return this
     */
    public Payment setTransactionOperationStatus(String transactionOperationStatus) {
        // set transaction operation status
        this.transactionOperationStatus = transactionOperationStatus;

        return this;
    }

    /**
     * Sends payment request.
     * 
     * @param  amount payment amount
     * @param  description payment description
     * @param  endUserId end user id
     * @param  referenceCode custom reference code
     * @param  transactionOperationStatus resource state
     * @return HttpResponse
     * @throws ApiException api exception
     * @throws HttpRequestException http request exception
     * @throws HttpResponseException http response exception
     */
    public HttpResponse sendPaymentRequest(double amount, String description, String endUserId,
            String referenceCode, String transactionOperationStatus)
            throws ApiException, HttpRequestException, HttpResponseException {

        // build url
        String url = this.buildUrl(this.PAYMENT_URL);

        // set base data
        Map<String, Object> data = new HashMap<>();

        // set amount
        data.put("amount", String.format("%.2f", amount));
        // set description
        data.put("description", description);
        // set end user id
        data.put("endUserId", endUserId);
        // set reference code
        data.put("referenceCode", referenceCode);
        // set transaction operation status
        data.put("transactionOperationStatus", transactionOperationStatus);

        // send request
        CloseableHttpResponse results = this.request
                // set url
                .setUrl(url)
                // set data
                .setData(data)
                // send post request
                .sendPost();

        return new HttpResponse(results);
    }

    /**
     * Sends payment request.
     * 
     * @return HttpResponse
     * @throws ApiException api exception
     * @throws HttpRequestException http request exception
     * @throws HttpResponseException http response exception
     */
    public HttpResponse sendPaymentRequest() throws ApiException, HttpRequestException, HttpResponseException {

        // call send payment request
        return this.sendPaymentRequest(this.amount, this.description, this.endUserId, this.referenceCode,
                this.transactionOperationStatus);
    }

    /**
     * Get last reference code request.
     * 
     * @return HttpResponse
     * @throws ApiException api exception
     * @throws HttpRequestException http request exception
     * @throws HttpResponseException http response exception
     */
    public HttpResponse getLastReferenceCode() throws ApiException, HttpRequestException, HttpResponseException {

        // build url
        String url = this.buildUrl(this.LAST_REF_URL, this.appId, this.appSecret);

        // send request
        CloseableHttpResponse results = this.request
                // set url
                .setUrl(url)
                // send get request
                .sendGet();

        return new HttpResponse(results);
    }

    /**
     * Build request url.
     * 
     * @param  url target url
     * @return String
     * @throws ApiException api exception
     */
    protected String buildUrl(String url) throws ApiException {
        // try parsing url
        try {
            // initialize url builder
            URIBuilder builder = new URIBuilder(url);

            // set access token parameter
            builder.setParameter("access_token", this.accessToken);

            // build the url
            url = builder.build().toString();

            return url;
        } catch (URISyntaxException e) {
            // throw exception
            throw new ApiException(e.getMessage());
        }
    }

    /**
     * Build request url.
     * 
     * @param  url target url
     * @param  appId app id
     * @param  appSecret app secret
     * @return String
     * @throws ApiException api exception
     */
    protected String buildUrl(String url, String appId, String appSecret) throws ApiException {
        // try parsing url
        try {
            // initialize url builder
            URIBuilder builder = new URIBuilder(url);

            // set app id parameter
            builder.setParameter("app_id", appId);
            // set app secret parameter
            builder.setParameter("app_secret", appSecret);

            // build the url
            url = builder.build().toString();

            return url;
        } catch (URISyntaxException e) {
            // throw exception
            throw new ApiException(e.getMessage());
        }
    }
}