-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrocador
More file actions
158 lines (148 loc) · 5.1 KB
/
Copy pathtrocador
File metadata and controls
158 lines (148 loc) · 5.1 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/**
* Prepaid Card Purchase App - Trocador
*
* This app demonstrates how to integrate a prepaid card purchasing system within the Intersend platform.
* It showcases:
* 1. How to fetch and display available prepaid card offers
* 2. How to create and confirm transactions for purchasing prepaid cards
* 3. How to handle the transaction flow using Intersend's API
* 4. How to display transaction history and other relevant information
*
* Architecture Overview:
* - The app runs within the Intersend platform
* - It uses Intersend's API for authentication, fetching offers, and processing transactions
* - The app uses generalized endpoints that can be applied to various types of applications
*
* Note: This example uses environment variables for sensitive data.
* Ensure you have set up your .env file with the necessary values.
*/
import React, { useState, useEffect } from 'react';
import axios from 'axios';
const PrepaidCardApp = () => {
const [offers, setOffers] = useState([]);
const [selectedAmount, setSelectedAmount] = useState('');
const [email, setEmail] = useState('');
const [transactionData, setTransactionData] = useState(null);
const [transactions, setTransactions] = useState([]);
const [isLoading, setIsLoading] = useState(false);
const [errorMessage, setErrorMessage] = useState('');
const [authToken, setAuthToken] = useState(null);
useEffect(() => {
initializeApp();
fetchOffers();
fetchTransactionHistory();
}, []);
const initializeApp = async () => {
try {
const response = await axios.post('https://node.intersend.io/app/initialize', {
appId: process.env.REACT_APP_PREPAID_CARD_APP_ID
});
setAuthToken(response.data.token);
} catch (error) {
console.error('Failed to initialize app:', error);
}
};
const fetchOffers = async () => {
try {
const response = await axios.get('https://node.intersend.io/app/offers', {
headers: { Authorization: `Bearer ${authToken}` }
});
setOffers(response.data);
} catch (error) {
console.error('Error fetching offers:', error);
setErrorMessage('Failed to fetch offers. Please try again later.');
}
};
const fetchTransactionHistory = async () => {
try {
const response = await axios.get('https://node.intersend.io/app/transactions', {
headers: { Authorization: `Bearer ${authToken}` }
});
setTransactions(response.data);
} catch (error) {
console.error('Error fetching transaction history:', error);
}
};
const createTransaction = async () => {
setIsLoading(true);
setErrorMessage('');
try {
const response = await axios.post('https://node.intersend.io/app/transaction/create', {
amount: selectedAmount,
email: email,
type: 'prepaid_card_purchase'
}, {
headers: { Authorization: `Bearer ${authToken}` }
});
setTransactionData(response.data);
setIsLoading(false);
} catch (error) {
console.error('Error creating transaction:', error);
setErrorMessage('Failed to create transaction. Please try again.');
setIsLoading(false);
}
};
const confirmTransaction = async () => {
setIsLoading(true);
setErrorMessage('');
try {
const response = await axios.post('https://node.intersend.io/app/transaction/confirm', {
transactionId: transactionData.id
}, {
headers: { Authorization: `Bearer ${authToken}` }
});
// Handle successful transaction
fetchTransactionHistory(); // Refresh transaction history
setIsLoading(false);
} catch (error) {
console.error('Error confirming transaction:', error);
setErrorMessage('Failed to confirm transaction. Please try again.');
setIsLoading(false);
}
};
return (
<div>
<h1>Buy Prepaid Card</h1>
<input
type="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
placeholder="Enter your email"
/>
<select
value={selectedAmount}
onChange={(e) => setSelectedAmount(e.target.value)}
>
<option value="">Select amount</option>
{offers.map((offer, index) => (
<option key={index} value={offer.amount}>
{offer.amount} USD
</option>
))}
</select>
<button onClick={createTransaction} disabled={isLoading}>
{isLoading ? 'Processing...' : 'Buy Card'}
</button>
{errorMessage && <p>{errorMessage}</p>}
{transactionData && (
<div>
<h2>Confirm Your Purchase</h2>
<p>Card Value: ${transactionData.cardValue}</p>
<p>Amount to Pay: ${transactionData.amountToPay}</p>
<button onClick={confirmTransaction} disabled={isLoading}>
Confirm Purchase
</button>
</div>
)}
<h2>Previous Purchases</h2>
{transactions.map((transaction, index) => (
<div key={index}>
<p>Date: {transaction.date}</p>
<p>Amount: ${transaction.amount}</p>
<p>Status: {transaction.status}</p>
</div>
))}
</div>
);
};
export default PrepaidCardApp;