77use App \Models \Transaction ;
88use Illuminate \Http \Request ;
99use App \Http \Controllers \Controller ;
10+ use App \Services \CheckoutService ;
1011use App \Services \PaymentGatewayService ;
1112use Illuminate \Support \Str ;
1213use Illuminate \Support \Facades \Log ;
1314use Exception ;
1415
1516class CheckoutController extends Controller
1617{
18+ protected CheckoutService $ service ;
19+
20+ public function __construct (CheckoutService $ service ){
21+ $ this ->service = $ service ;
22+ }
1723 // public function createOrder(Request $request)
1824 // {
1925 // try {
@@ -60,6 +66,13 @@ class CheckoutController extends Controller
6066 // }
6167 // }
6268
69+ /**
70+ * Create a Stripe PaymentIntent for the order.
71+ * This method remains the same and is specifically for Stripe payments.
72+ *
73+ * @param Request $request
74+ * @return \Illuminate\Http\JsonResponse
75+ */
6376 public function createStripeIntent (Request $ request )
6477 {
6578 try {
@@ -72,16 +85,7 @@ public function createStripeIntent(Request $request)
7285 'shipping ' => json_encode ($ request ->shipping ),
7386 ]);
7487
75- $ transaction = Transaction::create ([
76- 'user_id ' => auth ()->id (),
77- 'type ' => 'payment ' ,
78- 'transaction_id ' => $ charge ['transaction_id ' ] ?? null ,
79- 'amount ' => $ charge ['amount ' ],
80- 'currency ' => $ charge ['currency ' ],
81- 'status ' => 'pending ' ,
82- 'gateway ' => 'stripe ' ,
83- 'meta ' => json_encode ($ charge ['meta ' ] ?? []),
84- ]);
88+ $ transaction = $ this ->service ->createTransactionFromStripeCharge ($ charge );
8589
8690 return response ()->json ([
8791 'client_secret ' => $ charge ['client_secret ' ],
@@ -93,55 +97,27 @@ public function createStripeIntent(Request $request)
9397 }
9498 }
9599
100+ /**
101+ * Confirms the payment and creates the order, handling both Stripe and Cash on Delivery.
102+ * The payment confirmation logic has been updated to be more flexible and optimized.
103+ *
104+ * @param Request $request
105+ * @return \Illuminate\Http\JsonResponse
106+ */
96107 public function confirmPayment (Request $ request )
97108 {
98109 try {
99- $ transaction = Transaction::where ('transaction_id ' , $ request ->transaction_id )
100- ->where ('status ' , 'pending ' )
101- ->firstOrFail ();
102-
103- $ meta = json_decode ($ transaction ->meta , true );
104110 $ cart = session ()->get ('cart ' );
105111
112+ // Check for empty cart once
106113 if (!$ cart || empty ($ cart ['items ' ])) {
107114 return response ()->json (['error ' => 'Cart is empty. ' ], 400 );
108115 }
109116
110- $ order = Order::create ([
111- 'user_id ' => $ transaction ->user_id ,
112- 'order_number ' => 'ORD- ' . strtoupper (Str::random (10 )),
113- 'status ' => 'processing ' ,
114- 'payment_status ' => 'paid ' ,
115- 'subtotal ' => $ cart ['subtotal ' ] ?? $ cart ['total ' ],
116- 'discount ' => $ cart ['discount ' ] ?? 0 ,
117- 'tax ' => $ cart ['tax ' ] ?? 0 ,
118- 'shipping_cost ' => $ cart ['shipping_cost ' ] ?? 0 ,
119- 'total ' => $ cart ['total ' ],
120- 'currency ' => $ transaction ->currency ,
121- 'payment_gateway ' => 'stripe ' ,
122- 'shipping_address ' => json_encode ($ meta ['shipping ' ] ?? []),
123- 'billing_address ' => json_encode ($ meta ['billing ' ] ?? null ),
124- ]);
125-
126- foreach ($ cart ['items ' ] as $ item ) {
127- OrderItem::create ([
128- 'order_id ' => $ order ->id ,
129- 'product_id ' => $ item ['product_id ' ],
130- 'name ' => $ item ['title ' ],
131- 'quantity ' => $ item ['quantity ' ],
132- 'price ' => $ item ['price ' ],
133- 'total ' => $ item ['price ' ] * $ item ['quantity ' ],
134- 'meta ' => json_encode ($ item ['meta ' ] ?? null ),
135- ]);
136- }
117+ $ this ->service ->handlePaymentConfirmation ($ request , $ cart );
137118
138- $ transaction ->update ([
139- 'order_id ' => $ order ->id ,
140- 'status ' => 'paid ' ,
141- ]);
119+ // Clear the cart session after a successful order creation
142120 session ()->forget ('cart ' );
143-
144-
145121 return response ()->json (['success ' => true ]);
146122 } catch (\Exception $ e ) {
147123 Log::error ('Payment confirmation failed ' , ['error ' => $ e ->getMessage ()]);
0 commit comments