Monnify Transaction Verification Issue, - php

I ma currently integrating the Monnify payment system but I'm getting errors.
Here is my code;
public function validatePayment(Request $request){
//dd($request->input());
$url = ENV('MONNIFY_BASE_URL').'/api/v2/transactions/'.urlencode($request->transactionReference);
//dd($url);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array(
"Content-Type: application/json",
"Authorization: Bearer ".base64_encode(ENV('MONNIFY_API_KEY').":".ENV("MONNIFY_SECRET_KEY")),
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($curl);
curl_close($curl);
$resp = json_decode($resp, true);
dd($resp);
}
When i die dump respose, this is what I get
^ array:2 [▼
"error" => "invalid_token"
"error_description" => "Cannot convert access token to JSON"
]
Here is a link to the documentation
https://teamapt.atlassian.net/wiki/spaces/MON/pages/213909851/Get+Transaction+Status
What could be problem?
For any one who wants to try this, here are the test keys
MONNIFY_API_KEY=MK_TEST_FWD6HTML35
MONNIFY_SECRET_KEY=5Y8ZBQMSEUXMTQMHFAV3U3A34NNYH1X5
MONNIFY_BASE_URL=https://sandbox.monnify.com
Also here the test transaction reference that I want to verify
MNFY|05|20220703011128|000322

Related

API Key in curl

So my question is the following, using xampp for localhost, using PHP 7+ and curl, i need to make an API post call. it was given to me an API Key, that i don t know how to validate. this is the code:
<?php
$curl = curl_init();
$data_array = array(
"amount" => '1',
"operative"=> "AUTHORIZATION",
"signature"=> "KKbAWfePbyt41T7inU8ulGtR",
"customer_ext_id"=> "IDInterno",
"additional"=> "Additional",
"service"=>"CE4C4D1C-2A2C-4AA7-BE59-62E497AEFE16",
"secure"=> true,
"url_post"=> "https://demo.pagaqui.pt:5135/RegisterCard.aspxOk: ",
"url_ok"=> "https://carteira.pagaqui.pt/repo/ok.htmlKO: ",
"url_ko"=> " https://carteira.pagaqui.pt/repo/nok.html ",
"template_uuid"=> "43D8016C-D28C-4F44-899A-2195CA8DC023",
"description"=> "Desc"
);
$data_array = http_build_query($data_array);
curl_setopt($curl, CURLOPT_URL, 'https://api-gateway.pagaqui.pt/v1/sandbox/payment');
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, ['content-type: application/json']);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_array);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
$err = curl_error($curl);
if($err){
echo 'Curl Error: ' . $err;
}
else {
print_r($response);
}
<?php
My response in the localhost is
{"message":"Unauthorized","code":401,"details":""}
Thank you for the attention.
Already solved it, just needed to create a variable to the key, and after that, inside curl_setopt($curl, CURLOPT_HTTPHEADER, array( "Authorization: Bearer ".$Key, 'Content-Type: application/json' ));
Thank you.

Cloudflare API issue put request ("code":9020,"message":"Invalid DNS record type")

I am having issue while using cloudflare APIv4. Trying to update a dns record and not sure why am I receiving following error:
           
{"success":false,"errors":[{"code":1004,"message":"DNS Validation Error","error_chain":[{"code":9020,"message":"Invalid DNS record type"}]}],"messages":[],"result":null}
Here is the PHP function:          
function updateCloudflareDNS($zone_id,$dns_id, $updatedata){
$updatedata = '[{"name":"**.****.com"},{"type":"A"},{"ttl":"1"},{"content":"8.8.8.8"},{"proxied":"true"}]';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.cloudflare.com/client/v4/zones/".$zone_id."/dns_records/".$dns_id);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8',
'X-Auth-Email: **********',
'X-Auth-Key: ***********'
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $updatedata);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
Also, I am putting record type "A" correctly as mentioned on the Cloudflare API documentation
Could someone help me out with this issue?
Thanks
You're sending a payload of:
[{"name":"**.****.com"},{"type":"A"},{"ttl":"1"},{"content":"8.8.8.8"},{"proxied":"true"}]
Here's what the API expects:
{"type":"A", "name":"example.com", "content":"127.0.0.1", "ttl":120, "priority":10,"proxied":false}
And this is how you properly construct JSON in PHP:
$POST = json_encode(array(
"type" => "A",
"name" => "...",
...
));

cURL Output in PHP

