php銀聯網頁支付實現方法

來源:文萃谷 1.56W

php銀聯網頁支付實現方法,實例分析了php操作銀聯網支付接口的技巧,具有一定參考借鑑價值,需要的朋友可以參考下.

php銀聯網頁支付實現方法

這裏介紹的`銀聯WAP支付功能,僅限消費功能。

  1. PHP代碼如下:

複製代碼 代碼如下:

<?php

namespace commonservices;

class UnionPay

{

/**

* 支付配置

* @var array

*/

public $config = [];

/**

* 支付參數,提交到銀聯對應接口的所有參數

* @var array

*/

public $params = [];

/**

* 自動提交表單模板

* @var string

*/

private $formTemplate = <<<'HTML'

<!DOCTYPE HTML>

<html>

<head>

<meta charset="utf-8">

<title>支付</title>

</head>

<body>

<p style="text-align:center">跳轉中...</p>

<form id="pay_form" name="pay_form" action="%s" method="post">

%s

</form>

<script type="text/javascript">

adystatechange = function(){

if(yState == "complete") {

_it();

}

};

</script>

</body>

</html>

HTML;

/**

* 構建自動提交HTML表單

* @return string

*/

public function createPostForm()

{

$this->params['signature'] = $this->sign();

$input = '';

foreach($this->params as $key => $item) {

$input .= "tt<input type="hidden" name="{$key}" value="{$item}">n";

}

return sprintf($this->formTemplate, $this->config['frontUrl'], $input);

}

/**

* 驗證簽名

* 驗籤規則:

* 除signature域之外的所有項目都必須參加驗籤

* 根據key值按照字典排序,然後用&拼接key=value形式待驗簽字符串;

* 然後對待驗簽字符串使用sha1算法做摘要

* 用銀聯公鑰對摘要和簽名信息做驗籤操作

*

* @throws Exception

* @return bool

*/

public function verifySign()

{

$publicKey = $this->getVerifyPublicKey();

$verifyArr = $this->filterBeforSign();

ksort($verifyArr);

$verifyStr = $this->arrayToString($verifyArr);

$verifySha1 = sha1($verifyStr);

$signature = base64_decode($this->params['signature']);

$result = openssl_verify($verifySha1, $signature, $publicKey);

if($result === -1) {

throw new Exception('Verify Error:'ssl_error_string());

}

return $result === 1 ? true : false;

}

熱門標籤