php版银联支付接口开发简单实例详解

墨辰丷
发布: 2023-03-28 20:18:02
原创
2604 人浏览过

这篇文章主要介绍了php版银联支付接口开发的方法,结合实例形式分析了php银联支付接口开发的具体流程与相关操作技巧,需要的朋友可以参考下

支付接口现在有第三方的支付接口也有银行的支付接口。这里就来介绍php版本银联支付接口开发的方法。

银联支付,首先要注意二重要的部分:

PHP运行环境是5.4.18以上

开了扩展openssl

开发手册上面的列子只做参考,因为基本都是错的。你可以试着去官网下一个demo。。。注意现在银联开发,没有测试密钥提供,只能在正式环境开发

下面是我用ThinkPHP编写的一个支付类

/**
* 银联支付 v0.1
* @auther:Summer<dengwz7788@gmail.com>;
* @date:20151202
* **/
class NetPayAction extends BaseAction{
//在类初始化方法中,引入相关类库
public function _initialize() {
 header("Content-type:text/html;charset=utf-8");
 vendor(&#39;Netpay.util.common&#39;,"",".php"); //导入加密核心文件夹
 vendor(&#39;Netpay.util.SecssUtil&#39;,"",".class.php"); //导入加密核心文件夹
 vendor(&#39;Netpay.util.Settings_INI&#39;,"",".php"); //导入加密核心文件夹
 vendor(&#39;Netpay.util.Settings&#39;,"",".php"); //导入加密核心文件夹
 $this->securityPropFile= $_SERVER[&#39;DOCUMENT_ROOT&#39;] . "/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 (
   &#39;MerId&#39; => &#39;商户号&#39;,
   &#39;MerOrderNo&#39; => &#39;0000001944663232&#39;,
   &#39;OrderAmt&#39; => &#39;1&#39;,
   &#39;TranDate&#39; => &#39;20151219&#39;,
  &#39;TranTime&#39; =>&#39;171248&#39;,
  &#39;TranType&#39; => &#39;0001&#39;,
  &#39;BusiType&#39; =>&#39;0001&#39;,
  &#39;Version&#39; => &#39;20140728&#39;,
  &#39;CurryNo&#39; => &#39;CNY&#39;,
  &#39;AccessType&#39; =>; &#39;0&#39;,
  &#39;CommodityMsg&#39; => &#39;测试商品1号&#39;,
  &#39;MerPageUrl&#39; => $this->MerBgUrl,
  &#39;MerBgUrl&#39; =>$this->MerPageUrl,
  &#39;MerResv&#39; => &#39;MerResv&#39;,
 );
 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 = "<form name=&#39;payment&#39; action=&#39;{$pay_url}&#39; method=&#39;POST&#39; target=&#39;_blank&#39;>;";
  $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 .= "<input type=&#39;hidden&#39; name = &#39;" . $k . "&#39; value =&#39;" . $v . "&#39;/>";
   }
  }
    $html .= "<nput type=&#39;button&#39; type=&#39;hidden&#39; value=&#39;提交订单&#39; >";
    $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实现微信支付之退款功能

php支付宝系列之电脑网站支付

php性能分析之php-fpm慢执行日志slow log的用法

以上是php版银联支付接口开发简单实例详解的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!