首页 >后端开发 >php教程 >PHP后端银联支付及退款实例

PHP后端银联支付及退款实例

陈政宽~
陈政宽~原创
2018-05-10 16:45:594098浏览

本篇文章主要介绍了PHP后端银联支付及退款实例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

声明:此文以当前银联官方最新SDK(2016-08-09 5.1.0版)进行说明,若出现包不相同的情况请检查是否是此版本

近期遇到银联支付以及相关退款(此文仅以手机控件支付作为前提)操作,下面会依次写出期间遇到的问题以及基本流程,在此之前通过官方的一张图片了解一个支付中,对于后端人员的我们需要做到的一些事情

由此图可以看出,后端在此负责1、平台订单生成;2、银联全渠道平台订单推送;3、返回tn码给前端进行支付;4、处理前台通知以及全渠道平台的异步通知。

此间难点有三,订单推送、异步通知处理、订单状态查询。

通过官方的邮件说明下载相关的包并放入后端php代码中,(支付控件去下载你看到的估计只有IOS,安卓版的SDK,对于后端来说,随便下载一个即可,PHP的代码在里面都有放置);然后仔细阅读SDK中的readme.txt文件,此后进行以下步骤:

一、相关参数配置

对接过程中使用在sdk的assets文件夹中测试环境配置文件及证书,放置到sdk文件夹中,并配置/sdk/SDKconfig.php文件已正确的读取acp_sdk.ini配置文件。

在acp_sdk.ini文件中配置好acpsdk.signCert.path、acpsdk.encryptCert.path、acpsdk.rootCert.path、acpsdk.middleCert.path四个文件的绝对地址(自定义文件路径即可)。

因项目开发过程中会出现系统不同或项目地址不同导致的证书绝对地址等错误,尤其在实际生产环境中,极易出现项目部署文件地址不同,不可能在开发后每次更新都要更换证书地址,在此修改了一下SDK中的SDKconfig.php已兼容不同文件地址较长,这里还请点击展开查看

<?php
namespace com\unionpay\acp\sdk;;
include_once &#39;log.class.php&#39;;
include_once &#39;common.php&#39;;
 
class SDKConfig {
   
  private static $_config = null;
  public static function getSDKConfig(){
    if (SDKConfig::$_config == null ) {
      SDKConfig::$_config = new SDKConfig();
    }
    return SDKConfig::$_config;
  }
   
  private $frontTransUrl;
  private $backTransUrl;
  private $singleQueryUrl;
  private $batchTransUrl;
  private $fileTransUrl;
  private $appTransUrl;
  private $cardTransUrl;
  private $jfFrontTransUrl;
  private $jfBackTransUrl;
  private $jfSingleQueryUrl;
  private $jfCardTransUrl;
  private $jfAppTransUrl;
  private $qrcBackTransUrl;
  private $qrcB2cIssBackTransUrl;
  private $qrcB2cMerBackTransUrl;
   
  private $signMethod;
  private $version;
  private $ifValidateCNName;
  private $ifValidateRemoteCert;
   
  private $signCertPath;
  private $signCertPwd;
  private $validateCertDir;
  private $encryptCertPath;
  private $rootCertPath;
  private $middleCertPath;
  private $frontUrl;
  private $backUrl;
  private $secureKey;
  private $logFilePath;
  private $logLevel;
 
