true, CURLOPT_POST => true, CURLOPT_POSTFIELDS => 'grant_type=client_credentials', CURLOPT_HTTPHEADER => ['Accept: application/json'], CURLOPT_USERPWD => $client_id . ':' . $client_secret, ]); $tok = curl_exec($ch); $http = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($http !== 200) { http_response_code(500); echo json_encode(['error'=>'oauth_fail']); exit; } $access = json_decode($tok, true)['access_token'] ?? null; if (!$access) { http_response_code(500); echo json_encode(['error'=>'oauth_no_token']); exit; } // 2) Build purchase unit $purchaseUnit = [ 'amount' => [ 'currency_code' => $currency, 'value' => $amount_value, ], 'description' => $description, // Critical for webhook reconciliation: 'invoice_id' => $invoice_id, 'custom_id' => $custom_id ]; // If items provided, include them and add a breakdown with item_total to match the overall amount if ($items) { $purchaseUnit['items'] = $items; $purchaseUnit['amount']['breakdown'] = [ 'item_total' => [ 'currency_code' => $currency, 'value' => $amount_value ] ]; } // (Optional) Persist your raw line_invoices server-side here if you wish. // For example, write to a DB keyed by $invoice_id so you can join later. // 3) Create order (intent = CAPTURE) $body = [ 'intent' => 'CAPTURE', 'purchase_units' => [ $purchaseUnit ], // Guides PayPal where to send the buyer if the flow becomes a full-page redirect 'application_context' => [ 'return_url' => $return_url, 'cancel_url' => $cancel_url, 'user_action' => 'PAY_NOW' ] ]; $ch = curl_init("$api/v2/checkout/orders"); curl_setopt_array($ch, [ CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true, CURLOPT_POSTFIELDS => json_encode($body), CURLOPT_HTTPHEADER => [ 'Content-Type: application/json', 'Authorization: Bearer ' . $access ], ]); $res = curl_exec($ch); $http = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($http !== 201) { http_response_code($http); echo $res; exit; } echo $res;