Skip to content

Commit

Permalink
Merge pull request #34 from Chimoney/dev
Browse files Browse the repository at this point in the history
Update to use interledger payment pointers
  • Loading branch information
uchibeke authored Oct 23, 2024
2 parents d1f7b0a + 8bbd7dd commit f0730dc
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 41 deletions.
44 changes: 26 additions & 18 deletions open-giving/src/components/DonationForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
ListItemText,
TextField,
Typography,
Grid,
} from '@mui/material';
import React, { useEffect } from 'react';

Expand Down Expand Up @@ -160,24 +161,31 @@ const DonationForm = React.memo(
handleDonateClick();
}}
>
<TextField
label="Amount"
type="number"
value={donationAmount}
onChange={(e) => setDonationAmount(e.target.value)}
fullWidth
margin="normal"
required
/>
<TextField
label="Email"
type="email"
value={payerEmail}
onChange={(e) => setPayerEmail(e.target.value)}
fullWidth
margin="normal"
required
/>
<Grid container spacing={2}>
<Grid item xs={12} md={4}>
<TextField
label="Amount"
type="number"
value={donationAmount}
onChange={(e) => setDonationAmount(e.target.value)}
fullWidth
margin="normal"
required
/>
</Grid>
<Grid item xs={12} md={8}>
<TextField
label="Email"
type="email"
value={payerEmail}
onChange={(e) => setPayerEmail(e.target.value)}
fullWidth
margin="normal"
required
/>
</Grid>
</Grid>

{paymentLink && parseFloat(donationAmount) === paymentAmount ? (
<Box>
<Button
Expand Down
29 changes: 21 additions & 8 deletions open-giving/src/hooks/useDonation.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@ const useDonation = (method, setSnackbarMessage, setSnackbarOpen) => {
typeof paymentID === 'string' && paymentID.length > 0
? paymentID
: method?.paymentID;
console.log('paymentID', paymentID);
const walletID =
paymentID && paymentID.test
? useTestPaymentID
? paymentID.test
: paymentID.production
: paymentID;

paymentID =
paymentID && paymentID.test && useTestPaymentID
? paymentID.test
: paymentID.production || paymentID;

if (method.type !== 'chimoney') {
try {
Expand Down Expand Up @@ -60,6 +58,21 @@ const useDonation = (method, setSnackbarMessage, setSnackbarOpen) => {
return;
}

const chimoneyInterledgerDomains = [
'ilp.chimoney.com',

'ilp-sandbox.chimoney.com',
];
const settlementData = chimoneyInterledgerDomains.some((domain) =>
paymentID?.includes(domain)
)
? {
interledgerWalletAddress: paymentID,
}
: {
walletID: paymentID,
};

const response = await fetch('/api/chimoney-donate', {
method: 'POST',
headers: {
Expand All @@ -71,7 +84,7 @@ const useDonation = (method, setSnackbarMessage, setSnackbarOpen) => {
payerEmail,
redirect_url: `${window.location.origin}/donation-success`,
useTestPaymentID,
walletID,
...settlementData,
}),
});

Expand Down
30 changes: 20 additions & 10 deletions open-giving/src/pages/api/chimoney-donate.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
export default async function handler(req, res) {
const dev = process.env.NODE_ENV == 'development';

const getPaymentID = (paymentID, useTestPaymentID) => {
return useTestPaymentID && typeof paymentID?.test !== 'undefined'
? paymentID?.test
: typeof paymentID?.production !== 'undefined'
? paymentID?.production
: paymentID;
};

if (req.method === 'POST') {
const {
amount,
currency = 'USD',
payerEmail,
walletID: walletIDFromBody,
walletID,
redirect_url,
useTestPaymentID,
interledgerWalletAddress,
} = req.body;
const apiKEY = process.env.CHIMONEY_API_SECRET;
const apiKEYTest = process.env.CHIMONEY_API_SECRET_TEST;

try {
const walletID =
useTestPaymentID && typeof walletIDFromBody?.test !== 'undefined'
? walletIDFromBody?.test
: typeof walletIDFromBody?.production !== 'undefined'
? walletIDFromBody?.production
: walletIDFromBody;
const redeemData = {};
if (walletID) {
redeemData.walletID = getPaymentID(walletID, useTestPaymentID);
} else if (interledgerWalletAddress) {
redeemData.interledgerWalletAddress = getPaymentID(
interledgerWalletAddress,
useTestPaymentID
);
}

if (!walletID) {
res.status(400).json({
Expand Down Expand Up @@ -55,9 +67,7 @@ export default async function handler(req, res) {
paymentMethod: 'chimoney',
type: 'donation',
redirect_url,
redeemData: {
walletID,
},
redeemData,
}),
};
const response = await fetch(`${server}/payment/initiate`, config);
Expand Down
10 changes: 5 additions & 5 deletions src/non-profits-data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const nonProfits: readonly NonProfit[] = [
type: 'chimoney',
paymentID: {
test: '',
production: 'PR3tl5CsAhQWW19sQuPH',
production: 'https://ilp.chimoney.com/shecodeafrica',
},
donationHandler: 'chimoneyDonation',
},
Expand Down Expand Up @@ -146,7 +146,7 @@ const nonProfits: readonly NonProfit[] = [
type: 'chimoney',
paymentID: {
test: '',
production: 'uEBebfE7ISin8JkilFRq',
production: 'https://ilp.chimoney.com/devcareer',
},
donationHandler: 'chimoneyDonation',
},
Expand Down Expand Up @@ -177,8 +177,8 @@ const nonProfits: readonly NonProfit[] = [
{
type: 'chimoney',
paymentID: {
test: '69b9MVYsDMIWRbj5Oja5',
production: 'CoEp7GHW62Q2nQ6PbO8i',
test: 'https://ilp-sandbox.chimoney.com/africahacks',
production: 'https://ilp.chimoney.com/worldinnovationleague',
},
donationHandler: 'chimoneyDonation',
},
Expand Down Expand Up @@ -270,7 +270,7 @@ const nonProfits: readonly NonProfit[] = [
type: 'chimoney',
paymentID: {
test: '',
production: 'uFm29ZPvzBKABthw7Gi',
production: 'https://ilp.chimoney.com/41703163_1729687433664',
},
donationHandler: 'chimoneyDonation',
},
Expand Down

0 comments on commit f0730dc

Please sign in to comment.