zhangv/union-pay

The simplest union pay

v0.8.13 2019-06-13 16:00 UTC

README

Latest Stable Version License Build Status codecov

simplest union pay - no dependency to any library, simple enough to let you hack.

Feature

Install

composer require zhangv/union-pay

or

add:

"zhangv/union-pay":"0.8.12"

in composer.json

Step 1: config.php - 配置

return ['test', [
		'version' => '5.1.0',
		'signMethod'=> '01', //RSA
		'encoding' => 'UTF-8',
		'merId' => '700000000000001',
		'returnUrl' => 'http://dev.git.com/union-pay/demo/payreturn.php', //前台网关支付返回
		'notifyUrl' => 'http://dev.git.com/union-pay/demo/paynotify.php', //后台通知
		'frontFailUrl'   => 'http://dev.git.com/union-pay/demo/payfail.php',
		'refundnotifyUrl' => 'http://dev.git.com.com/union-pay/demo/refundnotify.php',
		'signCertPath' => dirname(__FILE__).'/../cert/acp_test_sign.pfx',
		'signCertPwd' => '000000', //签名证书密码
		'verifyCertPath' => dirname(__FILE__).'/../cert/acp_test_verify_sign.cer',  //v5.0.0 required
		'verifyRootCertPath' => dirname(__FILE__).'/../cert/acp_test_root.cer', //v5.1.0 required
		'verifyMiddleCertPath' => dirname(__FILE__).'/../cert/acp_test_middle.cer', //v5.1.0 required
		'encryptCertPath' => dirname(__FILE__).'/../cert/acp_test_enc.cer',
		'ifValidateCNName' => false, //正式环境设置为true
	]
];

Step 2: pay.php - 支付

<?php

list($mode,$config) = include './config.php';
$unionPay = UnionPay::B2C($config,$mode);

$payOrderNo = date('YmdHis');
$amt = 1;

$html = $unionPay->pay($payOrderNo,$amt);
echo $html;

Step 3: payreturn.php - 支付完成前台返回

<?php

list($mode,$config) = include './config.php';
$unionPay = UnionPay::B2C($config,$mode);

$postdata = $_REQUEST;
$unionPay->onPayNotify($postdata,function($notifydata){
	echo 'SUCCESS';
	var_dump($notifydata);
});

Step 4: paynotify.php - 支付完成后台通知

<?php
list($mode,$config) = include './config.php';
$unionPay = UnionPay::B2C($config,$mode);

$notifyData = $_POST;
$respCode = $notifyData['respCode'];
if($respCode == '00'){
	$unionPay->onNotify($notifyData,'demoCallback');
}elseif(in_array($respCode,['03','04','05'])){
	//后续需发起交易状态查询交易确定交易状态
}else{
	echo 'fail';
}


function demoCallback($notifyData){//自定义回调
	var_dump($notifyData);
	print('ok');
}

FAQ

  • 关于测试环境:有时候会报验签错误,一般的原因是证书错误或者参数拼接,如果确认二者没错,那么可能是因为你的商户号在银联测试环境中不生效,这种时候需要联系银联的客服。也可以直接用生产环境的证书测试交易,如果可以正常交易,那么测试交易这步也可以不做。
  • 关于测试用例:目前的测试用例依赖银联的测试环境,但并不是很稳定,经常会有返回错误的情况。后续可能考虑改成模拟数据。