użycie expo web browser do płatności
This commit is contained in:
@@ -6,12 +6,11 @@ const API_URL = "https://hopp.zikor.pl/api/v1/orders";
|
||||
export async function createOrder(noticeId, orderType) {
|
||||
const { token } = useAuthStore.getState();
|
||||
const headers = token ? { Authorization: `Bearer ${token}` } : {};
|
||||
const clientId = 1;
|
||||
|
||||
try {
|
||||
const response = await axios.post(
|
||||
`${API_URL}/add`,
|
||||
{ clientId: clientId, noticeId: noticeId, orderType: orderType },
|
||||
{ noticeId: noticeId, orderType: orderType },
|
||||
{
|
||||
headers: headers,
|
||||
}
|
||||
|
||||
@@ -18,6 +18,18 @@
|
||||
"bundleIdentifier": "com.hamx.artisanconnect"
|
||||
},
|
||||
"android": {
|
||||
"intentFilters": [
|
||||
{
|
||||
"action": "VIEW",
|
||||
"autoVerify": true,
|
||||
"data": [
|
||||
{
|
||||
"scheme": "com.hamx.artisanconnect"
|
||||
}
|
||||
],
|
||||
"category": ["BROWSABLE", "DEFAULT"]
|
||||
}
|
||||
],
|
||||
"adaptiveIcon": {
|
||||
"foregroundImage": "./assets/adaptive-icon.png",
|
||||
"backgroundColor": "#ffffff"
|
||||
|
||||
@@ -6,50 +6,28 @@ import { Box } from "@/components/ui/box";
|
||||
import { Text } from "@/components/ui/text";
|
||||
import { VStack } from "@/components/ui/vstack";
|
||||
import { ActivityIndicator, FlatList } from "react-native";
|
||||
import { useEffect, useState, useRef } from "react";
|
||||
import { useEffect, useState } from "react";
|
||||
import { createOrder, createPayment, getOrder } from "@/api/order";
|
||||
import { Linking } from "react-native";
|
||||
import { Ionicons } from "@expo/vector-icons";
|
||||
import { useToast, Toast, ToastTitle } from "@/components/ui/toast";
|
||||
import { AppState } from "react-native";
|
||||
import { useAuthStore } from "@/store/authStore";
|
||||
import * as WebBrowser from 'expo-web-browser';
|
||||
import { useRouter } from "expo-router";
|
||||
|
||||
export default function UserNotices() {
|
||||
const router = useRouter();
|
||||
const { notices, fetchNotices, deleteNotice } = useNoticesStore();
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
const [isRedirecting, setIsRedirecting] = useState(false);
|
||||
const toast = useToast();
|
||||
const appState = useRef(AppState.currentState);
|
||||
const [toastId, setToastId] = useState(0);
|
||||
const { user_id } = useAuthStore.getState();
|
||||
const currentUserId = user_id;
|
||||
const [orderId, setOrderId] = useState(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (!isRedirecting) return;
|
||||
const subscription = AppState.addEventListener("change", (state) => {
|
||||
if (state === "active") {
|
||||
(async () => {
|
||||
const lastOrder = await getOrder(orderId);
|
||||
const lastPayments = lastOrder.payments;
|
||||
const paymentStatus =
|
||||
lastPayments.length > 0
|
||||
? lastPayments[lastPayments.length - 1].status
|
||||
: null;
|
||||
setIsRedirecting(false);
|
||||
if (paymentStatus === "INCORRECT") {
|
||||
showNewToast("Płatność została anulowana.");
|
||||
} else if (paymentStatus === "CORRECT") {
|
||||
showNewToast("Płatność została zrealizowana.");
|
||||
} else {
|
||||
showNewToast("Płatność jeszcze nie wpłynęła.");
|
||||
}
|
||||
})();
|
||||
}
|
||||
appState.current = state;
|
||||
});
|
||||
return () => subscription.remove();
|
||||
}, [isRedirecting, toast, orderId]);
|
||||
WebBrowser.maybeCompleteAuthSession();
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
const loadNotices = async () => {
|
||||
@@ -84,7 +62,6 @@ export default function UserNotices() {
|
||||
};
|
||||
|
||||
const handleOrder = async (noticeId, type) => {
|
||||
{
|
||||
try {
|
||||
const result = await createOrder(noticeId, type);
|
||||
if (result) {
|
||||
@@ -93,20 +70,45 @@ export default function UserNotices() {
|
||||
const paymentResult = await createPayment(result);
|
||||
if (paymentResult) {
|
||||
setIsRedirecting(true);
|
||||
await Linking.openURL(paymentResult);
|
||||
|
||||
|
||||
await WebBrowser.openAuthSessionAsync(paymentResult);
|
||||
|
||||
|
||||
setTimeout(async () => {
|
||||
setIsRedirecting(false);
|
||||
|
||||
try {
|
||||
const lastOrder = await getOrder(result);
|
||||
const lastPayments = lastOrder.payments;
|
||||
const paymentStatus = lastPayments.length > 0
|
||||
? lastPayments[lastPayments.length - 1].status
|
||||
: null;
|
||||
|
||||
if (paymentStatus === "CORRECT") {
|
||||
showNewToast("Płatność została zrealizowana.");
|
||||
await fetchNotices();
|
||||
router.replace("/notices");
|
||||
} else {
|
||||
console.log(`Nie udało się aktywować ogłoszenia 4 ${noticeId}.`);
|
||||
showNewToast("Sprawdzanie statusu płatności...");
|
||||
}
|
||||
} catch (err) {
|
||||
// console.log("Błąd podczas aktywacji ogłoszenia 3:", err);
|
||||
console.log("Błąd podczas sprawdzania płatności:", err);
|
||||
showNewToast("Nie udało się sprawdzić statusu płatności.");
|
||||
}
|
||||
}, 300);
|
||||
|
||||
} else {
|
||||
// console.log(`Nie udało się aktywować ogłoszenia 2 ${noticeId}.`);
|
||||
console.log(`Nie udało się aktywować ogłoszenia ${noticeId}.`);
|
||||
}
|
||||
} catch (err) {
|
||||
console.log("Błąd podczas aktywacji ogłoszenia 1:", err);
|
||||
setIsRedirecting(false);
|
||||
console.log("Błąd podczas aktywacji ogłoszenia:", err);
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.log("Błąd podczas aktywacji ogłoszenia:", err);
|
||||
}
|
||||
};
|
||||
|
||||
const handleDeleteNotice = async (noticeId) => {
|
||||
@@ -133,7 +135,7 @@ export default function UserNotices() {
|
||||
<VStack className="p-2">
|
||||
{isRedirecting && (
|
||||
<Box className="absolute inset-0 bg-white bg-opacity-30 justify-center items-center z-50">
|
||||
<Ionicons name="card-outline" size="30" className="pt-4" />
|
||||
<Ionicons name="card-outline" size={30} className="pt-4" />
|
||||
<Text className="text-lg font-bold pt-2">
|
||||
Przekierowanie do płatności...
|
||||
</Text>
|
||||
|
||||
Reference in New Issue
Block a user