-
Notifications
You must be signed in to change notification settings - Fork 69
/
adyenCancelOrRefund.ds
140 lines (114 loc) · 4.16 KB
/
adyenCancelOrRefund.ds
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**
* Call the Adyen API to cancel or refund order payment
*
* @input Order : dw.order.Order
*
* @output Decision : String
*
*/
importPackage( dw.system );
importPackage( dw.order );
importPackage( dw.util );
importPackage( dw.net);
importPackage( dw.svc );
//script include
var AdyenHelper = require ("int_adyen_overlay/cartridge/scripts/util/AdyenHelper");
function execute( args : PipelineDictionary ) : Number
{
var decision = cancelOrRefund(args.Order);
if (decision == PIPELET_ERROR){
args.Decision = "ERROR";
return PIPELET_ERROR;
}
args.Decision = decision;
return PIPELET_NEXT;
}
function cancelOrRefund( order : Order ) : Number
{
// Check the input parameters
if (order == null) {
Logger.getLogger("Adyen").fatal("Cancellation or refund of order payment has failed. No order was provided.");
return PIPELET_ERROR;
}
// Get orderNo which is needed for some functions (error handling)
var orderNo : String = order.getOrderNo();
var decision = "ERROR";
// Error handling configuration
var fatalErrMsgPrefix : String = "Cancellation or refund of payment for order #" + orderNo + " has failed. ";
// Adyen configuration
var merchantAccount : String = Site.getCurrent().getCustomPreferenceValue("Adyen_merchantCode");
// Check the configuration
if (merchantAccount == null) {
Logger.getLogger("Adyen").fatal(fatalErrMsgPrefix + "merchantAccount is not set.");
return PIPELET_ERROR;
}
if (endpoint == null) {
Logger.getLogger("Adyen").fatal(fatalErrMsgPrefix + "Pal service URL is not set.");
return PIPELET_ERROR;
}
// Get order values needed for the API call
var pspReference : String = order.custom.Adyen_pspReference;
if (pspReference == null) {
Logger.getLogger("Adyen").fatal(fatalErrMsgPrefix + "PSP reference is not set.");
return PIPELET_ERROR;
}
try {
// Send a request
var params="action=" + "Payment.cancelOrRefund" +
"&modificationRequest.merchantAccount=" + encodeURIComponent(merchantAccount) +
"&modificationRequest.originalReference=" + encodeURIComponent(pspReference);
var callResult = null;
//var service : Service = ServiceRegistry.get("adyen.http.payment.send");
var service = AdyenHelper.getService(AdyenHelper.SERVICE.SEND);
if (service == null) {
return PIPELET_ERROR;
}
var resultObject = null;
service.addHeader("Content-type","application/x-www-form-URLencoded");
service.addHeader("charset", "UTF-8");
callResult = service.call(params);
if (callResult.isOk() == false){
Logger.error("Adyen: Call error code" + callResult.getError().toString() + " Error => ResponseStatus: " + callResult.getStatus() + " | ResponseErrorText: " + callResult.getErrorMessage() + " | ResponseText: " + callResult.getMsg());
return PIPELET_ERROR;
}
resultObject = callResult.object;
var result : String = resultObject.getText();
// Parse the response
if (!empty(result)) {
if (result.indexOf("cancelOrRefund-received") != -1) {
// Define the result of script execution
decision = "SUCCESS";
// Update the order
order.custom.Adyen_eventCode = "CANCELLATION OR REFUND";
/*
if (order.paymentStatus != Order.PAYMENT_STATUS_NOTPAID) {
order.setPaymentStatus(Order.PAYMENT_STATUS_NOTPAID);
}
*/
// Log the result of operation
Logger.getLogger("Adyen").info("Payment modification result for order #" + orderNo + ": Cancellation Or Refund");
} else {
// Define the result of script execution
decision = "REFUSED";
// Update the order
order.custom.Adyen_eventCode = "CANCELLATION OR REFUND REFUSED";
// Log the result of operation
Logger.getLogger("Adyen").info("Payment modification result for order #" + orderNo + ": Cancellation Or Refund Refused");
}
} else {
// Log the error and exit
Logger.getLogger("Adyen").fatal(fatalErrMsgPrefix + "The call to Adyen API did not return any result.");
return PIPELET_ERROR;
}
}
catch (e) {
// Log the error and exit
Logger.getLogger("Adyen").fatal(fatalErrMsgPrefix + "An error occurred during the call to Adyen API.");
return PIPELET_ERROR;
}
return decision;
}
module.exports = {
'execute': execute,
'cancelOrRefund': cancelOrRefund
}