com.paysafe.websample.ThreeDSecureSimpleServlet.java Source code

Java tutorial

Introduction

Here is the source code for com.paysafe.websample.ThreeDSecureSimpleServlet.java

Source

/*
 * Copyright (c) 2016 PaySafe
 * 
 * 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 com.paysafe.websample;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.*;
import javax.servlet.*;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.paysafe.Environment;
import com.paysafe.PaysafeApiClient;
import com.paysafe.common.Id;
import com.paysafe.common.PaysafeException;
import com.paysafe.threedsecure.Authentications;
import com.paysafe.threedsecure.EnrollmentChecks;

public class ThreeDSecureSimpleServlet extends PaysafeServletBase {

    private static final long serialVersionUID = 1L;

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {

        request.setAttribute("isPost", "false");
        request.setAttribute("currency", this.currencyCode);

        RequestDispatcher view = request.getRequestDispatcher("threeDSecureSimple.jsp");
        view.forward(request, response);

    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {

        PaysafeApiClient apiClient = new PaysafeApiClient(this.apiKey, this.apiPassword, Environment.TEST,
                this.accountNumber);

        PrintWriter out = response.getWriter();

        String threedsecure = request.getParameter("threed_secure");

        if (threedsecure.equals("submit_enroll")) {

            try {

                EnrollmentChecks responses = EnrollmentChecks.builder()
                        .merchantRefNum(request.getParameter("merchant_Ref_Number")).amount(99999999)
                        .currency(request.getParameter("currency")).customerIp("204.91.0.12")
                        .userAgent(
                                "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36")
                        .acceptHeader("text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*;q=0.8")
                        .merchantUrl("https://www.merchant.com").card().cardNum("4107857757053670").cardExpiry()
                        .month(10).year(2018).done().done().build();

                EnrollmentChecks enroll_responses;

                enroll_responses = apiClient.threeDSecureService().submit(responses);
                out.println("-------" + enroll_responses.getId());
                final GsonBuilder gsonBuilder = new GsonBuilder();
                gsonBuilder.excludeFieldsWithoutExposeAnnotation();
                final Gson gson = gsonBuilder.create();

                out.println("response" + gson.toJson(enroll_responses));
            } catch (PaysafeException e) {
                e.printStackTrace();
                out.println(e.getMessage());
            }

        }

        if (threedsecure.equals("authentication_request")) {
            try {
                EnrollmentChecks enroll_response = EnrollmentChecks.builder()
                        .merchantRefNum(request.getParameter("merchantRefNum")).amount(99999999)
                        .currency(request.getParameter("USD")).customerIp("204.91.0.12")
                        .userAgent(
                                "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36")
                        .acceptHeader("text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
                        .merchantUrl(request.getParameter("https://www.merchant.com")).card()
                        .cardNum("4107857757053670").cardExpiry().month(10).year(2018).done().done().build();
                enroll_response.setId(Id.create("390d8d32-cff5-4887-a96f-554fed7e1722", EnrollmentChecks.class));

                Authentications auth_response = (Authentications.builder()
                        .merchantRefNum(java.util.UUID.randomUUID().toString())
                        .paRes("eJzNWEeT20qS/isKzZEhwRCOL6iegAdIGAKEv7yAI7whDGF+/YDdUqtXodiYXc1heGFVVmZWmsqvKnH851yVnx5x12dN/e0z9BX8/CmuwybK6uTbZ9PgvhCf//lyNNIujplrHI5d/HKU4773k/hTFn373BUL+BX8Cv0dBkn/6KoBhP6GEAw+oHsIQ//GDgcEh/5Goc8vxwupx/2r0D9eh9/Z9gj0BcMIbOP4bsfLZsZX+Aj8mG4bdmHq18PL0Q/vlKi8IAf4AIJH4Pv0WMWdyLxgBEQckE3wbXoEfspdxueo34yfs+jFhU91uPeWwNEm+Uoma6wt1j73aY38dgSeHMfIH+IXGIQwEIWgTyD+Fwr/haBH4JV+bJ/qyKoZN93Q05CPhOMWpW4L4vJCINvS++wYz21TxxvHZuL7+Aj8tK316xfww2+P4U/dG/VoOC/HIat+a9Mr/dgP/jD2L+4R+D46hv7j8SLnZOLCaRpWXB7x1iLyUBnuldSDzc3XV5ZjHGYv4Kbo+f8qRZZJ02VDWr1Abzw/CUfgaQrwmsGX4zVL6m2zLv60naO6//Y5HYb2LwCYpunrtP/adAkAb44A4AHYGKI+S/7x+U0qjsT61vyfxGi/buos9Mts9YftYMjxkDbRp3fbfqfG0J+aIEBn6S+bqi8hhNRfnhRwD6GbTuD3Sj949u/s8quxXe9/6VP/eeqBXxS9HPX4Fj9PRPzJ1MWtGCAYJ3DsaQyTJXE//H82/LHZRw0/9Fl+OcYvzRBCup/Rt8ERPIDIysLR+jvDjqD87YfcG+cReLfwu/lvufoQkzfGihPHXBlaGSXpTrAjbkqu9S2N47mYVnkGLaNV89wJpnoxe8Yz/Qt4ouimQK8nqJc8YbhL5tW1VpRe7rw7aQfqfr1pD5KXVBVnJNlQMaYtmYUBydLXgyqKXDOdF/q6L6t4CulaAXYJw1/zAK/HlD0DFDj38d6e+bZOIwwZmcLb7SbAkjTmpgu7mh0lj1Pci7CIOxW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Sffj")
                        .enrollmentId(enroll_response.getId()).build());

                auth_response = apiClient.threeDSecureService().submit(auth_response);
                out.println("-------" + auth_response.getId());
                final GsonBuilder gsonBuilder1 = new GsonBuilder();
                gsonBuilder1.excludeFieldsWithoutExposeAnnotation();
                final Gson gson1 = gsonBuilder1.create();
                out.println("response" + gson1.toJson(auth_response));

            } catch (PaysafeException e) {

                e.printStackTrace();
                out.println(e.getMessage());
            }
        }

    }
}