-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaypal-checkout.js
More file actions
123 lines (108 loc) · 4.56 KB
/
paypal-checkout.js
File metadata and controls
123 lines (108 loc) · 4.56 KB
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
/**
* PayPal Checkout Integration for InsightLearn
* Handles PayPal button rendering and payment processing
*/
window.initializePayPalButton = function (amount, currency, dotNetReference) {
console.log('[PayPal] Initializing PayPal button', { amount, currency });
// Ensure PayPal SDK is loaded
if (typeof paypal === 'undefined') {
console.error('[PayPal] PayPal SDK not loaded');
dotNetReference.invokeMethodAsync('OnPayPalError', 'PayPal SDK not loaded. Please refresh the page.');
return;
}
// Clear existing button
const container = document.getElementById('paypal-button-container');
if (!container) {
console.error('[PayPal] Button container not found');
return;
}
container.innerHTML = '';
// Render PayPal button
paypal.Buttons({
style: {
layout: 'vertical',
color: 'gold',
shape: 'rect',
label: 'paypal',
height: 50,
tagline: false
},
// Create order on PayPal
createOrder: function (data, actions) {
console.log('[PayPal] Creating order', { amount, currency });
return actions.order.create({
purchase_units: [{
amount: {
currency_code: currency.toUpperCase(),
value: amount
},
description: 'InsightLearn Course Purchase'
}],
application_context: {
shipping_preference: 'NO_SHIPPING', // Digital goods - no shipping
brand_name: 'InsightLearn',
locale: 'en-US',
landing_page: 'BILLING', // Go directly to billing page
user_action: 'PAY_NOW' // Show "Pay Now" button instead of "Continue"
}
});
},
// On payment approval
onApprove: function (data, actions) {
console.log('[PayPal] Payment approved', data);
// Show processing spinner
container.innerHTML = `
<div style="text-align: center; padding: 2rem;">
<div class="spinner-border text-primary" role="status">
<span class="visually-hidden">Processing...</span>
</div>
<p class="mt-3 text-muted">Completing your purchase...</p>
</div>
`;
// Capture the payment
return actions.order.capture().then(function (details) {
console.log('[PayPal] Payment captured successfully', details);
// Call Blazor method to handle successful payment
dotNetReference.invokeMethodAsync('OnPayPalApprove', data.orderID)
.catch(function (error) {
console.error('[PayPal] Error calling OnPayPalApprove:', error);
dotNetReference.invokeMethodAsync('OnPayPalError', 'Failed to process payment. Please contact support.');
});
}).catch(function (error) {
console.error('[PayPal] Error capturing payment:', error);
dotNetReference.invokeMethodAsync('OnPayPalError', 'Payment capture failed. Please try again.');
});
},
// On payment error
onError: function (err) {
console.error('[PayPal] Payment error:', err);
let errorMessage = 'An error occurred during payment. Please try again.';
if (err && err.message) {
errorMessage = err.message;
}
dotNetReference.invokeMethodAsync('OnPayPalError', errorMessage);
},
// On payment cancel
onCancel: function (data) {
console.log('[PayPal] Payment cancelled by user', data);
dotNetReference.invokeMethodAsync('OnPayPalCancel');
}
}).render('#paypal-button-container')
.then(function () {
console.log('[PayPal] Button rendered successfully');
})
.catch(function (error) {
console.error('[PayPal] Error rendering button:', error);
dotNetReference.invokeMethodAsync('OnPayPalError', 'Failed to load PayPal button. Please refresh the page.');
});
};
/**
* Cleanup PayPal button (call when component is disposed)
*/
window.cleanupPayPalButton = function () {
const container = document.getElementById('paypal-button-container');
if (container) {
container.innerHTML = '';
console.log('[PayPal] Button cleanup completed');
}
};