Skip to content

Commit

Permalink
stripe payment fix
Browse files Browse the repository at this point in the history
  • Loading branch information
HetavShah committed May 26, 2023
1 parent 260633c commit ed2fcca
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 31 deletions.
5 changes: 3 additions & 2 deletions client/src/actions/orderActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export const getOrderDetails = (id) => async (dispatch, getState) => {
};

export const payOrder =
(orderId, paymentResult) => async (dispatch, getState) => {
(orderId, token) => async (dispatch, getState) => {

try {

Expand All @@ -117,7 +117,8 @@ export const payOrder =


const { data } = await axios.post('/api/payments', {
orderId
orderId: orderId,
tokenId:token.id
});


Expand Down
31 changes: 4 additions & 27 deletions client/src/screens/OrderScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,29 +52,6 @@ const OrderScreen = ({}) => {
const userLogin = useSelector((state) => state.userLogin);
const { userInfo } = userLogin;

const handleToken = async (token, amount) => {
try {
console.log(amount);
const res = await axios.post('/api/payments', {
orderId: order.id,
});
const { id } = res.data;
console.log(res.data);
if (id) {
toast('Success ! Check emails for details', {
type: 'success',
});
} else {
toast('Something went wrong', {
type: 'failure',
});
}
} catch (err) {
toast('Something went wrong', {
type: 'failure',
});
}
};

if (!loading) {
// Calculate prices
Expand Down Expand Up @@ -119,9 +96,9 @@ const OrderScreen = ({}) => {
dispatch(deliverOrder(order));
};

const successPaymentHandler = (paymentResult) => {
console.log(paymentResult);
dispatch(payOrder(id, paymentResult));
const handleToken = (token) => {
console.log(token);
dispatch(payOrder(id, token));
};

if (timeLeft < 0) {
Expand Down Expand Up @@ -238,7 +215,7 @@ const OrderScreen = ({}) => {
{loadingPay && <Loader />}

<StripeCheckout
token={successPaymentHandler}
token={handleToken}
stripeKey="pk_test_JMdyKVvf8EGTB0Fl28GsN7YY"
amount={order.totalPrice * 100}
email={userInfo.email}
Expand Down
10 changes: 8 additions & 2 deletions payment/src/routes/create-payment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@ requireAuth,
body('orderId')
.not()
.isEmpty()
.withMessage("Order Id is required")
.withMessage("Order Id is required"),
body("tokenId")
.not()
.isEmpty()
.withMessage("token is required")
],validateRequest,
async (req:Request, res:Response)=>{
const {orderId}=req.body;
const {orderId,tokenId}=req.body;
const order=await Order.findById(orderId);

if(!order){throw new NotFoundError()};
Expand All @@ -40,6 +44,8 @@ async (req:Request, res:Response)=>{
amount: order.totalPrice*100,
currency: 'inr',
payment_method_types: ['card'],
confirm:true,
payment_method:tokenId
});
if(!paymentIntent.id) throw new Error("Payment Failed");
const payment=Payment.build({
Expand Down

0 comments on commit ed2fcca

Please sign in to comment.