protected $apiUrl;
protected $clientId;
protected $secret;
public function __construct()
{
$mode = config('paypal.mode');
$paypalConfig = config('paypal.' . $mode);
$this->apiUrl = $mode === 'sandbox' ? 'https://api.sandbox.paypal.com' : 'https://api.paypal.com';
$this->clientId = $paypalConfig['client_id'];
$this->secret = $paypalConfig['secret'];
}
protected function makePaypalRequest($method, $endpoint, $data = [])
{
return Http::withBasicAuth($this->clientId, $this->secret)
->$method($this->apiUrl . $endpoint, $data);
}
public function capturePayment(Request $request)
{
// Extract orderId from the request body
$orderId = $request->input('orderId');
if (!$orderId) {
return response()->json(['error' => 'Order ID is required.'], 400);
}
try {
// Log the capture payment request
Log::channel('paypal')->info('Sending capture payment request', ['order_id' => $orderId]);
// Send the HTTP POST request to capture the payment using the makePaypalRequest method
// The request body is an empty JSON object
$response = $this->makePaypalRequest('post', "/v2/checkout/orders/{$orderId}/capture", []);
// Process the response
if ($response->successful()) {
$data = $response->json();
Log::channel('paypal')->info('Order captured successfully', ['order_id' => $orderId, 'response' => $data]);
return response()->json($data, 200);
} else {
return $this->handleApiError($response);
}
} catch (\Exception $e) {
Log::channel('paypal')->error('Exception caught in capture payment', ['message' => $e->getMessage()]);
return response()->json(['error' => 'An error occurred during the capture process.'], 500);
}
} and log [Removed. Phone #s not permitted]31:46] local.INFO: Sending capture payment request {"order_id":"9EF65198C1215791H"} [Removed. Phone #s not permitted]31:47] local.ERROR: PayPal API request failed: {"name":"INVALID_REQUEST","message":"Request is not well-formed, syntactically incorrect, or violates schema.","debug_id":"070d19afcea08","details":[{"field":"\/","location":"body","issue":"MALFORMED_REQUEST_JSON","description":"The request JSON is not well formed."}],"links":[{"href":"https:\/\/developer.paypal.com\/docs\/api\/orders\/v2\/#error-MALFORMED_REQUEST_JSON","rel":"information_link","encType":"application\/json"}]}
... View more