UPD: Send invoice to orders api
This commit is contained in:
parent
490d2c9b7f
commit
decb754215
@ -2,5 +2,4 @@ export {default as PayPal} from './paypal';
|
|||||||
export {default as Purchase} from './purchase';
|
export {default as Purchase} from './purchase';
|
||||||
export {default as ShowCase} from './showCase';
|
export {default as ShowCase} from './showCase';
|
||||||
export {default as Home} from './home';
|
export {default as Home} from './home';
|
||||||
export {default as Landing} from './landing';
|
export {default as Landing} from './landing';
|
||||||
export {default as Invoice} from './invoice';
|
|
@ -1,24 +0,0 @@
|
|||||||
import React from "react";
|
|
||||||
|
|
||||||
// re-arrange to make invoice detail a self-contained element
|
|
||||||
const Invoice = ( {invoiceDetails} ) => {
|
|
||||||
let addressInfo = invoiceDetails.purchase_units[0].shipping.address;
|
|
||||||
let address = `${addressInfo.address_line_1} ${addressInfo.admin_area_2}, ${addressInfo.admin_area_1} ${addressInfo.postal_code}`;
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div className="container">
|
|
||||||
Date Purchased: {invoiceDetails.create_time}
|
|
||||||
<br />
|
|
||||||
Item: {invoiceDetails.purchase_units[0].description}
|
|
||||||
<br />
|
|
||||||
Amount Paid: ${invoiceDetails.purchase_units[0].amount.value}
|
|
||||||
<br />
|
|
||||||
Ship to: {invoiceDetails.purchase_units[0].shipping.name.full_name}
|
|
||||||
<br />
|
|
||||||
{/* Shipping address: {streetAddr} {city}, {state} {postalCode} */}
|
|
||||||
Shpping address: {address}
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
export default Invoice;
|
|
@ -10,6 +10,7 @@ const Details = () => {
|
|||||||
const [cart, setCart] = useState([]);
|
const [cart, setCart] = useState([]);
|
||||||
const [orderID, setOrderID] = useState(null);
|
const [orderID, setOrderID] = useState(null);
|
||||||
const [total, setTotal] = useState(0);
|
const [total, setTotal] = useState(0);
|
||||||
|
const [invoice, setInvoice] = useState(null);
|
||||||
|
|
||||||
// refresh the cart logo and amount of the cart
|
// refresh the cart logo and amount of the cart
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@ -28,12 +29,11 @@ const Details = () => {
|
|||||||
<>
|
<>
|
||||||
<ShowCase />
|
<ShowCase />
|
||||||
{/* you can try 600011 in orderID */}
|
{/* you can try 600011 in orderID */}
|
||||||
<Orders orderID={orderID} setOrderID={setOrderID} />
|
<Orders orderID={orderID} setOrderID={setOrderID} invoice={invoice} />
|
||||||
<AllProducts setCart={setCart} />
|
<AllProducts setCart={setCart} />
|
||||||
<CartIcon count={cart?.length} total={total} />
|
<CartIcon count={cart?.length} total={total} />
|
||||||
<Purchase />
|
<Purchase />
|
||||||
<PayPal cart={cart} />
|
<PayPal cart={cart} setInvoice={setInvoice} />
|
||||||
|
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
import React, { useState } from "react";
|
import React from "react";
|
||||||
import { PayPalScriptProvider, PayPalButtons, FUNDING } from "@paypal/react-paypal-js";
|
import { PayPalScriptProvider, PayPalButtons, FUNDING } from "@paypal/react-paypal-js";
|
||||||
import { Invoice } from '.';
|
|
||||||
|
|
||||||
const initialOptions = {
|
const initialOptions = {
|
||||||
"client-id": "AQBhJiIcG8lS4K5UoTo_-G5it85lbFpxEcA-WWBb1A-_Nq6YfKUdn05oyg-SgpxzXTyjfpdMceWskq7G",
|
"client-id": "AQBhJiIcG8lS4K5UoTo_-G5it85lbFpxEcA-WWBb1A-_Nq6YfKUdn05oyg-SgpxzXTyjfpdMceWskq7G",
|
||||||
@ -10,10 +9,7 @@ const initialOptions = {
|
|||||||
// buyer account: dev01@oemautomation.com, pwd: 3Deerhead
|
// buyer account: dev01@oemautomation.com, pwd: 3Deerhead
|
||||||
// merchant account: biz01@oemautomation.com, pwd 3Deerhead
|
// merchant account: biz01@oemautomation.com, pwd 3Deerhead
|
||||||
|
|
||||||
const PayPal = ({cart}) => {
|
const PayPal = ({cart, setInvoice}) => {
|
||||||
const [showInvoice, setShowInvoice] = useState(false);
|
|
||||||
const [invoiceDetails, setInvoiceDetails] = useState({});
|
|
||||||
|
|
||||||
|
|
||||||
const createOrder = (data, actions) => {
|
const createOrder = (data, actions) => {
|
||||||
return actions.order.create({
|
return actions.order.create({
|
||||||
@ -32,8 +28,7 @@ const PayPal = ({cart}) => {
|
|||||||
|
|
||||||
const onApprove = (data, actions) => {
|
const onApprove = (data, actions) => {
|
||||||
return actions.order.capture().then(function(details) {
|
return actions.order.capture().then(function(details) {
|
||||||
setInvoiceDetails(details);
|
setInvoice(details);
|
||||||
setShowInvoice(true);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -53,7 +48,6 @@ const PayPal = ({cart}) => {
|
|||||||
</div>
|
</div>
|
||||||
</PayPalScriptProvider>
|
</PayPalScriptProvider>
|
||||||
</div>
|
</div>
|
||||||
{showInvoice && <Invoice invoiceDetails={invoiceDetails} />}
|
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
@ -1,32 +1,61 @@
|
|||||||
import React, {useEffect} from "react";
|
import React, { useEffect } from "react";
|
||||||
import { useGetOrders, useSetOrder } from "../api";
|
import { useGetOrders, useSetOrder } from "../api";
|
||||||
|
|
||||||
const sampleOrder = {
|
const sampleOrder = {
|
||||||
items: [
|
items: [
|
||||||
{ productName: "B1-14MR", price: 129.99 },
|
{ productName: "B1-14MR", price: 129.99 },
|
||||||
{ productName: "W2C", price: 29.99 },
|
{ productName: "W2C", price: 29.99 },
|
||||||
{ productName: "PSU-24VDC", price: 19.99}
|
{ productName: "PSU-24VDC", price: 19.99 }
|
||||||
],
|
],
|
||||||
totalAmount: 1779.97,
|
totalAmount: 1779.97,
|
||||||
orderDate: "2021-11-22",
|
orderDate: "2021-11-22",
|
||||||
email: "pocketooth@gmail.com",
|
email: "pocketooth@gmail.com",
|
||||||
orderByFirstName: "John",
|
orderByFirstName: "John",
|
||||||
orderByLastName: "Huang",
|
orderByLastName: "Huang",
|
||||||
};
|
};
|
||||||
|
|
||||||
// re-arrange to make invoice detail a self-contained element
|
// constructs paypal order details into correct shape for db
|
||||||
const Orders = ({ orderID, setOrderID }) => {
|
const invoiceShape = (order) => {
|
||||||
|
// let addressInfo = order.purchase_units[0].shipping.address;
|
||||||
|
// let address = `${addressInfo.address_line_1} ${addressInfo.admin_area_2}, ${addressInfo.admin_area_1} ${addressInfo.postal_code}`;
|
||||||
|
return {
|
||||||
|
items: order.purchase_units.map((item) => {
|
||||||
|
return {
|
||||||
|
productName: item.description,
|
||||||
|
price: +item.amount.value
|
||||||
|
};
|
||||||
|
}),
|
||||||
|
totalAmount: order.purchase_units.reduce((a,b) => {
|
||||||
|
return a + +b.amount.value;
|
||||||
|
}, 0),
|
||||||
|
orderDate: order.create_time,
|
||||||
|
// email: order.payer.email_address,
|
||||||
|
orderByFirstName: order.payer.name.given_name,
|
||||||
|
orderByLastName: order.payer.name.surname,
|
||||||
|
// address: address
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
const Orders = ({ orderID, setOrderID, invoice }) => {
|
||||||
const orders = useGetOrders(orderID);
|
const orders = useGetOrders(orderID);
|
||||||
const [results, setOrder] = useSetOrder();
|
const [results, setOrder] = useSetOrder();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (results.data) {
|
if (results.data) {
|
||||||
let orderID = results.data.setOrderInfo.split(':').pop();
|
let orderID = results.data.setOrderInfo.split(':').pop();
|
||||||
setOrderID(orderID);
|
setOrderID(orderID);
|
||||||
console.log("results:",orderID)
|
console.log("results:", orderID)
|
||||||
}
|
}
|
||||||
}, [results, setOrderID]); //didUpdate
|
}, [results, setOrderID]); //didUpdate
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (invoice) {
|
||||||
|
console.log("Invoice: ", invoiceShape(invoice));
|
||||||
|
setOrder(invoiceShape(invoice));
|
||||||
|
}
|
||||||
|
}, [invoice]); //missing dep warning
|
||||||
|
|
||||||
const onClick = (event) => {
|
const onClick = (event) => {
|
||||||
setOrder(sampleOrder);
|
setOrder(sampleOrder);
|
||||||
console.log("sample order button clicked");
|
console.log("sample order button clicked");
|
||||||
|
Loading…
Reference in New Issue
Block a user