Friday, 27 January 2012

Generic RMA for goodwill customer refund

The code sample below creates a generic RMA used to refund money back to a customer without a specific order item or in other words without returning an item. This can be used for goodwill refunds which most probably are done using gift vouchers. You need to specify the payment plugin policy ID that will be used to perform the refund.

The same logic can be used as well for more complex refund scenarios where you have to refund using two different payment methods e.g. £10 to payment card and £20 using a gift voucher.

/**
* Creates a generic (without associated order item) refund against a payment plugin. The steps are as follows:
* 1-Create RMA
* 2-Execute ReturnProcessCmd. The most important thing here is to pass the PaymentMethodId which identifies which payment plugin will be used. This is important if you support different
* payment methods e.g. payment card and gift vouchers. Most probably this will be used against gift voucher. This step will create a PI within the database and the payment plugin's checkPaymentInstruction
* will be executed.
* 3-Execute FinalizeRefundCmd which will internally call the payment plugin's credit method to perform the actual refund to customer
* @param orderId
* @param storeId
* @param refundAmount
* @param cmdContext
* @return true if the refund is created successfully.
*/
public static boolean createGenericRefund(Long orderId,BigDecimal refundAmount,String rmaComment,CommandContext cmdContext){
final String METHOD_NAME = "createGenericGiftVoucherRefund";
LOGGER.entering(CLASSNAME, METHOD_NAME);

boolean isSuccessful=false;
try{

RMAAccessBean rma = new RMAAccessBean(cmdContext.getStoreId(),cmdContext.getUserId(),new Timestamp(System.currentTimeMillis()), new Timestamp(System.currentTimeMillis()));
rma.setTotalCredit(refundAmount);
rma.setCurrency(cmdContext.getCurrency());
rma.setTradingId("10502");
rma.setRefundAgainstOrdId(orderId);
rma.setPrepared("Y");
rma.setComments(rmaComment);
rma.commitCopyHelper();

if(LOGGER.isLoggable(WsLevel.FINE)){
LOGGER.logp(Level.FINE, CLASSNAME, METHOD_NAME, "RMA with ID "+rma.getRmaId()+" created successfully with refund amount of "+refundAmount+ " "+cmdContext.getCurrency());
}

if(LOGGER.isLoggable(WsLevel.FINE)){
LOGGER.logp(Level.FINE, CLASSNAME, METHOD_NAME, "Initiate ReturnProcessCmd against giftvoucher policy ID");
}
ReturnProcessCmd rcmd = (ReturnProcessCmd) CommandFactory.createCommand(ReturnProcessCmd.NAME,cmdContext.getStoreId());
TypedProperty pParms = new TypedProperty();
pParms.put("storeId",cmdContext.getStoreId());
pParms.put("RMAId",rma.getRmaId());
pParms.put("URL", "");
pParms.put("PaymentMethodId", OrderUtil.getPaymentPluginPolicyId(cmdContext.getStoreId());
rcmd.setRequestProperties(pParms);
rcmd.setCommandContext(cmdContext);
rcmd.execute();

if(LOGGER.isLoggable(WsLevel.FINE)){
LOGGER.logp(Level.FINE, CLASSNAME, METHOD_NAME, "ReturnProcessCmd completed successfully");
}

if(LOGGER.isLoggable(WsLevel.FINE)){
LOGGER.logp(Level.FINE, CLASSNAME, METHOD_NAME, "Initiate FinalizeRefundCmd");
}
FinalizeRefundCmd fcmd = (FinalizeRefundCmd) CommandFactory.createCommand(FinalizeRefundCmd.NAME,cmdContext.getStoreId());
fcmd.setOrderId(new Long(orderId));
fcmd.setRmaId(new Long(rma.getRmaId()));
fcmd.setRMAAB(rma);
fcmd.setCommandContext(cmdContext);
fcmd.execute();

if(LOGGER.isLoggable(WsLevel.FINE)){
LOGGER.logp(Level.FINE, CLASSNAME, METHOD_NAME, "FinalizeRefundCmd completed successfully");
}

isSuccessful=true;
}catch(Exception e){
LOGGER.logp(Level.SEVERE,CLASSNAME,METHOD_NAME,"Exception caused by "+e.getMessage(),e);
}

LOGGER.exiting(CLASSNAME, METHOD_NAME);
return isSuccessful;
}

No comments:

Post a Comment