1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
package epayment.processor;
import epayment.framework.IGatewayAdapter;
import epayment.framework.IPaymentCommand;
import epayment.framework.IPaymentResponse;
import epayment.framework.PaymentException;
/**
* The <code>PaymentProcessor</code> class is a bridge
* for an <code>IGatewayAdapter</code>. This class is
* responsible for processing <code>IPaymentCommand</code>
* instances, while decoupling them from specific
* payment processing implementations.
* <p>
* This class is strictly an example.
*
* @author <a href="mailto:mike@clarkware.com">Mike Clark</a>
* @author <a href="http://www.clarkware.com">Clarkware Consulting</a>
*/
public class PaymentProcessor {
private IGatewayAdapter _adapter;
private static PaymentProcessor _processor;
/**
* Constructs a <code>PaymentProcessor</code>
* instance using the default configurator.
*/
public PaymentProcessor() {
try {
PaymentProcessorConfigurator configurator =
new PaymentProcessorConfigurator();
configurator.configure(this);
} catch(Exception e) {
System.err.println("Payment processor configuration error: " +
e.getMessage());
// default to consistent state
}
}
/**
* Constructs a <code>PaymentProcessor</code>
* instance with the specified gateway adapter.
*
* @param adapter Gateway adapter.
*/
public PaymentProcessor(IGatewayAdapter adapter) {
setGatewayAdapter(adapter);
}
/**
* Returns the sole instance of this class.
*
* @return Payment processor.
*/
public static PaymentProcessor getProcessor() {
if (_processor == null) {
_processor = new PaymentProcessor();
}
return _processor;
}
/**
* Sets the gateway adapter.
*
* @param adapter Gateway adapter.
*/
protected void setGatewayAdapter(IGatewayAdapter adapter) {
_adapter = adapter;
}
/**
* Processes the specified payment command using
* the specified payment request and returns a
* payment response.
*
* @param command Payment command.
* @return response Payment response.
* @throws PaymentException If an error occurs.
*/
public IPaymentResponse process(IPaymentCommand command)
throws PaymentException {
return command.execute(_adapter);
}
}
|