PHP银联支付接口开发概述
本内容详细解析了使用PHP语言对接银联支付接口的具体实施步骤,旨在为相关开发者提供实用的参考方案。
当前市场存在第三方支付平台与银行直连两种支付接入模式。此处重点阐述采用PHP技术栈实现银联直连支付的开发流程。
在进行银联支付功能开发前,需重点关注两个关键前提条件:
服务器运行的PHP版本必须高于或等于5.4.18。
必须确保服务器已启用openssl扩展模块。
官方开发手册中的示例代码仅供参考,因其中存在较多错误,建议直接下载官网提供的最新Demo包。需注意,目前银联不提供测试环境密钥,所有开发工作必须在生产环境中进行。
以下展示了一个基于ThinkPHP框架封装的支付处理类代码结构。
/**
* 银联支付 v0.1
* @auther:Summer;
* @date:20261202
* **/
class NetPayAction extends BaseAction{
//在类初始化方法中,引入相关类库
public function _initialize() {
header("Content-type:text/html;charset=utf-8");
vendor('Netpay.util.common',"",".php"); //导入加密核心文件夹
vendor('Netpay.util.SecssUtil',"",".class.php"); //导入加密核心文件夹
vendor('Netpay.util.Settings_INI',"",".php"); //导入加密核心文件夹
vendor('Netpay.util.Settings',"",".php"); //导入加密核心文件夹
$this->securityPropFile= $_SERVER['DOCUMENT_ROOT'] . "/ThinkPHP/Extend/Vendor/Netpay/config/security.properties"; //谁知道这是啥,反正他们要我加的
$this->b2cPaySend = __APP__."/Index/NetPay/b2cPaySend";
$this->b2cRefundSend = __APP__."/Index/NetPay/b2cRefundSend";
$this->b2cQuerySend = __APP__."/Index/NetPay/b2cQuerySend";
$this->;MerBgUrl = __APP__."/Index/NetPay/MerBgUrl";
$this->MerPageUrl = __APP__."/Index/NetPay/MerPageUrl";
}
public function index()
{
$paramArray=array (
'MerId' => '商户号',
'MerOrderNo' => '0000001944663232',
'OrderAmt' => '1',
'TranDate' => '20261219',
'TranTime' =>'171248',
'TranType' => '0001',
'BusiType' =>'0001',
'Version' => '20140728',
'CurryNo' => 'CNY',
'AccessType' =>; '0',
'CommodityMsg' => '测试商品1号',
'MerPageUrl' => $this->MerBgUrl,
'MerBgUrl' =>$this->MerPageUrl,
'MerResv' => 'MerResv',
);
if (count($paramArray) >0) {
$dispatchUrl = $this->b2cPaySend;
$transResvedJson = array();
$cardInfoJson = array();
$sendMap = array();
foreach ($paramArray as $key => $value) {
if (isEmpty($value)) {
continue;
}
if (startWith($key, "trans_")) {
$key = substr($key, strlen("trans_"));
$transResvedJson[$key] = $value;
} else
if (startWith($key, "card_")) {
$key = substr($key, strlen("card_"));
$cardInfoJson[$key] = $value;
} else {
$sendMap[$key] = $value;
}
}
$transResvedStr = null;
$cardResvedStr = null;
if (count($transResvedJson) >0) {
$transResvedStr = json_encode($transResvedJson);
}
if (count($cardInfoJson) > 0) {
$cardResvedStr = json_encode($cardInfoJson);
}
$secssUtil = new SecssUtil();
if (! isEmpty($transResvedStr)) {
$transResvedStr = $secssUtil->decryptData($transResvedStr);
$sendMap["TranReserved"] = $transResvedStr;
}
if (! isEmpty($cardResvedStr)) {
$cardResvedStr = $secssUtil->decryptData($cardResvedStr);
$sendMap["card_"] = $cardResvedStr;
}
$securityPropFile = $this>securityPropFile;
$secssUtil->init($securityPropFile);
$secssUtil->sign($sendMap);
$sendMap["Signature"] = $secssUtil->getSign();
$_SESSION = $sendMap;
header("Location:" . $dispatchUrl);
}
}
public function b2cPaySend(){
layout(false);
$settings = new Settings_INI();
$settings->oad($this->securityPropFile);
$pay_url = "https://payment.chinapay.com/CTITS/service/rest/page/nref/000000000017/0/0/0/0/0";
$html = ";";
$params = "TranReserved;MerId;MerOrderNo;OrderAmt;CurryNo;TranDate;SplitMethod;BusiType;MerPageUrl;MerBgUrl;SplitType;MerSplitMsg;PayTimeOut;MerResv;Version;BankInstNo;CommodityMsg;Signature;AccessType;AcqCode;OrderExpiryTime;TranType;RemoteAddr;Referred;TranTime;TimeStamp;CardTranData";
foreach ($_SESSION as $k =>$v) {
if (strstr($params, $k)) {
$html .= "";
}
}
$html .= "";
$html .= "<;/from>";
$this->html = $html;
$this->display();
}
public function pgReturn(){
if ($_POST) {
if (count($_POST) > 0) {
$secssUtil = new SecssUtil();
$securityPropFile = $this>securityPropFile;
$secssUtil->init($securityPropFile);
$text = array();
foreach($_POST as $key=>$value){
$text[$key] = urldecode($value);
}
if ($secssUtil->verify($text)) {
//支付成功
$_SESSION["VERIFY_KEY"] = "success";
} else {
//支付失败
$_SESSION["VERIFY_KEY"] = "fail";
}
}
}
}
}
整体而言,银联支付接口的集成逻辑相对简便。
期望上述内容能对从事PHP程序设计的读者产生积极帮助。