  function construct(){
 
    //如果想把acp_sdk.ini挪到其他路径的话,请修改下面这行指定绝对路径。
    $configFilePath = dirname(FILE) . "/acp_sdk.ini";
    $certsFilePath = dirname(dirname(FILE)) . "/certs/";
     
    if(!file_exists($configFilePath)){
      $logger = LogUtil::getLogger();
      $logger->LogError("配置文件加载失败,文件路径:[" . $configFilePath . "].请检查启动php的用户是否有读权限。");
      return;
    }
    $ini_array = parse_ini_file($configFilePath, true);
    $sdk_array = $ini_array["acpsdk"];
    $this->frontTransUrl = array_key_exists("acpsdk.frontTransUrl", $sdk_array)?$sdk_array["acpsdk.frontTransUrl"] : null;
    $this->backTransUrl = array_key_exists("acpsdk.backTransUrl", $sdk_array)?$sdk_array["acpsdk.backTransUrl"] : null;
    $this->singleQueryUrl = array_key_exists("acpsdk.singleQueryUrl", $sdk_array)?$sdk_array["acpsdk.singleQueryUrl"] : null;
    $this->batchTransUrl = array_key_exists("acpsdk.batchTransUrl", $sdk_array)?$sdk_array["acpsdk.batchTransUrl"] : null;
    $this->fileTransUrl = array_key_exists("acpsdk.fileTransUrl", $sdk_array)?$sdk_array["acpsdk.fileTransUrl"] : null;
    $this->appTransUrl = array_key_exists("acpsdk.appTransUrl", $sdk_array)?$sdk_array["acpsdk.appTransUrl"] : null;
    $this->cardTransUrl = array_key_exists("acpsdk.cardTransUrl", $sdk_array)?$sdk_array["acpsdk.cardTransUrl"] : null;
    $this->jfFrontTransUrl = array_key_exists("acpsdk.jfFrontTransUrl", $sdk_array)?$sdk_array["acpsdk.jfFrontTransUrl"] : null;
    $this->jfBackTransUrl = array_key_exists("acpsdk.jfBackTransUrl", $sdk_array)?$sdk_array["acpsdk.jfBackTransUrl"] : null;
    $this->jfSingleQueryUrl = array_key_exists("acpsdk.jfSingleQueryUrl", $sdk_array)?$sdk_array["acpsdk.jfSingleQueryUrl"] : null;
    $this->jfCardTransUrl = array_key_exists("acpsdk.jfCardTransUrl", $sdk_array)?$sdk_array["acpsdk.jfCardTransUrl"] : null;
    $this->jfAppTransUrl = array_key_exists("acpsdk.jfAppTransUrl", $sdk_array)?$sdk_array["acpsdk.jfAppTransUrl"] : null;
    $this->qrcBackTransUrl = array_key_exists("acpsdk.qrcBackTransUrl", $sdk_array)?$sdk_array["acpsdk.qrcBackTransUrl"] : null;
    $this->qrcB2cIssBackTransUrl = array_key_exists("acpsdk.qrcB2cIssBackTransUrl", $sdk_array)?$sdk_array["acpsdk.qrcB2cIssBackTransUrl"] : null;
    $this->qrcB2cMerBackTransUrl = array_key_exists("acpsdk.qrcB2cMerBackTransUrl", $sdk_array)?$sdk_array["acpsdk.qrcB2cMerBackTransUrl"] : null;
 
    $this->signMethod = array_key_exists("acpsdk.signMethod", $sdk_array)?$sdk_array["acpsdk.signMethod"] : null;
    $this->version = array_key_exists("acpsdk.version", $sdk_array)?$sdk_array["acpsdk.version"] : null;
    $this->ifValidateCNName = array_key_exists("acpsdk.ifValidateCNName", $sdk_array)?$sdk_array["acpsdk.ifValidateCNName"] : "true";
    $this->ifValidateRemoteCert = array_key_exists("acpsdk.ifValidateRemoteCert", $sdk_array)?$sdk_array["acpsdk.ifValidateRemoteCert"] : "false";
 
    $this->signCertPath = $certsFilePath . (array_key_exists("acpsdk.signCert.path", $sdk_array)?$sdk_array["acpsdk.signCert.path"]: null);
    $this->signCertPwd = array_key_exists("acpsdk.signCert.pwd", $sdk_array)?$sdk_array["acpsdk.signCert.pwd"]: null;
     
    $this->validateCertDir = array_key_exists("acpsdk.validateCert.dir", $sdk_array)? $sdk_array["acpsdk.validateCert.dir"]: null;
    $this->encryptCertPath = $certsFilePath . (array_key_exists("acpsdk.encryptCert.path", $sdk_array)? $sdk_array["acpsdk.encryptCert.path"]: null);
    $this->rootCertPath = $certsFilePath . (array_key_exists("acpsdk.rootCert.path", $sdk_array)? $sdk_array["acpsdk.rootCert.path"]: null);
    $this->middleCertPath = $certsFilePath . (array_key_exists("acpsdk.middleCert.path", $sdk_array)?$sdk_array["acpsdk.middleCert.path"]: null);
     
    $this->frontUrl = array_key_exists("acpsdk.frontUrl", $sdk_array)?$sdk_array["acpsdk.frontUrl"]: null;
    $this->backUrl = array_key_exists("acpsdk.backUrl", $sdk_array)?$sdk_array["acpsdk.backUrl"]: null;
     
    $this->secureKey = array_key_exists("acpsdk.secureKey", $sdk_array)?$sdk_array["acpsdk.secureKey"]: null;
    $this->logFilePath = array_key_exists("acpsdk.log.file.path", $sdk_array)?$sdk_array["acpsdk.log.file.path"]: null;
    $this->logLevel = array_key_exists("acpsdk.log.level", $sdk_array)?$sdk_array["acpsdk.log.level"]: null;
     
  }
 
