View Javadoc
1   /*
2    *
3    *  * Copyright (c) 2002-2017, Mairie de Paris
4    *  * All rights reserved.
5    *  *
6    *  * Redistribution and use in source and binary forms, with or without
7    *  * modification, are permitted provided that the following conditions
8    *  * are met:
9    *  *
10   *  *  1. Redistributions of source code must retain the above copyright notice
11   *  *     and the following disclaimer.
12   *  *
13   *  *  2. Redistributions in binary form must reproduce the above copyright notice
14   *  *     and the following disclaimer in the documentation and/or other materials
15   *  *     provided with the distribution.
16   *  *
17   *  *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
18   *  *     contributors may be used to endorse or promote products derived from
19   *  *     this software without specific prior written permission.
20   *  *
21   *  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22   *  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23   *  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24   *  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
25   *  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26   *  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27   *  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28   *  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29   *  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30   *  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31   *  * POSSIBILITY OF SUCH DAMAGE.
32   *  *
33   *  * License 1.0
34   *
35   */
36  
37  /**
38   * 
39   */
40  package fr.paris.lutece.plugins.tipi.business;
41  
42  import java.io.File;
43  import java.io.Serializable;
44  import java.math.BigDecimal;
45  import java.rmi.RemoteException;
46  import java.text.ParseException;
47  import java.util.Date;
48  
49  import javax.servlet.http.HttpServletRequest;
50  import javax.xml.rpc.ServiceException;
51  
52  import org.apache.commons.lang.StringUtils;
53  
54  import fr.paris.lutece.plugins.tipi.util.Constants;
55  import fr.paris.lutece.plugins.tipi.util.Utils;
56  import fr.paris.lutece.portal.service.spring.SpringContextService;
57  import fr.paris.lutece.portal.service.util.AppLogService;
58  import fr.paris.lutece.portal.service.util.AppPropertiesService;
59  import fr.paris.tipi.generated.CreerPaiementSecuriseRequest;
60  import fr.paris.tipi.generated.RecupererDetailPaiementSecuriseRequest;
61  import fr.paris.vdp.tipi.create.url.utils.PaiementUtils;
62  import fr.paris.vdp.tipi.create.url.webservice.CreateURLWebService;
63  import fr.paris.vdp.tipi.create.url.webservice.ParametresPaiementTipi;
64  
65  /**
66   * Classe de gestion du paiement Tipi
67   */
68  public class Tipi implements Serializable
69  {
70      private static final long      serialVersionUID    = 7173528813310268296L;
71  
72      private static final String    TRUSTSTORE          = "tipi.ssl.truststore";
73      private static final String    TRUSTSTORE_PASSWORD = "tipi.ssl.truststore.password";
74      private static final String    KEYSTORE            = "tipi.ssl.keystore";
75      private static final String    KEYSTORE_PASSWORD   = "tipi.ssl.keystore.password";
76  
77      private static final String    URLWSDL             = "tipi.urlwsdl";
78  
79      private static final String    CLIENT              = "client";
80      private static final String    URL_TYPE            = "tipi.url";
81  
82      private String                 identifier;
83      private ParametresPaiementTipi parameters;
84  
85      private TipiProcessor          processor           = SpringContextService.getBean( "tipiProcessor" );
86  
87      /**
88       * Default constructor
89       */
90      private Tipi( )
91      {
92          /* Init (or re-init) certificate each time we use Tipi. */
93          this.setCertificateValues( );
94      }
95  
96      /**
97       * Créer une nouvelle instance
98       * @return
99       */
100     private static Tipi newInstance( )
101     {
102         return new Tipi( );
103     }
104 
105     /**
106      * Génération d'un nouveau paiement
107      * 
108      * @param email
109      *            email
110      * @param refDet
111      *            référence
112      * @param amount
113      *            montant
114      * @return l'instance de gestion de Tipi
115      * @throws RemoteException
116      * @throws ServiceException
117      */
118     public static Tipi create( String email, String refDet, BigDecimal amount ) throws RemoteException, ServiceException
119     {
120         Tipi tipi = newInstance( );
121 
122         final String urlWsdl = getWsdlUrl( );
123 
124         final CreerPaiementSecuriseRequest request = TipiRequestFactory.createRequest( email, refDet, amount );
125 
126         AppLogService.debug( "url du webservice : " + urlWsdl );
127         AppLogService.debug( "parametre de la requete : " + Utils.object2String( request ) );
128 
129         tipi.identifier = new CreateURLWebService( ).appelWebServiceCreerPaiement( urlWsdl, request );
130 
131         return tipi;
132     }
133 
134     /**
135      * Récupère les informations d'un paiement
136      * 
137      * @param idOp
138      *            l'identifiant du paiement
139      * @return l'instance de gestion de Tipi
140      * @throws ServiceException
141      * @throws RemoteException
142      */
143     public static Tipi read( String idOp ) throws ServiceException, RemoteException
144     {
145         Tipi tipi = newInstance( );
146 
147         final String urlWsdl = getWsdlUrl( );
148 
149         RecupererDetailPaiementSecuriseRequest request = new RecupererDetailPaiementSecuriseRequest( );
150         request.setIdOp( idOp );
151 
152         AppLogService.debug( "url du webservice : " + urlWsdl );
153         AppLogService.debug( "parametre de la requete : " + Utils.object2String( request ) );
154 
155         tipi.identifier = idOp;
156         tipi.parameters = new CreateURLWebService( ).appelWebserviceDetailPaiement( request, urlWsdl );
157 
158         return tipi;
159     }
160 
161     /**
162      * Récupère les informations d'un paiement
163      * 
164      * @param request
165      *            la requete http contenant le paramètre idOp ou idop
166      * @return l'instance de gestion de Tipi
167      * @throws RemoteException
168      * @throws ServiceException
169      */
170     public static Tipi read( HttpServletRequest request ) throws RemoteException, ServiceException
171     {
172         String idOp = request.getParameter( "idOp" );
173 
174         if ( StringUtils.isBlank( idOp ) )
175         {
176             idOp = String.valueOf( request.getParameter( "idop" ) );
177         }
178 
179         if ( StringUtils.isBlank( idOp ) )
180         {
181             AppLogService.error( "echec traitement servlet, idOp is null" );
182             return null;
183         }
184 
185         return read( idOp );
186     }
187 
188     /**
189      * Retourne l'url du WSDL
190      * @return String url
191      */
192     private static final String getWsdlUrl( )
193     {
194         return AppPropertiesService.getProperty( URLWSDL );
195     }
196 
197     /**
198      * Ajoute le certificat Tipi
199      */
200     private final void setCertificateValues( )
201     {
202         // Valorisation des proprietes systemes pour les echanges SSL avec le webservice
203         if ( AppPropertiesService.getProperty( KEYSTORE ).isEmpty( ) )
204         {
205             File file = new File( getClass( ).getClassLoader( ).getResource( "security/cacerts" ).getFile( ) );
206             System.setProperty( "javax.net.ssl.trustStore", file.getAbsolutePath( ) );
207             System.setProperty( "javax.net.ssl.keyStore", file.getAbsolutePath( ) );
208         } else
209         {
210             System.setProperty( "javax.net.ssl.trustStore", AppPropertiesService.getProperty( TRUSTSTORE ) );
211             System.setProperty( "javax.net.ssl.keyStore", AppPropertiesService.getProperty( KEYSTORE ) );
212         }
213 
214         System.setProperty( "javax.net.ssl.trustStorePassword", AppPropertiesService.getProperty( TRUSTSTORE_PASSWORD ) );
215         System.setProperty( "javax.net.ssl.keyStorePassword", AppPropertiesService.getProperty( KEYSTORE_PASSWORD ) );
216     }
217 
218     /**
219      * Récupère les paramètres de la transaction
220      * 
221      * @return les paramètres de la transaction
222      */
223     public ParametresPaiementTipi getParameters( )
224     {
225         return parameters;
226     }
227 
228     /**
229      * Retourne la date du paiement
230      * 
231      * @return date
232      */
233     public Date getDate( )
234     {
235         Date date = null;
236 
237         if ( parameters != null )
238         {
239             try
240             {
241                 date = PaiementUtils.constructDate( parameters.getDatTrans( ), parameters.getHeurTrans( ) );
242             } catch ( ParseException e )
243             {
244                 // Do nothing
245             }
246         }
247 
248         return date;
249     }
250 
251     /**
252      * Retourne les paramètres d'une transaction Tipi
253      * 
254      * @param idop
255      * @return les paramètres de la transaction
256      * @throws RemoteException
257      * @throws ServiceException
258      */
259     public static ParametresPaiementTipi getParameters( String idop ) throws RemoteException, ServiceException
260     {
261         return read( idop ).getParameters( );
262     }
263 
264     /**
265      * Retourne un booléen indiquant si le paiement est un succès
266      * 
267      * @return vrai si le paiement est un succès
268      */
269     public boolean isPaymentSuccess( )
270     {
271         return Constants.PAYMENT_SUCCESS.equals( parameters.getResultrans( ) );
272     }
273 
274     /**
275      * Retourne un booléen indiquant si le paiement est refusé
276      * 
277      * @return vrai si le paiement est refusé
278      */
279     public boolean isPaymentDenied( )
280     {
281         return Constants.PAYMENT_DENIED.equals( parameters.getResultrans( ) );
282     }
283 
284     /**
285      * Retourne un booléen indiquant si le paiement est annulé
286      * 
287      * @return vrai si le paiement est annulé
288      */
289     public boolean isPaymentCancelled( )
290     {
291         return Constants.PAYMENT_CANCELLED.equals( parameters.getResultrans( ) );
292     }
293 
294     /**
295      * Gère le résultat dans le result handler
296      * 
297      * @return l'instance de gestion de Tipi
298      */
299     public Tipi process( )
300     {
301         if ( this.processor == null )
302         {
303             AppLogService.info( "Aucun handler n'a été défini pour gérer le retour du service Tipi" );
304         } else
305         {
306             this.processor.process( this );
307         }
308 
309         return this;
310     }
311 
312     public String getLink( )
313     {
314         Boolean isClient = CLIENT.equals( AppPropertiesService.getProperty( URL_TYPE ) );
315         return Utils.getUrlApplicationTipi( identifier, isClient );
316     }
317 
318     /**
319      * Récupère l'identifiant de transaction Tipi
320      *
321      * @return l'identifiant de transaction Tipi
322      */
323     public String getIdentifier( )
324     {
325         return identifier;
326     }
327 }