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) {
|
export async function createOrder(noticeId, orderType) {
|
||||||
const { token } = useAuthStore.getState();
|
const { token } = useAuthStore.getState();
|
||||||
const headers = token ? { Authorization: `Bearer ${token}` } : {};
|
const headers = token ? { Authorization: `Bearer ${token}` } : {};
|
||||||
const clientId = 1;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await axios.post(
|
const response = await axios.post(
|
||||||
`${API_URL}/add`,
|
`${API_URL}/add`,
|
||||||
{ clientId: clientId, noticeId: noticeId, orderType: orderType },
|
{ noticeId: noticeId, orderType: orderType },
|
||||||
{
|
{
|
||||||
headers: headers,
|
headers: headers,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,18 @@
|
|||||||
"bundleIdentifier": "com.hamx.artisanconnect"
|
"bundleIdentifier": "com.hamx.artisanconnect"
|
||||||
},
|
},
|
||||||
"android": {
|
"android": {
|
||||||
|
"intentFilters": [
|
||||||
|
{
|
||||||
|
"action": "VIEW",
|
||||||
|
"autoVerify": true,
|
||||||
|
"data": [
|
||||||
|
{
|
||||||
|
"scheme": "com.hamx.artisanconnect"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"category": ["BROWSABLE", "DEFAULT"]
|
||||||
|
}
|
||||||
|
],
|
||||||
"adaptiveIcon": {
|
"adaptiveIcon": {
|
||||||
"foregroundImage": "./assets/adaptive-icon.png",
|
"foregroundImage": "./assets/adaptive-icon.png",
|
||||||
"backgroundColor": "#ffffff"
|
"backgroundColor": "#ffffff"
|
||||||
|
|||||||
@@ -6,50 +6,28 @@ import { Box } from "@/components/ui/box";
|
|||||||
import { Text } from "@/components/ui/text";
|
import { Text } from "@/components/ui/text";
|
||||||
import { VStack } from "@/components/ui/vstack";
|
import { VStack } from "@/components/ui/vstack";
|
||||||
import { ActivityIndicator, FlatList } from "react-native";
|
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 { createOrder, createPayment, getOrder } from "@/api/order";
|
||||||
import { Linking } from "react-native";
|
|
||||||
import { Ionicons } from "@expo/vector-icons";
|
import { Ionicons } from "@expo/vector-icons";
|
||||||
import { useToast, Toast, ToastTitle } from "@/components/ui/toast";
|
import { useToast, Toast, ToastTitle } from "@/components/ui/toast";
|
||||||
import { AppState } from "react-native";
|
|
||||||
import { useAuthStore } from "@/store/authStore";
|
import { useAuthStore } from "@/store/authStore";
|
||||||
|
import * as WebBrowser from 'expo-web-browser';
|
||||||
|
import { useRouter } from "expo-router";
|
||||||
|
|
||||||
export default function UserNotices() {
|
export default function UserNotices() {
|
||||||
|
const router = useRouter();
|
||||||
const { notices, fetchNotices, deleteNotice } = useNoticesStore();
|
const { notices, fetchNotices, deleteNotice } = useNoticesStore();
|
||||||
const [isLoading, setIsLoading] = useState(true);
|
const [isLoading, setIsLoading] = useState(true);
|
||||||
const [isRedirecting, setIsRedirecting] = useState(false);
|
const [isRedirecting, setIsRedirecting] = useState(false);
|
||||||
const toast = useToast();
|
const toast = useToast();
|
||||||
const appState = useRef(AppState.currentState);
|
|
||||||
const [toastId, setToastId] = useState(0);
|
const [toastId, setToastId] = useState(0);
|
||||||
const { user_id } = useAuthStore.getState();
|
const { user_id } = useAuthStore.getState();
|
||||||
const currentUserId = user_id;
|
const currentUserId = user_id;
|
||||||
const [orderId, setOrderId] = useState(null);
|
const [orderId, setOrderId] = useState(null);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!isRedirecting) return;
|
WebBrowser.maybeCompleteAuthSession();
|
||||||
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]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const loadNotices = async () => {
|
const loadNotices = async () => {
|
||||||
@@ -84,28 +62,52 @@ export default function UserNotices() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const handleOrder = async (noticeId, type) => {
|
const handleOrder = async (noticeId, type) => {
|
||||||
{
|
try {
|
||||||
try {
|
const result = await createOrder(noticeId, type);
|
||||||
const result = await createOrder(noticeId, type);
|
if (result) {
|
||||||
if (result) {
|
setOrderId(result);
|
||||||
setOrderId(result);
|
try {
|
||||||
try {
|
const paymentResult = await createPayment(result);
|
||||||
const paymentResult = await createPayment(result);
|
if (paymentResult) {
|
||||||
if (paymentResult) {
|
setIsRedirecting(true);
|
||||||
setIsRedirecting(true);
|
|
||||||
await Linking.openURL(paymentResult);
|
|
||||||
} else {
|
await WebBrowser.openAuthSessionAsync(paymentResult);
|
||||||
console.log(`Nie udało się aktywować ogłoszenia 4 ${noticeId}.`);
|
|
||||||
}
|
|
||||||
} catch (err) {
|
setTimeout(async () => {
|
||||||
// console.log("Błąd podczas aktywacji ogłoszenia 3:", err);
|
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 {
|
||||||
|
showNewToast("Sprawdzanie statusu płatności...");
|
||||||
|
}
|
||||||
|
} catch (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 ${noticeId}.`);
|
||||||
}
|
}
|
||||||
} else {
|
} catch (err) {
|
||||||
// console.log(`Nie udało się aktywować ogłoszenia 2 ${noticeId}.`);
|
setIsRedirecting(false);
|
||||||
|
console.log("Błąd podczas aktywacji ogłoszenia:", err);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
|
||||||
console.log("Błąd podczas aktywacji ogłoszenia 1:", err);
|
|
||||||
}
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.log("Błąd podczas aktywacji ogłoszenia:", err);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -133,7 +135,7 @@ export default function UserNotices() {
|
|||||||
<VStack className="p-2">
|
<VStack className="p-2">
|
||||||
{isRedirecting && (
|
{isRedirecting && (
|
||||||
<Box className="absolute inset-0 bg-white bg-opacity-30 justify-center items-center z-50">
|
<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">
|
<Text className="text-lg font-bold pt-2">
|
||||||
Przekierowanie do płatności...
|
Przekierowanie do płatności...
|
||||||
</Text>
|
</Text>
|
||||||
|
|||||||
Reference in New Issue
Block a user