Skip to content

Commit

Permalink
Only check for manual payment type
Browse files Browse the repository at this point in the history
  • Loading branch information
TChukwuleta committed Feb 20, 2025
1 parent 0ee89c2 commit 0d4ca74
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 40 deletions.
2 changes: 1 addition & 1 deletion extensions/btcpaycheckout/shopify.extension.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = true
network_access = false

# Loads metafields on checkout resources, including the cart,
# products, customers, and more. Learn more:
Expand Down
42 changes: 3 additions & 39 deletions extensions/btcpaycheckout/src/Checkout.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
useApi,
useSelectedPaymentOptions
} from "@shopify/ui-extensions-react/checkout";
import { useState } from "react";

// 1. Choose an extension target
export default reactExtension(
Expand All @@ -17,52 +16,17 @@ export default reactExtension(
function Extension() {
const options = useSelectedPaymentOptions();
const { shop, checkoutToken } = useApi();
const [isInvoiceSettled, setIsInvoiceSettled] = useState(false);
const [isCheckingPayment, setIsCheckingPayment] = useState(false);
const hasManualPayment = options.some((option) => option.type.toLowerCase() === 'manualpayment');

const appUrl = `PLUGIN_URL/checkout?checkout_token=${checkoutToken.current}`;
const validatePaymentUrl = `PLUGIN_URL/validate-payment?checkout_token=${checkoutToken.current}`;

const checkPaymentStatus = async () => {
try {
const response = await fetch(validatePaymentUrl);
if (!response.ok) return;

const data = await response.json();
if (data.IsInvoiceSettled) {
setIsInvoiceSettled(true);
setIsCheckingPayment(false);
}
} catch (error) {}
};


const startCheckingPayment = () => {
if (isCheckingPayment) return;

setIsCheckingPayment(true);
const interval = setInterval(async () => {
await checkPaymentStatus();
}, 4000);
return () => clearInterval(interval);
};


if (!hasManualPayment) return null;

return (
<BlockStack>
<Text>Shop name: {shop.name}</Text>
{isInvoiceSettled ? (
<Text size="large" alignment="center" bold>Payment received. Thank you for your order!</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 onPress={startCheckingPayment}>Complete Payment</Button>
</>
)}
<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>
);
}

0 comments on commit 0d4ca74

Please sign in to comment.