使用curl授权和连接到API

时间:2015-12-09 04:58:50

标签: php api curl e-commerce

我有一个名为信用卡的类和一个用于为零售商输入在线证书付款的表格。但是,交易不断被支付网关拒绝,错误为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。

1 个答案:

答案 0 :(得分:0)

由于您收到300错误,这与重定向有关,您是否尝试过更改:     curl_setopt($ ch,CURLOPT_FOLLOWLOCATION,0); 至:     curl_setopt($ ch,CURLOPT_FOLLOWLOCATION,1);