📄 paymentgatewayservices.java
字号:
Debug.logInfo("Invoice ID: " + invoiceId, module);
if (payTo == null)
payTo = "Company";
String serviceType = fromAuth ? "AUTH" : CAPTURE_SERVICE_TYPE;
if (serviceType.equals("AUTH")) {
serviceType = paymentPreference.getString("statusId").equals("PAYMENT_NOT_AUTH") ? AUTH_SERVICE_TYPE : REAUTH_SERVICE_TYPE;
}
if (result != null && captureResult.booleanValue()) {
// create the PaymentGatewayResponse record
String responseId = delegator.getNextSeqId("PaymentGatewayResponse").toString();
GenericValue response = delegator.makeValue("PaymentGatewayResponse", null);
response.set("paymentGatewayResponseId", responseId);
response.set("paymentServiceTypeEnumId", serviceType);
response.set("orderPaymentPreferenceId", paymentPreference.get("orderPaymentPreferenceId"));
response.set("paymentMethodTypeId", paymentPreference.get("paymentMethodTypeId"));
response.set("paymentMethodId", paymentPreference.get("paymentMethodId"));
if (result.get("authRefNum") != null) {
response.set("subReference", result.get("authRefNum"));
}
// set the capture info
response.set("amount", amount);
response.set("referenceNum", result.get("captureRefNum"));
response.set("gatewayCode", result.get("captureCode"));
response.set("gatewayFlag", result.get("captureFlag"));
response.set("gatewayMessage", result.get("captureMessage"));
response.set("transactionDate", UtilDateTime.nowTimestamp());
delegator.create(response);
String orderId = paymentPreference.getString("orderId");
GenericValue orderRole = EntityUtil.getFirst(delegator.findByAnd("OrderRole",
UtilMisc.toMap("orderId", orderId, "roleTypeId", "BILL_TO_CUSTOMER")));
Map paymentCtx = UtilMisc.toMap("paymentTypeId", "RECEIPT");
paymentCtx.put("paymentMethodTypeId", paymentPreference.get("paymentMethodTypeId"));
paymentCtx.put("paymentMethodId", paymentPreference.get("paymentMethodId"));
paymentCtx.put("paymentGatewayResponseId", responseId);
paymentCtx.put("partyIdTo", payTo);
paymentCtx.put("partyIdFrom", orderRole.get("partyId"));
paymentCtx.put("statusId", "PMNT_RECEIVED");
paymentCtx.put("paymentPreferenceId", paymentPreference.get("orderPaymentPreferenceId"));
paymentCtx.put("amount", amount);
paymentCtx.put("userLogin", userLogin);
paymentCtx.put("paymentRefNum", result.get("captureRefNum"));
Map payRes = dispatcher.runSync("createPayment", paymentCtx);
String paymentId = (String) payRes.get("paymentId");
paymentPreference.set("statusId", "PAYMENT_SETTLED");
paymentPreference.store();
// create the PaymentApplication if invoiceId is available
if (invoiceId != null) {
Debug.logInfo("Processing Invoice #" + invoiceId, module);
Map paCtx = UtilMisc.toMap("paymentId", paymentId, "invoiceId", invoiceId);
paCtx.put("amountApplied", result.get("captureAmount"));
paCtx.put("userLogin", userLogin);
Map paRes = dispatcher.runSync("createPaymentApplication", paCtx);
}
} else if (result != null && !captureResult.booleanValue()) {
// problem with the capture lets get some needed info
OrderReadHelper orh = null;
try {
GenericValue orderHeader = paymentPreference.getRelatedOne("OrderHeader");
if (orderHeader != null)
orh = new OrderReadHelper(orderHeader);
} catch (GenericEntityException e) {
Debug.logError(e, "Problems getting OrderHeader; cannot re-auth the payment", module);
}
if (orh != null) {
// first lets re-auth the card
Map authPayRes = authPayment(dispatcher, userLogin, orh, paymentPreference, amount.doubleValue(), true);
if (authPayRes != null) {
Boolean authResp = (Boolean) result.get("authResult");
Boolean capResp = (Boolean) result.get("captureResult");
if (authResp != null) {
processAuthResult(dctx, authPayRes, userLogin, paymentPreference, paymentSettings);
if (authResp.booleanValue()) {
// first make sure we didn't already capture - probably not
if (capResp != null && capResp.booleanValue()) {
processCaptureResult(dctx, result, userLogin, paymentPreference, paymentSettings);
} else {
// lets try to capture the funds now
Map capPayRes = capturePayment(dispatcher, userLogin, orh, paymentPreference, amount.doubleValue());
if (capPayRes != null) {
Boolean capPayResp = (Boolean) result.get("captureResult");
if (capPayResp != null && capPayResp.booleanValue()) {
// it was successful
processCaptureResult(dctx, result, userLogin, paymentPreference, paymentSettings);
} else {
// not successful; log it
Debug.logError("Capture of authorized payment failed: " + paymentPreference, module);
}
} else {
Debug.logError("Problems trying to capture payment (null result): " + paymentPreference, module);
}
}
} else {
Debug.logError("Payment authorization failed: " + paymentPreference, module);
}
} else {
Debug.logError("Payment authorization failed (null result): " + paymentPreference, module);
}
} else {
Debug.logError("Problems trying to re-authorize the payment (null result): " + paymentPreference, module);
}
} else {
Debug.logError("Null OrderReadHelper cannot process", module);
}
} else {
Debug.logError("Result pass is null, no capture available", module);
}
}
public static Map refundPayment(DispatchContext dctx, Map context) {
GenericDelegator delegator = dctx.getDelegator();
LocalDispatcher dispatcher = dctx.getDispatcher();
GenericValue userLogin = (GenericValue) context.get("userLogin");
GenericValue paymentPref = (GenericValue) context.get("orderPaymentPreference");
Double refundAmount = (Double) context.get("refundAmount");
GenericValue orderHeader = null;
try {
orderHeader = paymentPref.getRelatedOne("OrderHeader");
} catch (GenericEntityException e) {
Debug.logError(e, "Cannot get OrderHeader from OrderPaymentPreference", module);
return ServiceUtil.returnError("Problems getting OrderHeader from OrderPaymentPreference: " + e.getMessage());
}
OrderReadHelper orh = new OrderReadHelper(orderHeader);
GenericValue paymentSettings = null;
if (orderHeader != null) {
paymentSettings = getPaymentSettings(orderHeader, paymentPref, REFUND_SERVICE_TYPE, false);
}
if (paymentSettings != null) {
String paymentConfig = paymentSettings.getString("paymentPropertiesPath");
String serviceName = paymentSettings.getString("paymentService");
if (serviceName != null) {
Map serviceContext = new HashMap();
serviceContext.put("orderPaymentPreference", paymentPref);
serviceContext.put("paymentConfig", paymentConfig);
serviceContext.put("currency", orh.getCurrency());
// get the creditCard/address/email
String payToPartyId = null;
try {
payToPartyId = getBillingInformation(orh, paymentPref, new HashMap());
} catch (GenericEntityException e) {
Debug.logError(e, "Problems getting billing information", module);
return ServiceUtil.returnError("Problems getting billing information");
}
// format the price
String currencyFormat = UtilProperties.getPropertyValue("general.properties", "currency.decimal.format", "##0.00");
DecimalFormat formatter = new DecimalFormat(currencyFormat);
String amountString = formatter.format(refundAmount);
Double processAmount = null;
try {
processAmount = new Double(formatter.parse(amountString).doubleValue());
} catch (ParseException e) {
Debug.logError(e, "Problem parsing amount using DecimalFormat", module);
return ServiceUtil.returnError("Refund processor problems; see logs");
}
serviceContext.put("refundAmount", processAmount);
serviceContext.put("userLogin", userLogin);
// call the service
Map refundResponse = null;
try {
refundResponse = dispatcher.runSync(serviceName, serviceContext);
} catch (GenericServiceException e) {
Debug.logError(e, "Problem refunding payment through processor", module);
return ServiceUtil.returnError("Refund processor problems; see logs");
}
//Debug.log("Called Electronic Refund Service : " + refundResponse, module);
// get the pay-from party
if (paymentConfig == null || paymentConfig.length() == 0) {
paymentConfig = "payment.properties";
}
String payFromPartyId = getPayToPartyId(orderHeader);
// create the PaymentGatewayResponse record
String responseId = delegator.getNextSeqId("PaymentGatewayResponse").toString();
GenericValue response = delegator.makeValue("PaymentGatewayResponse", null);
response.set("paymentGatewayResponseId", responseId);
response.set("paymentServiceTypeEnumId", REFUND_SERVICE_TYPE);
response.set("orderPaymentPreferenceId", paymentPref.get("orderPaymentPreferenceId"));
response.set("paymentMethodTypeId", paymentPref.get("paymentMethodTypeId"));
response.set("paymentMethodId", paymentPref.get("paymentMethodId"));
// set the capture info
response.set("amount", refundResponse.get("refundAmount"));
response.set("referenceNum", refundResponse.get("refundRefNum"));
response.set("gatewayCode", refundResponse.get("refundCode"));
response.set("gatewayFlag", refundResponse.get("refundFlag"));
response.set("gatewayMessage", refundResponse.get("refundMessage"));
response.set("transactionDate", UtilDateTime.nowTimestamp());
try {
delegator.create(response);
} catch (GenericEntityException e) {
Debug.logError(e, module);
return ServiceUtil.returnError("Unable to create PaymentGatewayResponse record");
}
// handle the (reverse) payment
Boolean refundResult = (Boolean) refundResponse.get("refundResult");
if (refundResult != null && refundResult.booleanValue()) {
// create a payment record
Map paymentCtx = UtilMisc.toMap("paymentTypeId", "DISBURSEMENT");
paymentCtx.put("paymentMethodTypeId", paymentPref.get("paymentMethodTypeId"));
paymentCtx.put("paymentMethodId", paymentPref.get("paymentMethodId"));
paymentCtx.put("paymentGatewayResponseId", responseId);
paymentCtx.put("partyIdTo", payToPartyId);
paymentCtx.put("partyIdFrom", payFromPartyId);
paymentCtx.put("statusId", "PMNT_SENT");
paymentCtx.put("paymentPreferenceId", paymentPref.get("orderPaymentPreferenceId"));
paymentCtx.put("amount", refundResponse.get("refundAmount"));
paymentCtx.put("userLogin", userLogin);
paymentCtx.put("paymentRefNum", refundResponse.get("refundRefNum"));
paymentCtx.put("comments", "Refund");
String paymentId = null;
try {
Map payRes = dispatcher.runSync("createPayment", paymentCtx);
if (ModelService.RESPOND_ERROR.equals(payRes.get(ModelService.RESPONSE_MESSAGE))) {
return ServiceUtil.returnError((String) payRes.get(ModelService.ERROR_MESSAGE));
} else {
paymentId = (String) payRes.get("paymentId");
}
} catch (GenericServiceException e) {
Debug.logError(e, "Problem creating Payment", module);
return ServiceUtil.returnError("Problem creating Payment");
}
//Debug.log("Payment created : " + paymentId, module);
if (paymentId == null) {
return ServiceUtil.returnError("Create payment failed");
}
Map result = ServiceUtil.returnS
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -