I tried to get a response from an api the first function give me the response correctly but the second does not
I tried to send it with postman and it works and there is no errors with curl ...
I tried it in many faces but it does not working the code ::
this is the code link :
https://www.mediafire.com/file/41yyjpbfg3o567f/post.php/file
this is the payment Api flow link :
Its simple, just remove the space at the starting in the $url
:
$url = ' https://accept.paymob.com/api/ecommerce/orders';
Change this to
$url = 'https://accept.paymob.com/api/ecommerce/orders';
Rest all working fine, the output I received is below :
{
"id":124526360,
"created_at":"2023-05-26T14:52:37.742685",
"delivery_needed":false,
"merchant":{
"id":748368,
"created_at":"2023-04-17T02:22:09.851139",
"phones":[
"+201141138156"
],
"company_emails":[
"3ssare3@gmail.com"
],
"company_name":"AMS",
"state":"",
"country":"EGY",
"city":"Cairo",
"postal_code":"",
"street":""
},
"collector":null,
"amount_cents":100000,
"shipping_data":null,
"currency":"EGP",
"is_payment_locked":false,
"is_return":false,
"is_cancel":false,
"is_returned":false,
"is_canceled":false,
"merchant_order_id":null,
"wallet_notification":null,
"paid_amount_cents":0,
"notify_user_with_email":false,
"items":[
{
"name":"ASC1515",
"description":"Smart Watch",
"amount_cents":500000,
"quantity":1
},
{
"name":"ERT6565",
"description":"Power Bank",
"amount_cents":200000,
"quantity":1
}
],
"order_url":"https://accept.paymob.com/standalone/?ref=i_LRR2b0JmcFV1dmlyeUlncW5MbnhQZWFndz09X2V3b01nSnhlMmcvdk1HWllxRlRRUlE9PQ",
"commission_fees":0,
"delivery_fees_cents":0,
"delivery_vat_cents":0,
"payment_method":"tbc",
"merchant_staff_tag":null,
"api_source":"OTHER",
"data":{
},
"token":"LRR2b0JmcFV1dmlyeUlncW5MbnhQZWFndz09X2V3b01nSnhlMmcvdk1HWllxRlRRUlE9PQ",
"url":"https://accept.paymob.com/standalone/?ref=i_LRR2b0JmcFV1dmlyeUlncW5MbnhQZWFndz09X2V3b01nSnhlMmcvdk1HWllxRlRRUlE9PQ"
}
UPDATED ON AUTHOR'S REQUEST
Complete code extracted from your file. The lines I have edited has been commented for your understanding.
<?php
function authApi(){
$url = 'https://accept.paymob.com/api/auth/tokens';
$data = array('api_key' => 'ZXlKaGJHY2lPaUpJVXpVeE1pSXNJblI1Y0NJNklrcFhWQ0o5LmV5SmpiR0Z6Y3lJNklrMWxjbU5vWVc1MElpd2ljSEp2Wm1sc1pWOXdheUk2TnpRNE16WTRMQ0p1WVcxbElqb2lhVzVwZEdsaGJDSjkuSE1rMGw2RFlNY1pfX1dDQ1AxLVRvSVJQRktMRGZRQTFIUzZUbUNKbGlDNTdmQzFGLUQ0aGJNOHpmOTdvSHppYVlqQTI3bDRWUUs4MnRrd0VDejZyWUE=');
$body = json_encode($data);
$ch = curl_init();
if (!$ch) {
die("Failed to create cURL handle");
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Disable SSL certificate verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$response = curl_exec($ch);
if (curl_error($ch)) {
echo 'Curl error: ' . curl_error($ch);
} else{
$response_array = json_decode($response, true); // decode the JSON response to an associative array
global $token;
$token = $response_array["token"]; // extract the token from the array
orderRegister($token);
}
curl_close($ch);
}
function orderRegister($token) {
/*
This line has been edited by @KirsSudh
** removed space at the beginning of the string in $url var **
*/
$url = 'https://accept.paymob.com/api/ecommerce/orders'; // the URL of the API endpoint
$data = array('auth_token' => $token,
"delivery_needed"=> "false",
"amount_cents"=> "100000",
"currency"=> "EGP",
/*
Following lines has been added by @KirsSudh
** a multi-dim array has been added for debug purpose **
** the array data obtained from https://docs.paymob.com/docs/accept-standard-redirect#2-order-registration-api **
*/
"items"=>array(
(object)array(
"name"=>"ASC1515",
"amount_cents"=>"500000",
"description"=>"Smart Watch",
"quantity"=> "1"
),
(object)array(
"name"=>"ERT6565",
"amount_cents"=> "200000",
"description"=>"Power Bank",
"quantity"=>"1"
)
) // the data to be sent in the POST request
);
$body = json_encode($data);
$ch = curl_init();
if (!$ch) {
die("Failed to create cURL handle");
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$response_order = curl_exec($ch);
if (curl_error($ch)) {
echo 'Curl error: ' . curl_error($ch);
} else{
$response_array_order = json_decode($response_order, true); // decode the JSON response to an associative array
echo $response_order;
}
}
authApi();