加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 使用curl授权和连接到API

发布时间:2020-12-13 22:24:46 所属栏目:PHP教程 来源:网络整理
导读:我有一个名为信用卡的类和一个用于为零售商输入在线证书付款的表格.但是,交易不断被支付网关拒绝,错误为300. //creditCards class class CreditCards extends GLOBALS { // Prepares info before sending to APIfunction doSale($order_id,$amount,$ccnumber
我有一个名为信用卡的类和一个用于为零售商输入在线证书付款的表格.但是,交易不断被支付网关拒绝,错误为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,CURLOPT_NOPROGRESS,CURLOPT_FOLLOWLOCATION,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,$phone) {
 $amount=1;
$order_id=1;
$cvv="";
$type="sale";
$payment = 'cc';
list($firstname,$cc_name);


$amount = number_format((float)$amount,"");

 $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,$url);
   curl_setopt($ch,0);
   curl_setopt($ch,$query);
   curl_setopt($ch,0);  
curl_setopt($ch,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,0); 至: ????curl_setopt($ch,1);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读