使用curl授权并连接到API


Authorizing and Connecting to API with curl

我有一个名为信用卡的类和一个用于输入零售商在线证书付款的表单。但是该交易一直被支付网关以错误300拒绝。

//creditCards class 
class CreditCards extends GLOBALS {

 // Prepares info before sending to API
function doSale($order_id, $amount, $ccnumber, $exp_month, $exp_year,           

$cvv,$cc_name, $email,$phone) {
$cvv="";
$type="sale";
$payment = 'cc';
list($firstname, $lastname) = explode(' ', $cc_name);

  $amount = number_format((float)$amount,2,".","");
  $ipaddress=$_SERVER['REMOTE_ADDR'];
  $ccexp = $exp_month."/".$exp_year;
  $payment = 'cc';
  $redirect = 'https://www.example.com/example/';
  $key_id="45565343";
  $key="mG3G2z3WjfjfahkjsdhkdPdmaqN";
  $time = time();
  $hash = md5($order_id."|".$amount."|".$time."|".$key);

 $query  = "";
 $query .= "type=".urlencode($type)."&";
 $query .= "key_id=".urlencode($key_id)."&";
 $query .= "hash=".urlencode($hash)."&";
 $query .= "time=".urlencode($time)."&";
 $query .= "redirect=".urlencode($redirect)."&";      
 $query .= "orderid=".urlencode($order_id)."&";
 $query .= "ccnumber=".urlencode($ccnumber)."&";
 $query .= "ccexp=".urlencode($ccexp)."&";
 $query .= "amount=".urlencode($amount)."&";
 $query .= "ipaddress=".urlencode($ipaddress)."&";  
 $query .= "firstname=".urlencode($firstname)."&";
 $query .= "lastname=".urlencode($lastname)."&";
 $query .= "address1=".urlencode($address1)."&";
 $query .= "phone=".urlencode($phone)."&";
 $query .= "email=".urlencode($email);   
  //URl API 
  $url = 'https://api.api.com/api/api.php';
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_VERBOSE, 0);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
  curl_setopt($ch, CURLOPT_HEADER,1);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_NOPROGRESS, 1);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION,0);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);  
  curl_setopt($ch, CURLOPT_FRESH_CONNECT,0);  
  $data = curl_exec($ch);
  //echo '<pre>';
  //var_dump($data);
  $response = curl_getinfo($ch);

   //if respone is invalid it prints the error message
  if(!$response){print_r('Error .  '.curl_error($ch));}
  curl_close($ch); 
  unset($ch);
    $data = explode("&",$data);
    for($i=0;$i<count($data);$i++) {
    $rdata = explode("=",$data[$i]);
    $this->responses[$rdata[0]] = $rdata[1];
}
 return $this->responses['response_code'];
}   


 // test for data accuracy
function doauth($order_id, $amount, $ccnumber, $exp_month, $exp_year,        $cvv,$cc_name, $email,$phone) {
 $amount=1;
$order_id=1;
$cvv="";
$type="sale";
$payment = 'cc';
list($firstname, $lastname) = explode(' ', $cc_name);

$amount = number_format((float)$amount,2,".","");
 $ipaddress=$_SERVER['REMOTE_ADDR'];
 $ccexp = $exp_month."/".$exp_year;
 $payment = 'cc';
 $redirect = 'https://www.example.com/example/';
 $key_id="3780731";
 $key="mG3G2z3W3625JRguQ2Qp4s68CbPdmaqN";
 $time = time();
 $hash = md5($order_id."|".$amount."|".$time."|".$key);

   $query  = "";
   $query .= "type=".urlencode($type)."&";
   $query .= "key_id=".urlencode($key_id)."&";
   $query .= "hash=".urlencode($hash)."&";
   $query .= "time=".urlencode($time)."&";
   $query .= "redirect=".urlencode($redirect)."&";      
   $query .= "orderid=".urlencode($order_id)."&";
   $query .= "ccnumber=".urlencode($ccnumber)."&";
   $query .= "ccexp=".urlencode($ccexp)."&";
   $query .= "amount=".urlencode($amount)."&";
   $query .= "ipaddress=".urlencode($ipaddress)."&";  
   $query .= "firstname=".urlencode($firstname)."&";
   $query .= "lastname=".urlencode($lastname)."&";
   $query .= "address1=".urlencode($address1)."&";
   $query .= "phone=".urlencode($phone)."&";
   $query .= "email=".urlencode($email);   

   $url = 'https://apimaker.apimaker.com/api/transact.php';
   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, $url);
   curl_setopt($ch, CURLOPT_VERBOSE, 0);
   curl_setopt($ch, CURLOPT_POST, 1);
   curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
   curl_setopt($ch, CURLOPT_HEADER,1);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($ch, CURLOPT_NOPROGRESS, 1);
   curl_setopt($ch, CURLOPT_FOLLOWLOCATION,0);
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);  
curl_setopt($ch, CURLOPT_FRESH_CONNECT,0);  
   $data = curl_exec($ch);
   //echo '<pre>';
   //var_dump($data);
     $response = curl_getinfo($ch);
  if(!$response){print_r('Error al enviar autorización a Credomatic.      '.curl_error($ch));}
  curl_close($ch); 
  unset($ch);

有人能给我指一个正确的方向吗?关于开关功能可能无法正常运行。或者如果我必须为我的API重新限定URL。

由于返回了一个与重定向有关的300错误,您是否尝试过更改:curl_setopt($ch,CURLOPT_FOLLOWLOCATION,0);收件人:curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);