everything semi working before sample implementation

This commit is contained in:
twotalesanimation
2025-12-30 00:09:14 +02:00
parent 00b8ff77b2
commit 3c24c0e9ef
39 changed files with 1072 additions and 1615 deletions
+142 -44
View File
@@ -164,36 +164,58 @@ class CustomOrderController extends Controller
*/
public function depositPayment(Request $request)
{
Log::info('Deposit payment initiated', [
'request_data' => $request->all(),
'user_id' => auth()->id(),
]);
$validated = $request->validate([
'custom_order_id' => 'required|exists:custom_orders,id',
]);
Log::info('Deposit payment validation passed', $validated);
$customOrder = CustomOrder::findOrFail($validated['custom_order_id']);
// Check authorization
if ($customOrder->user_id !== auth()->id()) {
Log::warning('Unauthorized deposit payment attempt', [
'custom_order_id' => $customOrder->id,
'user_id' => auth()->id(),
]);
abort(403);
}
// Check if already paid
if ($customOrder->deposit_status === 'paid') {
Log::info('Deposit already paid for custom order', [
'custom_order_id' => $customOrder->id,
]);
return redirect()->route('custom-orders.show', $customOrder)
->with('info', 'Deposit already paid for this order.');
}
// Initiate Yoco payment for deposit
Log::info('Initiating Yoco payment for custom order deposit', [
'custom_order_id' => $customOrder->id,
'deposit_amount' => $customOrder->deposit_amount,
]);
$yocoResponse = $this->initiateYocoPayment(
amount: (int)($customOrder->deposit_amount * 100), // Convert to cents
orderId: $customOrder->uuid,
customOrder: $customOrder,
orderType: 'custom_deposit',
description: "Deposit for Custom {$customOrder->type} Order #{$customOrder->order_number}"
description: "Deposit for Order #{$customOrder->order_number}"
);
if (!$yocoResponse) {
Log::error('Failed to initiate Yoco payment for custom order deposit', [
'custom_order_id' => $customOrder->id,
]);
return redirect()->route('custom-orders.show', $customOrder)
->with('error', 'Failed to initiate payment. Please try again.');
}
Log::info('Yoco payment initiated successfully for custom order deposit', [
'custom_order_id' => $customOrder->id,
'checkout_url' => $yocoResponse['checkout_url'],
]);
return redirect($yocoResponse['checkout_url']);
}
@@ -207,11 +229,11 @@ class CustomOrderController extends Controller
abort(403);
}
// Update order status
$customOrder->update([
'deposit_status' => 'paid',
'status' => 'submitted',
]);
// // Update order status
// $customOrder->update([
// 'deposit_status' => 'paid',
// 'status' => 'submitted',
// ]);
return view('custom-orders.deposit-success', [
'customOrder' => $customOrder,
@@ -250,67 +272,143 @@ class CustomOrderController extends Controller
/**
* Initiate Yoco payment
*/
private function initiateYocoPayment($amount, $orderId, $orderType, $description)
private function initiateYocoPayment($amount, $customOrder, $orderType, $description)
{
$yocoSecret = config('services.yoco.secret_key');
Log::info('Initiating Yoco payment', [
'order_id' => $customOrder->uuid,
'order_type' => $orderType,
'amount' => $amount,
'description' => $description,
]);
if (!$yocoSecret) {
if (!$customOrder) {
Log::error('Custom order not found for Yoco payment', [
'order_id' => $customOrder->uuid ?? 'unknown',
]);
return null;
}
Log::info('Custom order found for Yoco payment', [
'order_id' => $customOrder->uuid,
'custom_order_data' => $customOrder->toArray(),
]);
// Get configuration
$secretKey = config('services.yoco.secret_key');
$mode = config('services.yoco.mode');
Log::info('Yoco configuration', [
'mode' => $mode,
'secret_key_set' => !empty($secretKey) && $secretKey !== 'sk_test_your_key_here',
]);
// Check if API key is configured
if (empty($secretKey) || $secretKey === 'sk_test_your_key_here') {
Log::error('Yoco secret key not configured');
return null;
}
$payload = [
$baseUrl = $mode === 'live'
? 'https://payments.yoco.com/api/checkouts'
: 'https://payments.yoco.com/api/checkouts';
$checkoutData = [
'amount' => $amount,
'currency' => 'ZAR',
'successUrl' => route('yoco-custom-deposit-success', ['customOrder' => $orderId]),
'failureUrl' => route('custom-orders.show', ['customOrder' => $orderId]),
'cancelUrl' => route('custom-orders.show', ['customOrder' => $orderId]),
'successUrl' => route('yoco-custom-deposit-success', ['customOrder' => $customOrder->uuid]),
'cancelUrl' => route('custom-orders.show', ['customOrder' => $customOrder->uuid]),
'failureUrl' => route('custom-orders.show', ['customOrder' => $customOrder->uuid]),
'metadata' => [
'order_uuid' => $orderId,
'order_uuid' => $customOrder->uuid,
'order_type' => $orderType,
],
'description' => $description,
'site' => 'additional_design',
'description' => $description
]
];
Log::info('Yoco checkout data prepared', [
'order_id' => $customOrder->uuid,
'checkout_data' => $checkoutData,
]);
// Make API request to Yoco
try {
Log::info('Initiating Yoco payment', [
'amount' => $amount,
'orderId' => $orderId,
'description' => $description
]);
$response = Http::withHeaders([
'Authorization' => 'Bearer ' . $yocoSecret,
'Authorization' => 'Bearer ' . $secretKey,
'Content-Type' => 'application/json',
])->post('https://payments.yoco.com/api/checkouts', $payload);
Log::info('Yoco API response', [
'status' => $response->status(),
'body' => $response->body()
]);
])->post($baseUrl, $checkoutData);
if ($response->successful()) {
$data = $response->json();
Log::info('Yoco payment success', [
'checkout_url' => $data['redirectUrl'] ?? 'N/A',
'checkout_id' => $data['id'] ?? 'N/A'
$checkout = $response->json();
$checkoutId = $checkout['id'] ?? null;
$redirectUrl = $checkout['redirectUrl'] ?? null;
Log::info('Yoco checkout created for custom order', [
'order_uuid' => $customOrder->uuid,
'checkout_id' => $checkoutId,
'redirect_url' => $redirectUrl,
]);
if (!$checkoutId || !$redirectUrl) {
Log::error('Invalid Yoco checkout response: missing id or redirectUrl', [
'order_uuid' => $customOrder->uuid,
'response' => $checkout,
]);
throw new \Exception('Invalid Yoco checkout response: missing id or redirectUrl');
}
// Persist checkout info to database
try {
$customOrder->update([
'yoco_checkout_id' => $checkoutId,
'yoco_redirect_url' => $redirectUrl,
'yoco_checkout_response' => json_encode($checkout),
]);
// Reload the model to verify update
$customOrder->refresh();
if ($customOrder->yoco_checkout_id) {
Log::info('Yoco checkout info saved successfully for custom order', [
'order_uuid' => $customOrder->uuid,
'yoco_checkout_id' => $customOrder->yoco_checkout_id,
]);
} else {
Log::warning('Yoco checkout ID not saved after update for custom order', [
'order_uuid' => $customOrder->uuid,
'order_data' => $customOrder->toArray(),
]);
}
} catch (\Exception $dbException) {
Log::error('Database error while saving Yoco checkout info for custom order', [
'order_uuid' => $customOrder->uuid,
'error_message' => $dbException->getMessage(),
'error_code' => $dbException->getCode(),
'checkout_id' => $checkoutId,
'redirect_url' => $redirectUrl,
]);
throw $dbException;
}
return [
'checkout_url' => $data['redirectUrl'],
'checkout_id' => $data['id'],
'checkout_url' => $redirectUrl,
'checkout_id' => $checkoutId,
];
} else {
Log::error('Yoco payment API error', [
Log::error('Yoco API Error for custom order', [
'order_uuid' => $customOrder->uuid,
'status' => $response->status(),
'body' => $response->body()
]);
return null;
}
} catch (\Exception $e) {
Log::error('Yoco payment exception: ' . $e->getMessage(), [
'trace' => $e->getTraceAsString()
Log::error('Yoco Payment Exception for custom order', [
'order_uuid' => $customOrder->uuid,
'message' => $e->getMessage()
]);
return null;
}
return null;
}
}