📄 paymentgatewayservices.java
字号:
pgResponse.set("orderPaymentPreferenceId", paymentPref.get("orderPaymentPreferenceId"));
pgResponse.set("paymentMethodTypeId", paymentPref.get("paymentMethodTypeId"));
pgResponse.set("paymentMethodId", paymentPref.get("paymentMethodId"));
// set the auth info
pgResponse.set("referenceNum", releaseResult.get("releaseRefNum"));
pgResponse.set("gatewayCode", releaseResult.get("releaseCode"));
pgResponse.set("gatewayFlag", releaseResult.get("releaseFlag"));
pgResponse.set("gatewayMessage", releaseResult.get("releaseMessage"));
pgResponse.set("transactionDate", UtilDateTime.nowTimestamp());
// store the gateway response
try {
pgResponse.create();
} catch (GenericEntityException e) {
Debug.logError(e, "Problem storing PaymentGatewayResponse entity; authorization was released! : " + pgResponse, module);
}
if (releaseResponse != null && releaseResponse.booleanValue()) {
paymentPref.set("statusId", "PAYMENT_CANCELLED");
try {
paymentPref.store();
} catch (GenericEntityException e) {
Debug.logError(e, "Problem storing updated payment preference; authorization was released!", module);
}
finished.add(paymentPref);
// cancel any payment records
List paymentList = null;
try {
paymentList = paymentPref.getRelated("Payment");
} catch (GenericEntityException e) {
Debug.logError(e, "Unable to get Payment records from OrderPaymentPreference : " + paymentPref, module);
}
if (paymentList != null) {
Iterator pi = paymentList.iterator();
while (pi.hasNext()) {
GenericValue pay = (GenericValue) pi.next();
pay.set("statusId", "PMNT_CANCELLED");
try {
pay.store();
} catch (GenericEntityException e) {
Debug.logError(e, "Unable to store Payment : " + pay, module);
}
}
}
} else {
Debug.logError("Release failed for pref : " + paymentPref, module);
}
}
result = ServiceUtil.returnSuccess();
if (finished.size() == paymentPrefs.size()) {
result.put("processResult", "COMPLETE");
} else {
result.put("processResult", "FAILED");
}
return result;
}
/**
* Captures payments through service calls to the defined processing service for the ProductStore/PaymentMethodType
* @return COMPLETE|FAILED|ERROR for complete processing of ALL payment methods.
*/
public static Map capturePaymentsByInvoice(DispatchContext dctx, Map context) {
GenericDelegator delegator = dctx.getDelegator();
LocalDispatcher dispatcher = dctx.getDispatcher();
GenericValue userLogin = (GenericValue) context.get("userLogin");
String invoiceId = (String) context.get("invoiceId");
// lookup the invoice
GenericValue invoice = null;
try {
invoice = delegator.findByPrimaryKey("Invoice", UtilMisc.toMap("invoiceId", invoiceId));
} catch (GenericEntityException e) {
Debug.logError(e, "Trouble looking up Invoice #" + invoiceId, module);
return ServiceUtil.returnError("Trouble looking up Invoice #" + invoiceId);
}
if (invoice == null) {
Debug.logError("Could not locate invoice #" + invoiceId, module);
return ServiceUtil.returnError("Could not locate invoice #" + invoiceId);
}
// get the OrderItemBilling records for this invoice
List orderItemBillings = null;
try {
orderItemBillings = invoice.getRelated("OrderItemBilling");
} catch (GenericEntityException e) {
Debug.logError("Trouble getting OrderItemBilling(s) from Invoice #" + invoiceId, module);
return ServiceUtil.returnError("Trouble getting OrderItemBilling(s) from Invoice #" + invoiceId);
}
// check for an associated billing account
String billingAccountId = invoice.getString("billingAccountId");
// make sure they are all for the same order
String testOrderId = null;
boolean allSameOrder = true;
if (orderItemBillings != null) {
Iterator oii = orderItemBillings.iterator();
while (oii.hasNext()) {
GenericValue oib = (GenericValue) oii.next();
String orderId = oib.getString("orderId");
if (testOrderId == null) {
testOrderId = orderId;
} else {
if (!orderId.equals(testOrderId)) {
allSameOrder = false;
break;
}
}
}
}
if (testOrderId == null || !allSameOrder) {
Debug.logWarning("Attempt to settle Invoice #" + invoiceId + " which contained none/multiple orders", module);
return ServiceUtil.returnSuccess();
}
// get the invoice amount (amount to bill)
double invoiceTotal = InvoiceWorker.getInvoiceTotal(invoice);
//Debug.logInfo("Invoice total: " + invoiceTotal, module);
// now capture the order
Map serviceContext = UtilMisc.toMap("userLogin", userLogin, "orderId", testOrderId, "invoiceId", invoiceId, "captureAmount", new Double(invoiceTotal));
if (UtilValidate.isNotEmpty(billingAccountId)) {
serviceContext.put("billingAccountId", billingAccountId);
}
try {
return dispatcher.runSync("captureOrderPayments", serviceContext);
} catch (GenericServiceException e) {
Debug.logError(e, "Trouble running captureOrderPayments service", module);
return ServiceUtil.returnError("Trouble running captureOrderPayments service");
}
}
/**
* Captures payments through service calls to the defined processing service for the ProductStore/PaymentMethodType
* @return COMPLETE|FAILED|ERROR for complete processing of ALL payment methods.
*/
public static Map captureOrderPayments(DispatchContext dctx, Map context) {
GenericDelegator delegator = dctx.getDelegator();
LocalDispatcher dispatcher = dctx.getDispatcher();
GenericValue userLogin = (GenericValue) context.get("userLogin");
String orderId = (String) context.get("orderId");
String invoiceId = (String) context.get("invoiceId");
String billingAccountId = (String) context.get("billingAccountId");
Double captureAmount = (Double) context.get("captureAmount");
Map result = new HashMap();
// get the order header and payment preferences
GenericValue orderHeader = null;
List paymentPrefs = null;
try {
orderHeader = delegator.findByPrimaryKey("OrderHeader", UtilMisc.toMap("orderId", orderId));
// get the payment prefs
Map lookupMap = UtilMisc.toMap("orderId", orderId, "statusId", "PAYMENT_AUTHORIZED");
List orderList = UtilMisc.toList("-authAmount");
paymentPrefs = delegator.findByAnd("OrderPaymentPreference", lookupMap, orderList);
} catch (GenericEntityException gee) {
Debug.logError(gee, "Problems getting entity record(s), see stack trace", module);
result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR);
result.put(ModelService.ERROR_MESSAGE, "ERROR: Could not get order information (" + gee.getMessage() + ").");
return result;
}
// error if no order was found
if (orderHeader == null) {
return ServiceUtil.returnError("Could not find OrderHeader with orderId: " + orderId + "; not processing payments.");
}
// return complete if no payment prefs were found
if (paymentPrefs == null || paymentPrefs.size() == 0) {
Debug.logWarning("No orderPaymentPreferences available to capture", module);
result.put("processResult", "COMPLETE");
result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS);
return result;
}
OrderReadHelper orh = new OrderReadHelper(orderHeader);
double orderTotal = orh.getOrderGrandTotal();
double totalPayments = PaymentWorker.getPaymentsTotal(orh.getOrderPayments());
double remainingTotal = orderTotal - totalPayments;
Debug.logInfo("Remaining Total: " + remainingTotal, module);
// re-format the remaining total
String currencyFormat = UtilProperties.getPropertyValue("general.properties", "currency.decimal.format", "##0.00");
DecimalFormat formatter = new DecimalFormat(currencyFormat);
String remainingTotalString = formatter.format(remainingTotal);
try {
Number remaining = formatter.parse(remainingTotalString);
if (remaining != null) {
remainingTotal = remaining.doubleValue();
}
} catch (ParseException e) {
Debug.logError(e, "Problem getting parsed remaining total", module);
return ServiceUtil.returnError("ERROR: Cannot parse grand total from formatted string; see logs");
}
if (captureAmount == null) {
captureAmount = new Double(remainingTotal);
}
//Debug.logInfo("Formatted Remaining total : " + remainingTotal, module);
double amountToCapture = captureAmount.doubleValue();
//Debug.logInfo("Expected Capture Amount : " + amountToCapture, module);
// if we have a billing account get balance/limit and available
GenericValue billingAccount = null;
Double billingAccountBalance = null;
Double billingAccountAvail = null;
Map billingAccountInfo = null;
if (UtilValidate.isNotEmpty(billingAccountId)) {
try {
billingAccountInfo = dispatcher.runSync("calcBillingAccountBalance", UtilMisc.toMap("billingAccountId", billingAccountId));
} catch (GenericServiceException e) {
Debug.logError(e, "Unable to get billing account information for #" + billingAccountId, module);
}
}
if (billingAccountInfo != null) {
billingAccount = (GenericValue) billingAccountInfo.get("billingAccount");
billingAccountBalance = (Double) billingAccountInfo.get("accountBalance");
}
if (billingAccount != null && billingAccountBalance != null) {
Double accountLimit = billingAccount.getDouble("accountLimit");
if (accountLimit == null) {
accountLimit = new Double(0.00);
}
billingAccountAvail = new Double(accountLimit.doubleValue() - billingAccountBalance.doubleValue());
}
// iterate over the prefs and capture each one until we meet our total
List finished = new ArrayList();
Iterator payments = paymentPrefs.iterator();
while (payments.hasNext()) {
GenericValue paymentPref = (GenericValue) payments.next();
GenericValue authTrans = getAuthTransaction(paymentPref);
if (authTrans == null) {
continue;
}
Double authAmount = authTrans.getDouble("amount");
if (authAmount == null) authAmount = new Double(0.00);
if (authAmount.doubleValue() == 0.00) {
// nothing to capture
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -