-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy path30_days_money_back_guarantee.ds
135 lines (124 loc) · 4.39 KB
/
30_days_money_back_guarantee.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
/*
Entity - subscription
Triggers - Subscription Cancelled.
Work flow - When subscription Cancelled with in the specified days, then amount will be refunded.
Change this {{daysToConsider}} variable as per your need.
Place your {{connection name}} in "YOUR_CONNECTION_NAME" variables.
* This custom function refunds the payment done for a subscription, if it is cancelled within 30 days.
* When a subscription is cancelled, the last sent invoice details are retrieved.
* If the invoice is paid, the amount is refunded (in case of online payment) or a credit note is raised (in case of offline payment).
* If the invoice is unpaid, it is marked as void.
*/
// Change the daysToConsider value according to your need.
dayToConsider = 30;
createdDate = subscriptions.get("activated_at");
cancelledDate = subscriptions.get("cancelled_at");
totalDays = days360(createdDate,cancelledDate);
if(totalDays > dayToConsider)
{
//If the subscription is active for more than (daysToConsider) 30 days, we simnply exit the function.
return;
}
domain = "https://www.zohoapis.com/billing/v1";
organizationID = organization.get("organization_id");
invoiceID = subscriptions.get("child_invoice_id");
if(invoiceID.isEmpty())
{
return;
}
url = domain + "/invoices/" + invoiceID + "?organization_id=" + organizationID;
request = "retriving invoice";
response = invokeUrl [
url : url
type : GET
connection : "YOUR_CONNECTION_NAME"
];
// Retrieving the details of the last paid invoice.
if(response.get("code") != 0)
{
errorMessage = response.get("message");
sendmail
[
from :zoho.adminuserid
to :zoho.adminuserid
subject :"Error occured in Auto Cancellation custom function while " + request
message :"<b>Affected url :</b><br>" + url + "<br><b>Error Message</b><br>" + errorMessage
]
return;
}
invoiceMap = response.get("invoice");
invoiceStatus = invoiceMap.get("status");
paramsMap = Map();
// Handling the use case for paid invoice.
if(invoiceStatus == "paid")
{
payments = invoiceMap.get("payments").get(0);
paidAmount = payments.get("amount");
isOnlinePayment = payments.get("gateway_transaction_id");
if(isOnlinePayment == "")
{
// For offline payment, we create a credit note for the customer.
customerID = invoiceMap.get("customer_id");
invoiceItems = invoiceMap.get("invoice_items").get(0);
planCode = invoiceItems.get("code");
creditNoteDate = zoho.currentdate.toString("yyyy-MM-dd");
creditNoteItemList = list();
creditNoteItem = Map();
creditNoteItem.put("code",planCode);
creditNoteItem.put("quantity","1");
creditNoteItem.put("price",paidAmount);
creditNoteItemList.add(creditNoteItem);
paramsMap.put("customer_id",customerID);
paramsMap.put("date",creditNoteDate);
paramsMap.put("creditnote_items",creditNoteItemList);
url = domain + "/creditnotes?organization_id=" + organizationID;
request = "creating Credit Note";
response = invokeUrl [
url : url
type : POST
parameters : paramsMap.toString()
connection : "YOUR_CONNECTION_NAME"
];
}
else
{
// For online payment, we intiate a refund for the paid amount.
paymentID = payments.get("payment_id");
refundDescription = "Subscription is cancelled and amount is auto-refunded";
paramsMap.put("amount",paidAmount);
paramsMap.put("description",refundDescription);
url = domain + "/payments/" + paymentID + "/refunds?organization_id=" + organizationID;
request = "refunding payment";
response = invokeUrl [
url : url
type : POST
parameters : paramsMap.toString()
connection : "YOUR_CONNECTION_NAME"
];
}
}
// Handling the use case for unpaid invoice, by simply voiding it.
else if(invoiceStatus == "sent" || invoiceID == "overdue")
{
paramsMap.put("reason","subscription cancelled");
url = domain + "/invoices/" + invoiceID + "/void?organization_id=" + organizationID;
request = "voiding invoice";
response = invokeUrl [
url : url
type : POST
parameters : paramsMap.toString()
connection : "YOUR_CONNECTION_NAME"
];
}
// If error occurs in any of the API requests, an email will be sent to the admin user with the request URL and it's error.
if(response.get("code") != 0)
{
errorMessage = response.get("message");
sendmail
[
from :zoho.adminuserid
to :zoho.adminuserid
subject :"Error occured in Auto Cancellation custom function while " + request
message :"<b>Affected url :</b><br>" + url + "<br><b>Error Message</b><br>" + errorMessage
]
}