From b29c57316d04df48cafbd012d6f36bb4935b71cf Mon Sep 17 00:00:00 2001 From: Riad Elimemmedov Date: Tue, 19 Mar 2024 21:15:53 +0400 Subject: [PATCH] Completed transaction logs process for stripe --- backend/abstract/constants.py | 15 +++ backend/apps/order/migrations/__init__.py | 0 backend/apps/order/views.py | 36 ++++--- backend/apps/pet/migrations/0001_initial.py | 8 +- .../migrations/0002_remove_pet_pet_index.py | 17 ---- backend/apps/transaction/admin.py | 11 +-- backend/apps/transaction/helpers.py | 40 ++++++++ .../transaction/migrations/0001_initial.py | 11 ++- ...r_transaction_adopted_pet_slug_and_more.py | 31 ++++++ .../0003_remove_transaction_confirmations.py | 17 ++++ .../0004_transaction_confirmations.py | 18 ++++ .../migrations/0005_transaction_session.py | 18 ++++ ...6_rename_session_transaction_session_id.py | 18 ++++ backend/apps/transaction/models.py | 14 ++- backend/apps/transaction/views.py | 5 +- .../apps/upload/migrations/0001_initial.py | 2 +- .../user_profile/migrations/0001_initial.py | 2 +- backend/apps/users/migrations/0001_initial.py | 2 +- backend/run_migrations.sh | 2 +- frontend/contract-ui/contracts/PetLocal.json | 2 +- .../src/components/PaymentSuccsess.jsx | 5 +- .../contract-ui/src/components/Petitem.jsx | 2 +- frontend/contract-ui/src/routes/Cart.jsx | 5 +- frontend/contract-ui/state/store.js | 98 +++++++++++-------- 24 files changed, 277 insertions(+), 102 deletions(-) delete mode 100644 backend/apps/order/migrations/__init__.py delete mode 100644 backend/apps/pet/migrations/0002_remove_pet_pet_index.py create mode 100644 backend/apps/transaction/helpers.py create mode 100644 backend/apps/transaction/migrations/0002_alter_transaction_adopted_pet_slug_and_more.py create mode 100644 backend/apps/transaction/migrations/0003_remove_transaction_confirmations.py create mode 100644 backend/apps/transaction/migrations/0004_transaction_confirmations.py create mode 100644 backend/apps/transaction/migrations/0005_transaction_session.py create mode 100644 backend/apps/transaction/migrations/0006_rename_session_transaction_session_id.py diff --git a/backend/abstract/constants.py b/backend/abstract/constants.py index ae816fd..3cc2528 100644 --- a/backend/abstract/constants.py +++ b/backend/abstract/constants.py @@ -33,9 +33,24 @@ class AppName(Enum): ImageExtension = ["png", "jpg", "jpeg"] +# !PaymentMethods +PaymentOptions = [ + ("STRIPE", _("stripe")), + ("ETHEREUM", _("ethereum")), +] + # !Configure stripe stripe_keys = { "secret_key": config("STRIPE_SECRET_KEY"), "publishable_key": config("STRIPE_PUBLISHABLE_KEY"), } stripe.api_key = stripe_keys["secret_key"] + + +# session = stripe.checkout.Session.retrieve( +# "cs_test_b1vTOtKpWL95zEGz7wg7BrsGmVZeLUGufdnarAq3BW7jWtMiScuNYkL8pV" +# ) + + +# payment_intent = stripe.PaymentIntent.retrieve(session["payment_intent"]) +# print("Payment intend value ", payment_intent) diff --git a/backend/apps/order/migrations/__init__.py b/backend/apps/order/migrations/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/backend/apps/order/views.py b/backend/apps/order/views.py index 6ca7e43..daaac70 100644 --- a/backend/apps/order/views.py +++ b/backend/apps/order/views.py @@ -1,9 +1,12 @@ +import json + from django.contrib.auth.models import User from rest_framework import authentication, permissions from rest_framework.response import Response from rest_framework.views import APIView from abstract.constants import stripe, stripe_keys +from apps.transaction.helpers import save_transaction # !OrderView @@ -17,7 +20,23 @@ class OrderView(APIView): def post(self, request, format=None): domain_url = "http://localhost:8001/" - + body = json.loads(request.body.decode("utf-8")) + line_items = [] + pet_data = [] + for pet in body["pets"]: + item = { + "name": str(pet[2]), + "quantity": 1, + "currency": "usd", + "amount": round(int(pet[4]["hex"], 16) * 100), + } + pet_data.append( + { + "adopted_pet_slug": str(pet[1]), + "value": round(int(pet[4]["hex"], 16) * 100), + } + ) + line_items.append(item) try: # create new checkout session for payment checkout_session = stripe.checkout.Session.create( @@ -25,16 +44,11 @@ def post(self, request, format=None): cancel_url=domain_url + "canceled", payment_method_types=["card"], mode="payment", - line_items=[ - { - "name": "Sherlock Holmes", - "quantity": 1, - "currency": "usd", - "amount": round(float(5) * 100), - } - ], + line_items=line_items, + ) + save_transaction( + checkout_session["id"], pet_data, body["from_user"], "STRIPE" ) return Response({"sessionId": checkout_session["id"]}) - except Exception as e: - print("e ", e) + except Exception: return Response({"message": "Please try again"}) diff --git a/backend/apps/pet/migrations/0001_initial.py b/backend/apps/pet/migrations/0001_initial.py index 179d52b..518a1c4 100644 --- a/backend/apps/pet/migrations/0001_initial.py +++ b/backend/apps/pet/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 5.0.1 on 2024-03-11 21:15 +# Generated by Django 5.0.1 on 2024-03-18 19:05 import colorfield.fields import django.core.validators @@ -105,12 +105,6 @@ class Migration(migrations.Migration): "vaccinated", models.BooleanField(default=True, verbose_name="Vaccinated"), ), - ( - "pet_index", - models.IntegerField( - null=True, unique=True, verbose_name="Pet index" - ), - ), ( "price_currency", djmoney.models.fields.CurrencyField( diff --git a/backend/apps/pet/migrations/0002_remove_pet_pet_index.py b/backend/apps/pet/migrations/0002_remove_pet_pet_index.py deleted file mode 100644 index ab3542a..0000000 --- a/backend/apps/pet/migrations/0002_remove_pet_pet_index.py +++ /dev/null @@ -1,17 +0,0 @@ -# Generated by Django 5.0.1 on 2024-03-12 16:40 - -from django.db import migrations - - -class Migration(migrations.Migration): - - dependencies = [ - ("pet", "0001_initial"), - ] - - operations = [ - migrations.RemoveField( - model_name="pet", - name="pet_index", - ), - ] diff --git a/backend/apps/transaction/admin.py b/backend/apps/transaction/admin.py index 753a012..8b65e34 100644 --- a/backend/apps/transaction/admin.py +++ b/backend/apps/transaction/admin.py @@ -28,15 +28,8 @@ def clean(self): # !TransactionalAdmin class TransactionalAdmin(admin.ModelAdmin): form = TransactionModelForm - list_display = [ - "from_user", - "confirmations", - "value", - "adopted_pet_slug", - "created", - "modified", - ] - list_display_links = ["from_user", "confirmations"] + list_display = ["from_user", "value", "adopted_pet_slug", "created", "session_id"] + list_display_links = ["from_user", "session_id"] admin.site.register(Transaction, TransactionalAdmin) diff --git a/backend/apps/transaction/helpers.py b/backend/apps/transaction/helpers.py new file mode 100644 index 0000000..fb2139d --- /dev/null +++ b/backend/apps/transaction/helpers.py @@ -0,0 +1,40 @@ +import httpx + +from abstract.constants import stripe + + +# ? save_transaction +def save_transaction( + session_id: str, pet_data: list, from_user: str, payment_options: str +): + url = "http://127.0.0.1:8000/transactions/" + try: + for pet_data in pet_data: + response = httpx.post( + url, + json={ + "from_user": from_user, + "confirmations": 1 if session_id is not None else 0, + "value": str(pet_data["value"] / 100), + "adopted_pet_slug": pet_data["adopted_pet_slug"], + "payment_options": payment_options, + "session_id": session_id, + }, + ) + response.raise_for_status() + if response.status_code == 201: + print("Request successful!") + print("Response:", response.json()) + else: + print("Request failed with status code:", response.status_code) + except httpx.HTTPError as e: + print("HTTPError occurred:", str(e)) + except Exception as e: + print("An error occurred:", str(e)) + + +# ? get_transaction_detail +def get_transaction_detail(session_id: str): + session = stripe.checkout.Session.retrieve(session_id) + payment_intent = stripe.PaymentIntent.retrieve(session["payment_intent"]) + return payment_intent["amount"] / 100 diff --git a/backend/apps/transaction/migrations/0001_initial.py b/backend/apps/transaction/migrations/0001_initial.py index a8f9203..24838c5 100644 --- a/backend/apps/transaction/migrations/0001_initial.py +++ b/backend/apps/transaction/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 5.0.1 on 2024-03-18 12:37 +# Generated by Django 5.0.1 on 2024-03-18 19:07 import django_extensions.db.fields from django.db import migrations, models @@ -45,6 +45,15 @@ class Migration(migrations.Migration): "adopted_pet_slug", models.CharField(max_length=100, verbose_name="adopted slug"), ), + ( + "payment_options", + models.CharField( + choices=[("STRIPE", "stripe"), ("ETHEREUM", "ethereum")], + max_length=100, + null=True, + verbose_name="payment options", + ), + ), ], options={ "verbose_name": "Transaction", diff --git a/backend/apps/transaction/migrations/0002_alter_transaction_adopted_pet_slug_and_more.py b/backend/apps/transaction/migrations/0002_alter_transaction_adopted_pet_slug_and_more.py new file mode 100644 index 0000000..f8bb1c9 --- /dev/null +++ b/backend/apps/transaction/migrations/0002_alter_transaction_adopted_pet_slug_and_more.py @@ -0,0 +1,31 @@ +# Generated by Django 5.0.1 on 2024-03-18 19:08 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("transaction", "0001_initial"), + ] + + operations = [ + migrations.AlterField( + model_name="transaction", + name="adopted_pet_slug", + field=models.CharField( + db_index=True, max_length=100, unique=True, verbose_name="adopted slug" + ), + ), + migrations.AlterField( + model_name="transaction", + name="payment_options", + field=models.CharField( + choices=[("STRIPE", "stripe"), ("ETHEREUM", "ethereum")], + default=1, + max_length=100, + verbose_name="payment options", + ), + preserve_default=False, + ), + ] diff --git a/backend/apps/transaction/migrations/0003_remove_transaction_confirmations.py b/backend/apps/transaction/migrations/0003_remove_transaction_confirmations.py new file mode 100644 index 0000000..0078138 --- /dev/null +++ b/backend/apps/transaction/migrations/0003_remove_transaction_confirmations.py @@ -0,0 +1,17 @@ +# Generated by Django 5.0.1 on 2024-03-19 16:08 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ("transaction", "0002_alter_transaction_adopted_pet_slug_and_more"), + ] + + operations = [ + migrations.RemoveField( + model_name="transaction", + name="confirmations", + ), + ] diff --git a/backend/apps/transaction/migrations/0004_transaction_confirmations.py b/backend/apps/transaction/migrations/0004_transaction_confirmations.py new file mode 100644 index 0000000..614daf8 --- /dev/null +++ b/backend/apps/transaction/migrations/0004_transaction_confirmations.py @@ -0,0 +1,18 @@ +# Generated by Django 5.0.1 on 2024-03-19 16:36 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("transaction", "0003_remove_transaction_confirmations"), + ] + + operations = [ + migrations.AddField( + model_name="transaction", + name="confirmations", + field=models.IntegerField(null=True, verbose_name="confirmations"), + ), + ] diff --git a/backend/apps/transaction/migrations/0005_transaction_session.py b/backend/apps/transaction/migrations/0005_transaction_session.py new file mode 100644 index 0000000..9c91228 --- /dev/null +++ b/backend/apps/transaction/migrations/0005_transaction_session.py @@ -0,0 +1,18 @@ +# Generated by Django 5.0.1 on 2024-03-19 16:41 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("transaction", "0004_transaction_confirmations"), + ] + + operations = [ + migrations.AddField( + model_name="transaction", + name="session", + field=models.CharField(max_length=100, null=True, verbose_name="session"), + ), + ] diff --git a/backend/apps/transaction/migrations/0006_rename_session_transaction_session_id.py b/backend/apps/transaction/migrations/0006_rename_session_transaction_session_id.py new file mode 100644 index 0000000..55d8978 --- /dev/null +++ b/backend/apps/transaction/migrations/0006_rename_session_transaction_session_id.py @@ -0,0 +1,18 @@ +# Generated by Django 5.0.1 on 2024-03-19 16:41 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ("transaction", "0005_transaction_session"), + ] + + operations = [ + migrations.RenameField( + model_name="transaction", + old_name="session", + new_name="session_id", + ), + ] diff --git a/backend/apps/transaction/models.py b/backend/apps/transaction/models.py index 255021d..8e6eb33 100644 --- a/backend/apps/transaction/models.py +++ b/backend/apps/transaction/models.py @@ -2,17 +2,21 @@ from django.utils.translation import gettext_lazy as _ from django_extensions.db.models import TimeStampedModel -from .validate_field import validate_confirmations, validate_from_user +from abstract.constants import PaymentOptions # !Transaction class Transaction(TimeStampedModel): from_user = models.CharField(_("from user"), max_length=100) - confirmations = models.IntegerField( - _("confirmations"), validators=[validate_confirmations] - ) + confirmations = models.IntegerField(_("confirmations"), null=True) value = models.CharField(_("value"), max_length=100) - adopted_pet_slug = models.CharField(_("adopted slug"), max_length=100) + adopted_pet_slug = models.CharField( + _("adopted slug"), unique=True, db_index=True, max_length=100 + ) + payment_options = models.CharField( + _("payment options"), max_length=100, choices=PaymentOptions + ) + session_id = models.CharField(_("session"), max_length=100, null=True) class Meta: verbose_name = "Transaction" diff --git a/backend/apps/transaction/views.py b/backend/apps/transaction/views.py index 0c2f079..7cf01da 100644 --- a/backend/apps/transaction/views.py +++ b/backend/apps/transaction/views.py @@ -1,4 +1,5 @@ from django.shortcuts import render +from rest_framework import status from rest_framework.response import Response from rest_framework.views import APIView @@ -18,5 +19,5 @@ def post(self, request, format=None): serializer = self.serializer_class(data=request.data) if serializer.is_valid(): serializer.save() - return Response(serializer.data, status=201) - return Response(serializer.errors, status=400) + return Response(serializer.data, status=status.HTTP_201_CREATED) + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) diff --git a/backend/apps/upload/migrations/0001_initial.py b/backend/apps/upload/migrations/0001_initial.py index de906af..c544d4b 100644 --- a/backend/apps/upload/migrations/0001_initial.py +++ b/backend/apps/upload/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 5.0.1 on 2024-03-11 21:15 +# Generated by Django 5.0.1 on 2024-03-18 19:05 from django.db import migrations, models diff --git a/backend/apps/user_profile/migrations/0001_initial.py b/backend/apps/user_profile/migrations/0001_initial.py index 11dec55..5d4a115 100644 --- a/backend/apps/user_profile/migrations/0001_initial.py +++ b/backend/apps/user_profile/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 5.0.1 on 2024-03-11 21:15 +# Generated by Django 5.0.1 on 2024-03-18 19:05 import django.core.validators import django.db.models.deletion diff --git a/backend/apps/users/migrations/0001_initial.py b/backend/apps/users/migrations/0001_initial.py index 614f4a2..df5480a 100644 --- a/backend/apps/users/migrations/0001_initial.py +++ b/backend/apps/users/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 5.0.1 on 2024-03-11 21:15 +# Generated by Django 5.0.1 on 2024-03-18 19:05 import django.utils.timezone from django.db import migrations, models diff --git a/backend/run_migrations.sh b/backend/run_migrations.sh index f5e2dd5..a420e5c 100644 --- a/backend/run_migrations.sh +++ b/backend/run_migrations.sh @@ -1,5 +1,5 @@ #!/bin/sh -APP_NAMES=("users" "user_profile" "pet" "upload") +APP_NAMES=("users" "user_profile" "pet" "upload" "order" "transaction") for APP_NAME in "${APP_NAMES[@]}"; do # Run makemigrations for specific apps diff --git a/frontend/contract-ui/contracts/PetLocal.json b/frontend/contract-ui/contracts/PetLocal.json index d974812..f8aa7ed 100644 --- a/frontend/contract-ui/contracts/PetLocal.json +++ b/frontend/contract-ui/contracts/PetLocal.json @@ -1 +1 @@ -{"abi":"[{\"type\":\"constructor\",\"payable\":false,\"inputs\":[{\"type\":\"uint256\",\"name\":\"initialPetIndex\"}]},{\"type\":\"function\",\"name\":\"addPet\",\"constant\":false,\"payable\":false,\"inputs\":[],\"outputs\":[]},{\"type\":\"function\",\"name\":\"addToCart\",\"constant\":false,\"payable\":false,\"inputs\":[{\"type\":\"uint256\",\"name\":\"_petId\"},{\"type\":\"string\",\"name\":\"_petName\"},{\"type\":\"string\",\"name\":\"_petColor\"},{\"type\":\"uint256\",\"name\":\"_petPrice\"},{\"type\":\"string\",\"name\":\"_petPhoto\"}],\"outputs\":[]},{\"type\":\"function\",\"name\":\"adoptPet\",\"constant\":false,\"payable\":false,\"inputs\":[{\"type\":\"uint256\",\"name\":\"adoptIdx\"}],\"outputs\":[]},{\"type\":\"function\",\"name\":\"allAdoptedPets\",\"constant\":true,\"stateMutability\":\"view\",\"payable\":false,\"inputs\":[{\"type\":\"uint256\"}],\"outputs\":[{\"type\":\"uint256\"}]},{\"type\":\"function\",\"name\":\"getAllAdoptedPets\",\"constant\":true,\"stateMutability\":\"view\",\"payable\":false,\"inputs\":[],\"outputs\":[{\"type\":\"uint256[]\"}]},{\"type\":\"function\",\"name\":\"getAllAdoptedPetsByOwner\",\"constant\":true,\"stateMutability\":\"view\",\"payable\":false,\"inputs\":[],\"outputs\":[{\"type\":\"uint256[]\"}]},{\"type\":\"function\",\"name\":\"getCartItems\",\"constant\":true,\"stateMutability\":\"view\",\"payable\":false,\"inputs\":[],\"outputs\":[{\"type\":\"tuple[]\",\"components\":[{\"type\":\"uint256\",\"name\":\"id\"},{\"type\":\"string\",\"name\":\"name\"},{\"type\":\"string\",\"name\":\"color\"},{\"type\":\"uint256\",\"name\":\"price\"},{\"type\":\"string\",\"name\":\"photo\"}]}]},{\"type\":\"function\",\"name\":\"getCartLength\",\"constant\":true,\"stateMutability\":\"view\",\"payable\":false,\"inputs\":[],\"outputs\":[{\"type\":\"uint256\"}]},{\"type\":\"function\",\"name\":\"getOwner\",\"constant\":true,\"stateMutability\":\"view\",\"payable\":false,\"inputs\":[],\"outputs\":[{\"type\":\"address\"}]},{\"type\":\"function\",\"name\":\"owner\",\"constant\":true,\"stateMutability\":\"view\",\"payable\":false,\"inputs\":[],\"outputs\":[{\"type\":\"address\"}]},{\"type\":\"function\",\"name\":\"ownerAddressToPetList\",\"constant\":true,\"stateMutability\":\"view\",\"payable\":false,\"inputs\":[{\"type\":\"address\"},{\"type\":\"uint256\"}],\"outputs\":[{\"type\":\"uint256\"}]},{\"type\":\"function\",\"name\":\"petIdxToOwnerAddress\",\"constant\":true,\"stateMutability\":\"view\",\"payable\":false,\"inputs\":[{\"type\":\"uint256\"}],\"outputs\":[{\"type\":\"address\"}]},{\"type\":\"function\",\"name\":\"petIndex\",\"constant\":true,\"stateMutability\":\"view\",\"payable\":false,\"inputs\":[],\"outputs\":[{\"type\":\"uint256\"}]},{\"type\":\"function\",\"name\":\"removeCart\",\"constant\":false,\"payable\":false,\"inputs\":[{\"type\":\"uint256\",\"name\":\"index\"}],\"outputs\":[]}]","address":"0x5FbDB2315678afecb367f032d93F642f64180aa3","network":"localhost","deployer":"0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"} +{"abi":"[{\"type\":\"constructor\",\"payable\":false,\"inputs\":[{\"type\":\"uint256\",\"name\":\"initialPetIndex\"}]},{\"type\":\"function\",\"name\":\"addPet\",\"constant\":false,\"payable\":false,\"inputs\":[],\"outputs\":[]},{\"type\":\"function\",\"name\":\"addToCart\",\"constant\":false,\"payable\":false,\"inputs\":[{\"type\":\"uint256\",\"name\":\"_petId\"},{\"type\":\"string\",\"name\":\"_petSlug\"},{\"type\":\"string\",\"name\":\"_petName\"},{\"type\":\"string\",\"name\":\"_petColor\"},{\"type\":\"uint256\",\"name\":\"_petPrice\"},{\"type\":\"string\",\"name\":\"_petPhoto\"}],\"outputs\":[]},{\"type\":\"function\",\"name\":\"adoptPet\",\"constant\":false,\"payable\":false,\"inputs\":[{\"type\":\"uint256\",\"name\":\"adoptIdx\"}],\"outputs\":[]},{\"type\":\"function\",\"name\":\"allAdoptedPets\",\"constant\":true,\"stateMutability\":\"view\",\"payable\":false,\"inputs\":[{\"type\":\"uint256\"}],\"outputs\":[{\"type\":\"uint256\"}]},{\"type\":\"function\",\"name\":\"getAllAdoptedPets\",\"constant\":true,\"stateMutability\":\"view\",\"payable\":false,\"inputs\":[],\"outputs\":[{\"type\":\"uint256[]\"}]},{\"type\":\"function\",\"name\":\"getAllAdoptedPetsByOwner\",\"constant\":true,\"stateMutability\":\"view\",\"payable\":false,\"inputs\":[],\"outputs\":[{\"type\":\"uint256[]\"}]},{\"type\":\"function\",\"name\":\"getCartItems\",\"constant\":true,\"stateMutability\":\"view\",\"payable\":false,\"inputs\":[],\"outputs\":[{\"type\":\"tuple[]\",\"components\":[{\"type\":\"uint256\",\"name\":\"id\"},{\"type\":\"string\",\"name\":\"slug\"},{\"type\":\"string\",\"name\":\"name\"},{\"type\":\"string\",\"name\":\"color\"},{\"type\":\"uint256\",\"name\":\"price\"},{\"type\":\"string\",\"name\":\"photo\"}]}]},{\"type\":\"function\",\"name\":\"getCartLength\",\"constant\":true,\"stateMutability\":\"view\",\"payable\":false,\"inputs\":[],\"outputs\":[{\"type\":\"uint256\"}]},{\"type\":\"function\",\"name\":\"getOwner\",\"constant\":true,\"stateMutability\":\"view\",\"payable\":false,\"inputs\":[],\"outputs\":[{\"type\":\"address\"}]},{\"type\":\"function\",\"name\":\"owner\",\"constant\":true,\"stateMutability\":\"view\",\"payable\":false,\"inputs\":[],\"outputs\":[{\"type\":\"address\"}]},{\"type\":\"function\",\"name\":\"ownerAddressToPetList\",\"constant\":true,\"stateMutability\":\"view\",\"payable\":false,\"inputs\":[{\"type\":\"address\"},{\"type\":\"uint256\"}],\"outputs\":[{\"type\":\"uint256\"}]},{\"type\":\"function\",\"name\":\"petIdxToOwnerAddress\",\"constant\":true,\"stateMutability\":\"view\",\"payable\":false,\"inputs\":[{\"type\":\"uint256\"}],\"outputs\":[{\"type\":\"address\"}]},{\"type\":\"function\",\"name\":\"petIndex\",\"constant\":true,\"stateMutability\":\"view\",\"payable\":false,\"inputs\":[],\"outputs\":[{\"type\":\"uint256\"}]},{\"type\":\"function\",\"name\":\"removeCart\",\"constant\":false,\"payable\":false,\"inputs\":[{\"type\":\"uint256\",\"name\":\"index\"}],\"outputs\":[]}]","address":"0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0","network":"localhost","deployer":"0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"} diff --git a/frontend/contract-ui/src/components/PaymentSuccsess.jsx b/frontend/contract-ui/src/components/PaymentSuccsess.jsx index 93ed8cf..b255634 100644 --- a/frontend/contract-ui/src/components/PaymentSuccsess.jsx +++ b/frontend/contract-ui/src/components/PaymentSuccsess.jsx @@ -4,6 +4,7 @@ import connect_contract from '../../helpers/connect_contract'; import { getSigner } from '../../helpers/get_signer'; import { useMoralis } from "react-moralis"; import { Navbar } from './Navbar'; +import usePetStore from '../../state/store'; // *PaymentSuccessCard @@ -11,6 +12,8 @@ const PaymentSuccessCard = () => { // Moralis const { web3,account } = useMoralis(); + const {confirmations} = usePetStore() + // getPaymentSessionId const getPaymentSessionId = () => { const queryString = window.location.search; @@ -33,6 +36,7 @@ const PaymentSuccessCard = () => { } } + //useEffect useEffect(()=>{ removePetsFromCard() },[]) @@ -41,7 +45,6 @@ const PaymentSuccessCard = () => { //return jsx to client return ( <> -
{ @@ -155,7 +158,7 @@ export default function Cart(){ headers: { 'Content-Type': 'application/json', }, - body: JSON.stringify({ book_id: 1 }), + body: JSON.stringify({ pets: pets,from_user:account }), }) .then((result) => { return result.json() diff --git a/frontend/contract-ui/state/store.js b/frontend/contract-ui/state/store.js index 6eef66c..089d3f3 100644 --- a/frontend/contract-ui/state/store.js +++ b/frontend/contract-ui/state/store.js @@ -2,46 +2,60 @@ import {create} from 'zustand'; import { toast } from 'react-toastify' -const authNetworkStore = create((set) => { - const HARDHAT_NETWORK_ID = Number(import.meta.env.VITE_VUE_HARDHAT_NETWORK_ID); // Replace with your desired network ID - // return jsx to client - return { - isAuthenticated: false, +// const authNetworkStore = create((set) => { +// const HARDHAT_NETWORK_ID = Number(import.meta.env.VITE_VUE_HARDHAT_NETWORK_ID); // Replace with your desired network ID +// // return jsx to client +// return { +// isAuthenticated: false, - checkAuthentication: async (provider) => { - if (typeof provider !== 'undefined') { - await provider.request({ method: 'eth_requestAccounts' }); - const isCheck = await this.checkNetwork(provider); - set({ isAuthenticated: isCheck }); - } else { - toast.error('MetaMask is not installed'); - set({ isAuthenticated: false }); - } - }, - checkNetwork: async (provider) => { - console.log("🚀 ~ checkNetwork: ~ provider:", provider) - if (provider.net_version !== HARDHAT_NETWORK_ID.toString()) { - if (await this.switchNetwork(provider)) { - return true; - } else { - return false; - } - } - return true; - }, - switchNetwork: async (provider) => { - const chainIdHex = `0x${HARDHAT_NETWORK_ID.toString(16)}`; // Convert to 16-based hexadecimal chainIdHex number - try { - await provider.request({ - method: 'wallet_switchEthereumChain', - params: [{ chainId: chainIdHex }], // Only accept hex number - }); - return true; - } catch (error) { - toast.error('Switching to the network failed'); - return false; - } - } - }; -}); -export default authNetworkStore +// checkAuthentication: async (provider) => { +// if (typeof provider !== 'undefined') { +// await provider.request({ method: 'eth_requestAccounts' }); +// const isCheck = await this.checkNetwork(provider); +// set({ isAuthenticated: isCheck }); +// } else { +// toast.error('MetaMask is not installed'); +// set({ isAuthenticated: false }); +// } +// }, +// checkNetwork: async (provider) => { +// console.log("🚀 ~ checkNetwork: ~ provider:", provider) +// if (provider.net_version !== HARDHAT_NETWORK_ID.toString()) { +// if (await this.switchNetwork(provider)) { +// return true; +// } else { +// return false; +// } +// } +// return true; +// }, +// switchNetwork: async (provider) => { +// const chainIdHex = `0x${HARDHAT_NETWORK_ID.toString(16)}`; // Convert to 16-based hexadecimal chainIdHex number +// try { +// await provider.request({ +// method: 'wallet_switchEthereumChain', +// params: [{ chainId: chainIdHex }], // Only accept hex number +// }); +// return true; +// } catch (error) { +// toast.error('Switching to the network failed'); +// return false; +// } +// } +// }; +// }); +// export default authNetworkStore + +const usePetStore = create((set) => ({ + from_user: "", + confirmations:null, + value:"", + adopted_pet_slug:"", + payment_options:"", + setFromUser: (from_user) => set({ from_user: from_user }), + setConfirmations: (confirmations) => set({ confirmations: confirmations }), + setValue: (value) => set({ value: value }), + setAdoptedPetSlug: (adopted_pet_slug) => set({ adopted_pet_slug: adopted_pet_slug }), + setPaymentOptions: (payment_options) => set({ payment_options: payment_options }), +})) +export default usePetStore