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

yipeiwu_com6年前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程序设计有所帮助。

相关文章

PHP静态方法和静态属性及常量属性的区别与介绍

PHP中若使用static关键字来修饰属性、方法,称这些属性、方法为静态属性、静态方法。static关键字声明一个属性或方法是和类相关的,而不是和类的某个特定的实例相关,因此,这类属性或...

解析smarty模板中类似for的功能实现

1. 功能说明,在页面使用smarty循环100次输出,类似for循环100次{section name=total loop=100}{$smarty.section.total.in...

谈谈从phpinfo中能获取哪些值得注意的信息

谈谈从phpinfo中能获取哪些值得注意的信息

phpinfo函数 phpinfo函数 PHP中提供了PHPInfo()函数,该函数返回 PHP 的所有信息,包括了 PHP 的编译选项及扩充配置、PHP 版本、服务器信息及环境变量、P...

PHP new static 和 new self详解

PHP new static 和 new self详解

最近在一个视频的评论被问到一个小问题:这里选择用static 而不是self有特殊的考虑么?或者我们可以这样转换一下问题: PHP 的 new static 和 new self 具体有...

PHP使用两个栈实现队列功能的方法

本文实例讲述了PHP使用两个栈实现队列功能的方法。分享给大家供大家参考,具体如下: 问题 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。 解决思路...