import React, { useState, useEffect } from 'react';
/* eslint-disable no-unused-vars */
import { getProducts, getBraintreeClientToken, processPayment, createOrder } from './apiCore';
import { emptyCart } from './cartHelpers';
import Card from './Card';
import { isAuthenticated } from '../auth';
import { Link } from 'react-router-dom';
// import "braintree-web"; // not using this package
import DropIn from 'braintree-web-drop-in-react';
const Checkout = ({ products, setRun = f => f, run = undefined }) => {
const [data, setData] = useState({
loading: false,
success: false,
clientToken: null,
error: '',
instance: {},
address: ''
});
const userId = isAuthenticated() && isAuthenticated().user._id;
const token = isAuthenticated() && isAuthenticated().token;
const getToken = (userId, token) => {
getBraintreeClientToken(userId, token).then(data => {
if (data.error) {
console.log(data.error);
setData({ ...data, error: data.error });
} else {
console.log(data);
setData({ clientToken: data.clientToken });
}
});
};
useEffect(() => {
getToken(userId, token);
// eslint-disable-next-line
}, []);
const handleAddress = event => {
setData({ ...data, address: event.target.value });
};
const getTotal = () => {
return products.reduce((currentValue, nextValue) => {
return currentValue + nextValue.count * nextValue.price;
}, 0);
};
const showCheckout = () => {
return isAuthenticated() ? (
{showDropIn()}
) : (
);
};
let deliveryAddress = data.address;
const buy = () => {
setData({ loading: true });
// send the nonce to your server
// nonce = data.instance.requestPaymentMethod()
let nonce;
let getNonce = data.instance.requestPaymentMethod()
.then(data => {
// console.log(data);
nonce = data.nonce;
// once you have nonce (card type, card number) send nonce as 'paymentMethodNonce'
// and also total to be charged
// console.log(
// "send nonce and total to process: ",
// nonce,
// getTotal(products)
// );
const paymentData = {
paymentMethodNonce: nonce,
amount: getTotal(products)
};
processPayment(userId, token, paymentData)
.then(response => {
console.log(response);
// empty cart
// create order
const createOrderData = {
products: products,
transaction_id: response.transaction.id,
amount: response.transaction.amount,
address: deliveryAddress
};
createOrder(userId, token, createOrderData)
.then(response => {
emptyCart(() => {
setRun(!run); // run useEffect in parent Cart
console.log('payment success and empty cart');
setData({
loading: false,
success: true
});
});
})
.catch(error => {
console.log(error);
setData({ loading: false });
});
})
.catch(error => {
console.log(error);
setData({ loading: false });
});
})
.catch(error => {
// console.log("dropin error: ", error);
setData({ ...data, error: error.message });
});
};
const showDropIn = () => (
setData({ ...data, error: '' })}>
{data.clientToken !== null && products.length > 0 ? (
(data.instance = instance)}
/>
) : null}
);
const showError = error => (
{error}
);
const showSuccess = success => (
Thanks! Your payment was successful!
);
const showLoading = loading => loading && Loading...
;
return (
Total: ${getTotal()}
{showLoading(data.loading)}
{showSuccess(data.success)}
{showError(data.error)}
{showCheckout()}
);
};
export default Checkout;