Compare commits

...

3 Commits

Author SHA1 Message Date
Chukwuleta Tobechi
50dabd529f default redirect 2025-03-21 12:53:06 +01:00
Chukwuleta Tobechi
1f55f6614e old users compatibility 2025-03-21 12:37:04 +01:00
Chukwuleta Tobechi
a9a4e9f162 auto-create invoice 2025-03-18 20:07:40 +01:00
2 changed files with 35 additions and 5 deletions

View File

@ -27,7 +27,7 @@ api_access = false
# Gives your extension access to make external network calls, using the
# JavaScript `fetch()` API. Learn more:
# https://shopify.dev/docs/api/checkout-ui-extensions/unstable/configuration#network-access
network_access = false
network_access = true
# Loads metafields on checkout resources, including the cart,
# products, customers, and more. Learn more:

View File

@ -4,8 +4,10 @@ import {
Button,
Text,
useApi,
Spinner,
useSelectedPaymentOptions
} from "@shopify/ui-extensions-react/checkout";
import { useEffect, useState } from "react";
// 1. Choose an extension target
export default reactExtension(
@ -16,17 +18,45 @@ export default reactExtension(
function Extension() {
const options = useSelectedPaymentOptions();
const { shop, checkoutToken } = useApi();
const [isLoading, setIsLoading] = useState(true);
const [isSuccess, setIsSuccess] = useState(false);
const hasManualPayment = options.some((option) => option.type.toLowerCase() === 'manualpayment');
const appUrl = `PLUGIN_URL/checkout?checkout_token=${checkoutToken.current}`;
useEffect(() => {
if (!hasManualPayment) return;
const fetchInvoice = async () => {
setIsLoading(true);
try {
const response = await fetch(`${appUrl}&redirect=false`, {
method: 'GET',
headers: { 'Content-Type': 'application/json' },
});
if (response.ok) {
setIsSuccess(true);
}
} catch (error) {}
finally {
setIsLoading(false);
}
};
const timer = setTimeout(fetchInvoice, 1000);
return () => clearTimeout(timer)
}, [hasManualPayment]);
if (!hasManualPayment) return null;
return (
<BlockStack>
<Text>Shop name: {shop.name}</Text>
<Text size="large" alignment="center" bold>Review and pay using BTCPay Server!</Text>
<Text>Please review your order and complete the payment using BTCPay Server.</Text>
<Button to={appUrl} external>Complete Payment</Button>
{isLoading && <Spinner />}
{!isLoading && isSuccess && (
<>
<Text>Shop name: {shop.name}</Text>
<Text size="large" alignment="center" bold>Review and pay using BTCPay Server!</Text>
<Text>Please review your order and complete the payment using BTCPay Server.</Text>
<Button to={appUrl} external>Complete Payment</Button>
</>
)}
</BlockStack>
);
}