【笔记】PHP对接支付接口参考代码 含PHP发送POST 字符串和JSON

先记录下 curl 函数请求构造,如果你的系统框架没内置函数,就需要加上

function curll_post($url , $postdata){
    //要求post application/json;数据,将数组转换成json
    $data = json_encode(($postdata);
    //要求post application/x-www-form-urlencoded数据,将数组转换成字符串
    $data = http_build_query($postdata);
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url); //请求地址
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
    //请求头定义为json数据--不用json可删除此部分
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type:application/json;charset=utf-8',
    'Content-Length: '.strlen($data)
    )
    ); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}

以下是支付接口请求网关参考代码,流程道理一样

//接收发起支付页面post数据
$u_name = $_POST['u_name'];
$amount = number_format($_POST['amount'],'2');
$u_order = 'pay'.$get_sigle['u_answer'].date('Ymd',time()).time();
//组装接口需要的参数
$mer_no     = "gm761100000033104";
$key         = "EE7DBB17BF1B237973FAAD1EAF6A5AA6";
$mer_order_no     = 'pay'.$get_sigle['u_answer'].date('Ymd',time()).time();
$pname         = $get_sigle['u_answer'];
$pemail        = $u_name.'@gmail.com';
$phone         = $u_name;
$order_amount     = $amount;
$countryCode     = "IND";
$ccy_no     = "INR";
$bankCode    = "TPB";
$timeout_express= "90m";
$busi_code     = "100303";
$goods         = "goods";
$notifyUrl     = 'http://'.$_SERVER['HTTP_HOST'].'/pay/notify.php';
$pageUrl     = 'http://'.$_SERVER['HTTP_HOST'].'/user.php';
//需要严格按接口方签名要求
$sign_arr     = 'bankCode='.$bankCode.'&busi_code='.$busi_code.'&ccy_no='.$ccy_no.'&countryCode='.$countryCode.'&goods='.$goods.'&mer_no='.$mer_no.'&mer_order_no='.$mer_order_no.'&notifyUrl='.$notifyUrl.'&order_amount='.$order_amount.'&pageUrl='.$pageUrl.'&pemail='.$pemail.'&phone='.$phone.'&pname='.$pname.'&timeout_express='.$timeout_express.'&key='.$key;
$sign    = md5($sign1);
//为了方便简易看懂,组装一下post支付网关的数组
$postdata = array(
    'bankCode'=>$bankCode,
    'mer_no'=>$mer_no,
    'pname'=>$pname,
    'sign'=>$sign,
    'goods'=>$goods,
    'pemail'=>$pemail,
    'phone'=>$phone,
    'countryCode'=>$countryCode,
    "order_amount"=>$order_amount,
    'timeout_express'=>$timeout_express,
    'notifyUrl'=>$notifyUrl,
    'pageUrl'=>$pageUrl,
    'ccy_no'=>$ccy_no,
    'busi_code'=>$busi_code,
    'mer_order_no'=>$mer_order_no
);

$url = "http://www.xxxxxxx.com/ty/orderPay";
$res = $this->curll_post($url,$postdata); //此处用到了上面的curl函数
$res_array = json_decode($res,true);
//判断网关返回的状态,成功则跳转支付连接,否则输出错误代码
if($res_array['status'] == "SUCCESS"){
    header('Location:'.$res_array['order_data']);
}else{
    echo $res_array['err_msg'].' 错误代码:'.$res_array['err_code'];
}

也可以使用下面几个脚本

//格式化参数格式化成url参数
function to_params($signdata){
    $buff = "";
    foreach ($signdata as $k => $v) {
        if($k != "sign" && $v != "" && !is_array($v)){
            $buff .= $k . "=" . $v . "&";
        }
    }
    $buff = trim($buff, "&");
    return $buff;
}
//签名
public function make_sign($signdata,$api_key) {
    //签名步骤一:按字典序排序参数
    ksort($signdata);
    $string = $this->to_params($signdata);
    //签名步骤二:在string后加入KEY
    $string = $string . "&key=".$api_key;
    //签名步骤三:MD5加密
    $string = md5($string);
    //签名步骤四:所有字符转为大写
    //$result = strtoupper($string);
    return $string;
}
用法示例
$dataArray['merId'] = $pay['appid'];
$dataArray['orderId'] = 'gms'.time().'U'.$uid;
$dataArray['applyDate'] = date("Y-m-d H:i:s");
$dataArray['channelCode'] = 'YdzfUPI';
$dataArray['notifyUrl'] = $notifyUrl;
$dataArray['amount'] = $price*1000;
$dataArray['ip'] = $usinfo['ip'];
$dataArray['currency'] = 'INR';
                
//签名
$sign = $this->make_sign($dataArray,$pay['paykey']);
$dataArray['sign'] = strtoupper($sign);
$dataArray['callbackUrl'] = $returnUrl;
$dataArray['attach'] = $uid;
            
$url = 'https://www.xxxxxx.com/PayGo';
$res = $this->curll_post($url,$dataArray); 
$res_array = json_decode($res,true);
//判断网关返回的状态,成功则跳转支付连接,否则输出错误代码
//die (json_encode($res_array));
if($res_array['code'] == 200){
    //业务逻辑
}else{
    echo $res_array['msg'];
}
0 条评论

赞助
会员