现如今支付系统几乎成了许多网站必不可少的东西,那么常见的几种支付方式:支付宝微信;关于 支付宝微信 的实现方式在 世界上最好用的 Alipay 和 WeChat 的支付 SDK 这篇文章有介绍,这里就不多解释了。 今天就着重介绍下银联支付 的具体实现方式。

关于银联支付我发现一个很好用的 SDK omnipay-unionpay . 下面步入正题:

安装 omnipay-unionpay

composer.json 文件中添加如下代码

{
    "require": {
        "lokielse/omnipay-unionpay": "^0.4"
    }
}

使用 composer update 进行更新依赖

composer update

或者直接采用如下命令安装

composer require lokielse/omnipay-unionpay dev-master

获取银联支付配置信息 到 银联支付开发者中心 获取配置信息,我们测试的话使用测试接口就好

file

点击 私钥和公钥进行下载

获取 PrivateKey, PublicKey, Cert ID

1.获取 PrivateKey

openssl pkcs12 -in 700000000000001_acp.pfx  -nocerts -nodes | openssl rsa -out private_key.pem
file 2.Public key 就是 verify_sign_acp.cer

3.获取 Cert ID

openssl pkcs12 -in 700000000000001_acp.pfx -clcerts -nokeys | openssl x509 -serial -noout

1_1542987039_6MHoKfeRk4.pngfile

4.将第三步获取到的值使用如下网址进行进制转换

https://lokielse.github.io/hex2dec

1_1542987233_uQM557RM99.pngfile

支付

use Omnipay\Omnipay;

public function actionIndex()
{
    $gateway    = Omnipay::create('UnionPay_Express');
    $gateway->setMerId(Yii::$app->params['merId']);
    $gateway->setCertId(Yii::$app->params['certId']);
    $gateway->setPrivateKey(Yii::getAlias('@backend').'/web/pem/private_key.pem'); // path or content
    $gateway->setReturnUrl('https://laravelcode.cn/return');
    $gateway->setNotifyUrl('https://laravelcode.cn/notify');

    $order = [
        'orderId'   => date('YmdHis'), //Your order ID
        'txnTime'   => date('YmdHis'), //Should be format 'YmdHis'
        'orderDesc' => 'My order title', //Order Title
        'txnAmt'    => '100', //Order Total Fee
    ];
    //For PC/Wap
    $response = $gateway->purchase($order)->send();
   return $response->getRedirectHtml();

   //For APP
    $response = $gateway->createOrder($order)->send();
   return $response->getTradeNo();
}

注意上方设置私钥我这里采用YII框架进行配置,如不是 YII 框架,请使用根目录全路径进行配置,如:/etc/

只要你的配置正确那么以上就可以调起银联支付了。

回调

public function actionNotify()
{
    $gateway    = Omnipay::create('UnionPay_Express');
    $gateway->setMerId(Yii::$app->params['merId']);
    $gateway->setPublicKey(Yii::getAlias('@backend').'/web/pem/publicKey'); // path or content

    $response = $gateway->completePurchase(['request_params'=>$_REQUEST])->send();

    if ($response->isPaid()) {
        //pay success
    }else{
        //pay fail
    }
}

更多使用方法请看 官方文档

关于极客返利

极客返利 是由我个人开发的一款网课返利、返现平台。包含 极客时间返现、拉勾教育返现、掘金小册返现、GitChat返现。目前仅包含这几个平台。后续如果有需要可以考虑其他平台。 简而言之就是:你买课,我返现。让你花更少的钱,就可以买到课程。

https://geekfl.com

https://geek.laravelcode.cn

版权许可

本作品采用 知识共享署名 4.0 国际许可协议 进行许可。

转载无需与我联系,但须注明出处,注明文章来源 PHP 集成银联支付

联系我

编程怪事
暂无回复
0 / 180