I have this curl output where I replace with my id, secret and url. It gives correct response.
curl -XPOST -H "Cache-Control:no-cache" -H "Content-Type:application/json" --user 'id:secret' 'url'
But when I'm trying to write it in php in the following way, it's giving error for invalid cleint. How to rewrite curl from the above output in the same way in php? Thanks!
$url = 'https://test.api.neteller.com/v1/oauth2/token?grant_type=client_credentials';
$clientId = 'client_id;
$clientSecret = 'secret_id';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERPWD, $clientId . ":" . $clientSecret);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
curl_close($curl);
It's giving error - 'invalid cleint', but when I run it in console, it returns access token. This code is only for access token, not for payment.
Edited: My current code is:
$url ='https://test.api.neteller.com/v1/oauth2/token?grant_type=client_credentials';
$clientId = NETELLER_CLIENT_ID;
$clientSecret = NETELLER_CLIENT_SECRET;
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array(
'Authorization: Basic '. base64_encode($clientId) . ':' . base64_encode($clientSecret),
'Content-Type:application/json'
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($curl);
if (curl_error($curl)) {
curl_close($curl);
throw new \Exception('System error. Can not get neteller token');
}
curl_close($curl);
Tried to add the credentials as part of the headers.
$headers = array(
'Authorization: Basic '. base64_encode($clientID . ':' . $clientSecret),
'Content-Type:application/json'
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
Ok I found out.
Update:
There are 4 reasons why you may get { "error": "invalid_client" } when trying to run your code.
You are using client_id + client_secret from your test account on the production environment or vice versa.
Your client_id or client_secret values are wrong.
The IP address from which your application is making outgoing requests is not white-listed in your merchant account.
You are not sending the Authorization header properly.
You missed curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type:application/json", "Cache-Control:no-cache")); from your code.

Converting Command line curl to PHP curl functions

I am unable to convert this command line curl to php:
Structure
curl -X POST https://kanbanflow.com/api/v1/tasks -H "Content-type: application/json"
-d '{ "<PROPERTY_1>": <PROPERTY_VALUE_1>, "<PROPERTY_2>": <PROPERTY_VALUE_2>, ... "<PROPERTY_N>": <PROPERTY_VALUE_N> }'
Example in api documentation
curl -X POST https://kanbanflow.com/api/v1/tasks -H "Content-type: application/json"
-d '{ "name": "Write report", "columnId": "7ca19de0403f11e282ebef81383f3229", "color": "red" }'
I can't understand what is -d here? and how to pass data in this format.
so far i reach here but its not working.
Updated Code
$data = json_encode(array('name'=>'Testing of api', 'columnId' =>"xxxxxxxxxxxxxxxxxxx",'color'=>"red"));
$token = base64_encode("apiToken:xxxxxxxxxxxxxxxxxxxxxxxxx");
$headers = array(
"Authorization: Basic " . $token,
"Content-type: application/json"
);
$url = "https://kanbanflow.com/api/v1/tasks";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
echo "<pre>";
print_r($response);
Suggest me where i am wrong..
Update
I want to use https://kanbanflow.com api to add task
Response: {"errors":[{"message":"Unexpected error"}]}
I got solution of my problem i was missing swimlaneId attribute. I am posting code here for future reference so it will help others.
Now sending data using object like following
$object = new stdClass();
$object->name = 'Testing Task';
$object->columnId = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$object->swimlaneId = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx';
$object->color = 'green';
$url = "https://kanbanflow.com/api/v1/tasks";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($object));
$response = curl_exec($curl);
Array of Json_encode is also working here
$data = json_encode(array('name'=>'Testing of api', 'columnId' =>"f648831061e111e3a41aa3dbd7a40406", 'color'=>'red', 'swimlaneId' => 'd0635fc061e711e3a41aa3dbd7a40406'));
Looks like you forgot to json encode your data
$data = json_encode(array('name'=>'Testing of api', 'columnId' =>"xxxxxxxxxxxxxxxxxxx",'color'=>"red"));

Not being able to authenticate Google Visualization Query Language with given Authentication Key

Not being able to authenticate Google Spreadsheet API.
I'm using this code stolen from here,
$clientlogin_url = "https://www.google.com/accounts/ClientLogin";
$clientlogin_post = array(
"accountType" => "HOSTED_OR_GOOGLE",
"Email" => "MY_EMAIL#GOOGLE.COM",
"Passwd" => "MY_EMAIL_PASS",
"service" => "writely",
"source" => "MY_APPLICATION_NAME"
);
// Initialize the curl object
$curl = curl_init($clientlogin_url);
// Set some options (some for SHTTP)
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $clientlogin_post);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// Execute
$response = curl_exec($curl);
// Get the Auth string and save it
preg_match("/Auth=([a-z0-9_\-]+)/i", $response, $matches);
$auth = $matches[1];
echo "The auth string is: " . $auth; //this worked!
So this works and now I have a key But actually using it...with the bellow code gives me a user not authenticated error:
$headers = array(
"Authorization: GoogleLogin auth=" . $auth,
"GData-Version: 3.0",
);
$key = 'MY_KEY';
// Make the request
curl_setopt($curl, CURLOPT_URL, 'https://spreadsheets.google.com/tq?tqx=version:0.6;out:json&key='.$key);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, false);
$response = curl_exec($curl);
curl_close($curl);
var_dump ($response);
this gives me
string(272) "google.visualization.Query.setResponse({version:'0.6',status:'error',errors:[{reason:'user_not_authenticated',message:'User not signed in',detailed_message:'\u003ca target=\u0022_blank\u0022 href=\u0022http://spreadsheets.google.com/\u0022\u003eSign in\u003c/a\u003e'}]});"
So apparently it is not able to use the authentication key for the Google Visualization Query Language.. what am I doing wrong?
Thank you so much!
I would try the following modifications in the code that uses google authentication string:
add urlencode
$headers = array(
"Authorization: GoogleLogin auth=" . urlencode($auth),
"GData-Version: 3.0",
);
and skip verifying SSL sertificates:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
I've described my experience implementing authentication with CURL in this post. There you'll find some tips on debugging CURL requests.

Categories