3 posts / 0 new
Last post
davidenz6632
PHP, Curl and the Bad Request

I'm trying to use PHP and Curl to integrate with the Payeezy API. The Curl sample below works well and it works in Postman also, but when using PHP to do the same I receive Bad Request.

This works -

curl -X POST -H "Co02641994c1e55e7cdf4c02b6" -d '{ "apikey:y6pWAJNyJyjGv66IsVuWnklkKUPFbb0a" -H "token:fdoa-a480ce8951daa73262734cf10
"merchant_ref": "Astonishing-Sale",
"transaction_type": "authorize",
"method": "credit_card",
"amount": "1299",
"currency_code": "USD",
"credit_card": {
"type": "visa",
"cardholder_name": "John Smith",
"card_number": "4788250000028291",
"exp_date": "1019",
"cvv": "123"
}
}' https://api-cert.payeezy.com/v1/transactions

This does not work -

$url = 'https://api-cert.payeezy.com/v1/transactions';

$fields = array(
'merchant_ref'=>'Astonishing-Sale',
'transaction_type'=>'authorize',
'method'=>'credit_card',
'amount'=>'1299',
'currency_code'=>'USD',
'credit card'=>array(
'type'=>'visa',
'cardholder_name'=>'John Smith',
'card_number'=>'4788250000028291',
'exp_date'=>'1019',
'cvv'=>'123'
)

);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type'=>'application/json','apikey'=>'y6pWAJNyJyjGv66IsVuWnklkKUPFbb0a','token'=>'fdoa-a480ce8951daa73262734cf102641994c1e55e7cdf4c02b6'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch);

curl_close($ch);

$return = json_decode($result);
var_dump($return);

I've tried different configurations for Curl, but the PHP output is always -

object(stdClass)#1 (2) {
["code"]=>
string(3) "400"
["message"]=>
string(11) "Bad Request"
}


rohitrajagopal3538
Re: PHP, Curl and the Bad Request

Try this code, I tested and it works. If it doesn't work, please paste the complete API response.

 

<?php

#require_once('payeezy-include.php');

$apiKey = "1AXHp1dY6JjJ29Eak5z1v7sFwmji1pvG";

$apiSecret = "2a9e90250949e4eefd1f7ca9ac2c8cade3a81c256006eca6f55f80778c12939a";

$token = "fdoa-a480ce8951daa73262734cf102641994c1e55e7cdf4c02b6";

$nonce = strval(hexdec(bin2hex(openssl_random_pseudo_bytes(4, $cstrong))));

$timestamp = strval(time()*1000); //time stamp in milli seconds

 

$payload = getPayload();

 

  /**

   * Generate Payload

   */

 

   function getPayload($args = array())

  {

    $data = "";

   

    $data = array(

              'merchant_ref'=> 'sample txn',

              'transaction_type'=> "authorize",

              'method'=> 'credit_card',

              'amount'=> '200',

              'currency_code'=> 'USD',

              'credit_card'=> array(

                      'type'=> 'visa',

                      'cardholder_name'=> 'JohnSmith',

                      'card_number'=> '4788250000028291',

                      'exp_date'=> '1030',

                      'cvv'=> '123',

                    )

    );

  

    return json_encode($data, JSON_FORCE_OBJECT);

  }

 

$data = $apiKey . $nonce . $timestamp . $token . $payload;

 

$hashAlgorithm = "sha256";

 

### Make sure the HMAC hash is in hex -->

$hmac = hash_hmac ( $hashAlgorithm , $data , $apiSecret, false );

 

### Authorization : base64 of hmac hash -->

$hmac_enc = base64_encode($hmac);

 

$curl = curl_init('https://api-cert.payeezy.com/v1/transactions');

 

$headers = array(

      'Content-Type: application/json',

      'apikey:'.strval($apiKey),

      'token:'.strval($token),

      'Authorization:'.$hmac_enc,

      'nonce:'.$nonce,

      'timestamp:'.$timestamp,

    );

 

curl_setopt($curl, CURLOPT_HEADER, false);

curl_setopt($curl, CURLOPT_POST, true);

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);

 

curl_setopt($curl, CURLOPT_VERBOSE, true);

curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

 

$json_response = curl_exec($curl);

 

$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

 

$response = json_decode($json_response, true);

 

if ( $status != 201 ) {

die("Error: call to URL $serviceURL failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));

}

 

curl_close($curl);

echo "JSON response is: ".$json_response."\n";

?>

Regards,

Payeezy team


davidenz6632
Re: PHP, Curl and the Bad Request

Thank you, rohitrajagopal3538.

Your code worked for me. I will use it as a base to work from.