欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php版银联支付接口开发简明教程

程序员文章站 2024-04-02 12:28:52
本文实例讲述了php版银联支付接口开发的方法。分享给大家供大家参考,具体如下: 支付接口现在有第三方的支付接口也有银行的支付接口。这里就来介绍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('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' => '20151219',
  '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 = "<form name='payment' action='{$pay_url}' method='post' target='_blank'>;";
  $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='hidden' name = '" . $k . "' value ='" . $v . "'/>";
   }
  }
    $html .= "<nput type='button' type='hidden' value='提交订单' >";
    $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数组(array)操作技巧大全》、《php排序算法总结》、《php常用遍历算法与技巧总结》、《php数据结构与算法教程》、《php程序设计算法总结》、《php数学运算技巧总结》、《php正则表达式用法总结》、《php运算与运算符用法总结》及《php字符串(string)用法总结

希望本文所述对大家php程序设计有所帮助。