fork download
  1. <?php
  2.  
  3. function createTotalPayOrder($apiUrl, $apiKey, $orderData) {
  4. // Initialize cURL
  5. $ch = curl_init();
  6.  
  7. // Set cURL options
  8. curl_setopt($ch, CURLOPT_URL, $apiUrl . '/create-order'); // Adjust the endpoint as necessary
  9. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  10. curl_setopt($ch, CURLOPT_POST, true);
  11. curl_setopt($ch, CURLOPT_HTTPHEADER, [
  12. 'Content-Type: application/json',
  13. 'Authorization: Bearer ' . $apiKey,
  14. ]);
  15. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($orderData));
  16.  
  17. // Execute the request
  18. $response = curl_exec($ch);
  19.  
  20. // Check for cURL errors
  21. if ($response === false) {
  22. echo 'Curl error: ' . curl_error($ch);
  23. return null;
  24. }
  25.  
  26. // Close cURL session
  27. curl_close($ch);
  28.  
  29. // Decode the response
  30. return json_decode($response, true);
  31. }
  32.  
  33. // Usage example
  34. $apiUrl = 'https://c...content-available-to-author-only...y.global'; // TotalPay API URL
  35. $apiKey = '26581a8f244c2b2f16fc80caa2d7a58b'; // Your API key from TotalPay
  36.  
  37. // Prepare order data
  38. $orderData = [
  39. 'merchant_id' => '072f3aa2-1cfd-11ef-b318-7a03a3b9dc8b', // Your merchant ID
  40. 'amount' => 1000, // Amount in the smallest currency unit (e.g., cents)
  41. 'currency' => 'USD',
  42. 'order_id' => uniqid(), // Unique order ID
  43. 'description' => 'Order description here',
  44. 'redirect_url' => 'https://b...content-available-to-author-only...t.com/thank-you', // URL to redirect after payment
  45. 'cancel_url' => 'https://b...content-available-to-author-only...t.com/cancel', // URL for cancel action
  46. ];
  47.  
  48. // Create order
  49. $orderResponse = createTotalPayOrder($apiUrl, $apiKey, $orderData);
  50.  
  51. // Check the response
  52. if ($orderResponse) {
  53. if ($orderResponse['status'] === 'success') {
  54. // Redirect the user to the payment page
  55. header('Location: ' . $orderResponse['payment_url']);
  56. exit();
  57. } else {
  58. echo 'Order creation failed: ' . $orderResponse['message'];
  59. }
  60. } else {
  61. echo 'Failed to create order.';
  62. }
  63. ?>
  64.  
Success #stdin #stdout 0.03s 26392KB
stdin
Standard input is empty
stdout
Curl error: Could not resolve host: checkout.totalpay.globalFailed to create order.