  public function get($property_name)
  {
    if(isset($this->$property_name))
    {
      return($this->$property_name);
    }
    else
    {
      return(NULL);
    }
  } 
}

二、全渠道商品订单推送

相关代码请点击查看

use com\unionpay\acp\sdk\AcpService;
use com\unionpay\acp\sdk\LogUtil;
use com\unionpay\acp\sdk\SDKConfig;
 
  /**
   * 银联支付下单
   *
   * @param $orders
   * @param $orders_type
   * @return array
   */
  public function unionPay($orders, $orders_type = 0)
  {
    include_once dirname(dirname(dirname(FILE))) . &#39;/Model/unionpay-sdk/sdk/acp_service.php&#39;;
    $config = new SDKConfig();
    $AcpService = new AcpService();
    $log = LogUtil::getLogger();
    $time = date(&#39;YmdHis&#39;, time());
    $params = array(
 
      //以下信息非特殊情况不需要改动
      &#39;version&#39; => $config->getSDKConfig()->version,         //版本号
      &#39;encoding&#39; => &#39;utf-8&#39;,         //编码方式
      &#39;txnType&#39; => &#39;01&#39;,           //交易类型
      &#39;txnSubType&#39; => &#39;01&#39;,         //交易子类
      &#39;bizType&#39; => &#39;000201&#39;,         //业务类型
      &#39;frontUrl&#39; => $config->getSDKConfig()->frontUrl, //前台通知地址
      &#39;backUrl&#39; => $this->getURL(&#39;api_pay_unionpay_call_back&#39;),  //后台通知地址
      &#39;signMethod&#39; => $config->getSDKConfig()->signMethod,         //签名方法
      &#39;channelType&#39; => &#39;08&#39;,         //渠道类型,07-PC,08-手机
      &#39;accessType&#39; => &#39;0&#39;,        //接入类型
      &#39;currencyCode&#39; => &#39;156&#39;,      //交易币种,境内商户固定156
 
      //TODO 以下信息需要填写
      &#39;merId&#39; => $this->getParameter(&#39;mer_id&#39;),   //商户代码,请改自己的测试商户号
      &#39;orderId&#39; => $orders["order_no"],  //商户订单号,8-32位数字字母,不能含“-”或“_”
      &#39;txnTime&#39; => $time, //订单发送时间,格式为YYYYMMDDhhmmss,取北京时间
      &#39;txnAmt&#39; => $orders[&#39;total_price&#39;] * 100,  //交易金额,单位分
    );
 
    $AcpService->sign ( $params ); // 签名
    $url = $config->getSDKConfig()->appTransUrl;
 
    $result_arr = $AcpService->post ($params, $url);
 
    if(count($result_arr)<=0) { //没收到200应答的情况 $log->LogInfo(&#39;没收到200应答的情况&#39;);
    }
 
//    $this->printResult ($url, $params, $result_arr ); //页面打印请求应答数据
 
    if (!$AcpService->validate ($result_arr) ){
      $log->LogInfo(&#39;应答报文验签失败&#39;);
    }
    if ($result_arr["respCode"] == "00"){
      //成功
      return array(&#39;txn_time&#39;=>$time, &#39;tn&#39;=>$result_arr["tn"]);
//      echo "后续请将此tn传给手机开发,由他们用此tn调起控件后完成支付。
\n";
//      echo "手机端demo默认从仿真获取tn,仿真只返回一个tn,如不想修改手机和后台间的通讯方式,【此页面请修改代码为只输出tn】。
\n";
    } else {
      //其他应答码做以失败处理
      return array(&#39;txn_time&#39;=>$time, &#39;tn&#39;=>0);
      //echo "失败:" . $result_arr["respMsg"] . "。
\n";
 
    }
  }

在此注意txnTime格式不要传错,测试环境下应该不会出现什么问题,将得到的tn返回APP进行支付即可

三、异步通知处理以及订单交易状态查询

这一步主要作用为处理银联交易成功信息,并尽可能避免出现回调未处理导致问题。

先说异步通知处理,此步骤为订单状态修改的主要依据。无实际难点,保证相关参数无问题即可

/**
   * 银联回调
   *
   * @param Request $request
   * @return array|Response
   */
  public function unionPayCallBackAction(Request $request)
  {
    if ($request->get(&#39;type&#39;) == 1){//前台通知-进行订单状态查询
      $query = $this->unionPayQuery($request, array(), 1);
 
      return new JsonResponse($query);
    }
 
    require_once dirname(dirname(dirname(FILE))) . "/Model/unionpay-sdk/sdk/acp_service.php";
    $log = LogUtil::getLogger();
    $AcpService = new AcpService();
 
 
    if ($request->request->has(&#39;signature&#39;) && $AcpService->validate($_POST)) {
      $order_no = $request->request->get(&#39;orderId&#39;);
      $respCode = $request->request->get(&#39;respCode&#39;);
      $total = $request->request->get(&#39;txnAmt&#39;); // 交易金额
      if ($respCode === &#39;00&#39; || $respCode === &#39;A6&#39;) {
        $trade_no = $request->request->get(&#39;origQryId&#39;)?:&#39;UN&#39; . date(&#39;YmdHis&#39;, time()) . substr(implode(NULL, array_map(&#39;ord&#39;, str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
        $this->dispose($order_no, $trade_no, 4);//订单交易处理-请根据实际情况自行编写
      }
    } else {
      if (!$request->request->has(&#39;signature&#39;)) {
        $log->LogInfo(&#39;签名为空&#39;);
      } else {
        $log->LogInfo(&#39;验签失败&#39;);
      }
    }
 
    exit;
  }

订单交易状态查询

   do{//循环查询,直到获取到退款订单的queryID
      sleep($number * 2);
      $query = $this->unionPayQuery(&#39;&#39;, $orders);
      $number += 1;
    }while($query[&#39;errorCode&#39;] != 0 || empty($query[&#39;result_arr&#39;]["queryId"]));
 
public function unionPayQuery($request, $orders)
  {
    require_once dirname(dirname(dirname(FILE))) . "/Model/unionpay-sdk/sdk/acp_service.php";
    $config = new SDKConfig();
    $AcpService = new AcpService();
    $log = LogUtil::getLogger();
    $params = array(
      //以下信息非特殊情况不需要改动
      &#39;version&#39; => $config->getSDKConfig()->version,    //版本号
      &#39;encoding&#39; => &#39;utf-8&#39;,     //编码方式
      &#39;signMethod&#39; => $config->getSDKConfig()->signMethod,     //签名方法
      &#39;txnType&#39; => &#39;00&#39;,       //交易类型
      &#39;txnSubType&#39; => &#39;00&#39;,     //交易子类
      &#39;bizType&#39; => &#39;000000&#39;,     //业务类型
      &#39;accessType&#39; => &#39;0&#39;,    //接入类型
      &#39;channelType&#39; => &#39;07&#39;,     //渠道类型
 
      //TODO 以下信息需要填写
      &#39;orderId&#39; => $orders[&#39;order_no&#39;],  //请修改被查询的交易的订单号,8-32位数字字母,不能含“-”或“_”
      &#39;merId&#39; => $this->getParameter(&#39;mer_id&#39;),   //商户代码,请改自己的测试商户号
      &#39;txnTime&#39; => date(&#39;YmdHis&#39;, time()), //请修改被查询的交易的订单发送时间,格式为YYYYMMDDhhmmss
    );
 
    $AcpService->sign ( $params ); // 签名
    $url = $config->getSDKConfig()->singleQueryUrl;
 
    $result_arr = $AcpService->post ( $params, $url);
    if(count($result_arr)<=0) { //没收到200应答的情况 $log->LogInfo(&#39;没收到200应答的情况&#39;);
    }
 
    if (!$AcpService->validate ($result_arr) ){
      $log->LogInfo(&#39;应答报文验签失败&#39;);
    }
    if ($result_arr["respCode"] == "00"){
      if ($result_arr["origRespCode"] == "00"){
        //交易成功
        $trade_no = &#39;UN&#39; . date(&#39;YmdHis&#39;, time()) . substr(implode(NULL, array_map(&#39;ord&#39;, str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
        $this->dispose($orders[&#39;order_no&#39;], $trade_no, 4);
        $result = array(&#39;errorCode&#39;=>0, &#39;message&#39;=>&#39;交易成功&#39;, &#39;result_arr&#39;=>$result_arr);
 
      } else if ($result_arr["origRespCode"] == "03"
        || $result_arr["origRespCode"] == "04"
        || $result_arr["origRespCode"] == "05"){
        //后续需发起交易状态查询交易确定交易状态
 
        $result = array(&#39;errorCode&#39;=>2, &#39;message&#39;=>&#39;交易处理中&#39;, &#39;result_arr&#39;=>$result_arr);
 
      } else {
        //其他应答码做以失败处理
 
        echo "交易失败:" . $result_arr["origRespMsg"] . "。
\n";
 
        $result = array(&#39;errorCode&#39;=>1, &#39;message&#39;=>"交易失败:" . $result_arr["origRespMsg"] . ".", &#39;result_arr&#39;=>$result_arr);
      }
    } else if ($result_arr["respCode"] == "03"
      || $result_arr["respCode"] == "04"
      || $result_arr["respCode"] == "05" ){
      //后续需发起交易状态查询交易确定交易状态
 
      $result = array(&#39;errorCode&#39;=>2, &#39;message&#39;=>"处理超时,请稍后查询.", &#39;result_arr&#39;=>$result_arr);
    } else {
      //其他应答码做以失败处理
 
 
      $result = array(&#39;errorCode&#39;=>1, &#39;message&#39;=>"失败:" . $result_arr["respMsg"] . ".", &#39;result_arr&#39;=>$result_arr);
    }
 
    return $result;
  }

到此为止,若是项目没有订单线上退款就完成了。

订单退款相关

public function refundUnionPay($orders)
  {
    require_once(dirname(dirname(FILE)) . "/Model/unionpay-sdk/sdk/acp_service.php");
 
    set_time_limit(100);
 
    $config = new SDKConfig();
    $AcpService = new AcpService();
    $log = LogUtil::getLogger();
    $number = 0;
    do{//循环查询,直到获取到退款订单的queryID
      sleep($number * 2);
      $query = $this->unionPayQuery(&#39;&#39;, $orders);
      $number += 1;
    }while($query[&#39;errorCode&#39;] != 0 || empty($query[&#39;result_arr&#39;]["queryId"]));
   
 
    if ($query[&#39;errorCode&#39;] != 0) {
      return array(&#39;errorCode&#39;=>1, &#39;message&#39;=>&#39;订单未成交,无法退款&#39;);
    }
    $params = array(
 
      //以下信息非特殊情况不需要改动
      &#39;version&#39; => $config->getSDKConfig()->version,      //版本号
      &#39;encoding&#39; => &#39;utf-8&#39;,       //编码方式
      &#39;signMethod&#39; => $config->getSDKConfig()->signMethod,       //签名方法
      &#39;txnType&#39; => &#39;04&#39;,         //交易类型
      &#39;txnSubType&#39; => &#39;00&#39;,       //交易子类
      &#39;bizType&#39; => &#39;000201&#39;,       //业务类型
      &#39;accessType&#39; => &#39;0&#39;,      //接入类型
      &#39;channelType&#39; => &#39;07&#39;,       //渠道类型
      &#39;backUrl&#39; => $config->getSDKConfig()->backUrl, //后台通知地址
 
      //TODO 以下信息需要填写
      &#39;orderId&#39; => "T" . $orders[&#39;order_no&#39;],   //商户订单号,8-32位数字字母,不能含“-”或“_”,可以自行定制规则,重新产生-此处为在退款订单前拼接 T
      &#39;merId&#39; => $this->getParameter(&#39;mer_id&#39;),     //商户代码,请改成自己的商户号
      &#39;origQryId&#39; => $query[&#39;result_arr&#39;]["queryId"], //原消费的queryId,可以从查询接口或者通知接口中获取
      &#39;txnTime&#39; => date(&#39;YmdHis&#39;, time()),    //订单发送时间,格式为YYYYMMDDhhmmss,重新产生,不同于原消费
      &#39;txnAmt&#39; => $orders[&#39;total_price&#39;] * 100,   //交易金额,退货总金额需要小于等于原消费
    );
 
    $AcpService->sign ( $params ); // 签名
    $url = $config->getSDKConfig()->backTransUrl;
 
    $result_arr = $AcpService->post ( $params, $url);
    if(count($result_arr)<=0) { //没收到200应答的情况 return array(&#39;errorCode&#39;=>1, &#39;message&#39;=>"没收到应答.");
    }
 
    if (!$AcpService->validate ($result_arr) ){
      return array(&#39;errorCode&#39;=>1, &#39;message&#39;=>"应答报文验签失败.");
    }
 
    if ($result_arr["respCode"] == "00"){
      //交易已受理,等待接收后台通知更新订单状态,如果通知长时间未收到也可发起交易状态查询
      return array(&#39;errorCode&#39;=>0, &#39;message&#39;=>"受理成功.");
 
    } else if ($result_arr["respCode"] == "03"
      || $result_arr["respCode"] == "04"
      || $result_arr["respCode"] == "05" ){
      //后续需发起交易状态查询交易确定交易状态
      return array(&#39;errorCode&#39;=>1, &#39;message&#39;=>"处理超时,请稍微查询.");
    } else {
      //其他应答码做以失败处理
 
      return array(&#39;errorCode&#39;=>1, &#39;message&#39;=>"失败:" . $result_arr["respMsg"] . ".");
    }
  }

依据返回状态值进行相关操作即可,实际逻辑代码请自行实现

切换生产环境

项目关系暂无法进行-后续补充

未完待续。。。。

以上是PHP后端银联支付及退款实例的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn