This repository was archived by the owner on Feb 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadd_payment_links.js
76 lines (61 loc) · 2.02 KB
/
add_payment_links.js
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
cj(function ($) {
'use strict';
$('.transaction-id').each(
(i, el) => {
linkToStripe($, el);
});
// Select the node that will be observed for mutations
const targetNode = document.getElementsByTagName('body')[0]; // document.getElementsByClassName('CRM_Contribute_Form_Search')[0];
listenForPayments($, targetNode);
});
function linkToStripe($, el) {
var stripe_section = '';
var text = $(el).text();
var link = "";
if (text.startsWith('ch_')) {
stripe_section = 'payments';
link = "Payment";
} else if (text.startsWith('pi_')) {
stripe_section = 'payments';
link = "Payment";
} else if (text.startsWith('sub_')) {
stripe_section = 'subscriptions'
link = "Subscription";
} else if (text.startsWith('in_')) {
stripe_section = 'invoices'
link = "Invoice";
} else {
return
}
$(el).html(
'<a target=_new href="https://dashboard.stripe.com/'
+ stripe_section
+ '/'
+ $(el).text()
+ '">View '
+ link
+ '</a>'
);
}
function listenForPayments(jQuery, targetNode) {
// Options for the observer (which mutations to observe)
const config = { attributes: true, childList: true, subtree: true };
const callback = function (mutationsList, observer) {
for (const mutation of mutationsList) {
if (mutation.type === 'childList') {
mutation.addedNodes.forEach(node => {
jQuery(node).find('.transaction-id').each(
(i, el) => {
linkToStripe(jQuery, el);
});
});
}
console.log("heard " + mutation.type);
}
};
// Create an observer instance linked to the callback function
const observer = new MutationObserver(callback);
// Start observing the target node for configured mutations
observer.observe(targetNode, config);
return observer;
}