KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > epayment > processor > PaymentProcessor


1 package epayment.processor;
2
3 import epayment.framework.IGatewayAdapter;
4 import epayment.framework.IPaymentCommand;
5 import epayment.framework.IPaymentResponse;
6 import epayment.framework.PaymentException;
7
8 /**
9  * The <code>PaymentProcessor</code> class is a bridge
10  * for an <code>IGatewayAdapter</code>. This class is
11  * responsible for processing <code>IPaymentCommand</code>
12  * instances, while decoupling them from specific
13  * payment processing implementations.
14  * <p>
15  * This class is strictly an example.
16  *
17  * @author <a HREF="mailto:mike@clarkware.com">Mike Clark</a>
18  * @author <a HREF="http://www.clarkware.com">Clarkware Consulting</a>
19  */

20  
21 public class PaymentProcessor {
22
23     private IGatewayAdapter _adapter;
24     private static PaymentProcessor _processor;
25     
26     /**
27      * Constructs a <code>PaymentProcessor</code>
28      * instance using the default configurator.
29      */

30     public PaymentProcessor() {
31         
32         try {
33         
34             PaymentProcessorConfigurator configurator =
35                 new PaymentProcessorConfigurator();
36             configurator.configure(this);
37         
38         } catch(Exception JavaDoc e) {
39             System.err.println("Payment processor configuration error: " +
40                 e.getMessage());
41             // default to consistent state
42
}
43     }
44
45     /**
46      * Constructs a <code>PaymentProcessor</code>
47      * instance with the specified gateway adapter.
48      *
49      * @param adapter Gateway adapter.
50      */

51     public PaymentProcessor(IGatewayAdapter adapter) {
52         setGatewayAdapter(adapter);
53     }
54     
55     /**
56      * Returns the sole instance of this class.
57      *
58      * @return Payment processor.
59      */

60     public static PaymentProcessor getProcessor() {
61         if (_processor == null) {
62             _processor = new PaymentProcessor();
63         }
64         return _processor;
65     }
66         
67     /**
68      * Sets the gateway adapter.
69      *
70      * @param adapter Gateway adapter.
71      */

72     protected void setGatewayAdapter(IGatewayAdapter adapter) {
73         _adapter = adapter;
74     }
75     
76     /**
77      * Processes the specified payment command using
78      * the specified payment request and returns a
79      * payment response.
80      *
81      * @param command Payment command.
82      * @return response Payment response.
83      * @throws PaymentException If an error occurs.
84      */

85     public IPaymentResponse process(IPaymentCommand command)
86         throws PaymentException {
87         
88         return command.execute(_adapter);
89     }
90 }
91
Popular Tags