PHP对接拉卡拉支付

<?php
namespace common\lakala;

class Lakala {

    private $appid;
    private $mchSerialNo;
    private $mercId;
    private $merchantPrivateKeyPath;
    private $lklCertificatePath;
    private $subject;
    private $schema = 'LKLAPI-SHA256withRSA';
    private $version = '3.0';
    private $outOrderNo;
    private $amount;
    private $notifyUrl;
    private $uoUrl = 'https://test.wsmsd.cn/sit/api/v3/ccss/counter/order/create';

    /**
     * 设置APPId
     */
    public function setAppid($appid) {
        $this->appid = $appid;
    }

    /**
     * 设置mchSerialNo
     */
    public function setMchSerialNo($mchSerialNo) {
        $this->mchSerialNo = $mchSerialNo;
    }

    /**
     * 设置mercId
     */
    public function setMercId($mercId) {
        $this->mercId = $mercId;
    }

    /**
     * 设置merchantPrivateKeyPath
     */
    public function setMerchantPrivateKeyPath($merchantPrivateKeyPath) {
        $this->merchantPrivateKeyPath = $merchantPrivateKeyPath;
    }

    /**
     * 设置lklCertificatePath
     */
    public function setLklCertificatePath($lklCertificatePath) {
        $this->lklCertificatePath = $lklCertificatePath;
    }

    /**
     * 设置out_order_no
     */
    public function setOutOrderNo($outOrderNo) {
        $this->outOrderNo = $outOrderNo;
    }

    /**
     * 设置subject
     */
    public function setSubject($subject) {
        $this->subject = $subject;
    }

    /**
     * 设置amount
     */
    public function setAmount($amount) {
        $this->amount = $amount;
    }

    /**
     * 设置notifyUrl
     */
    public function setNotifyUrl($notifyUrl) {
        $this->notifyUrl = $notifyUrl;
    }


	public function pay() {
        $params = [
            'out_order_no' => $this->outOrderNo,
            'merchant_no' => $this->mercId,
            'total_amount' => $this->amount,
            'order_efficient_time' => date('YmdHis'),
            'notify_url' => $this->notifyUrl,
            'support_refund' => 1,
            'support_repeat_pay' => 1,
            'order_info' => $this->subject,
            'extend_info' => $this->subject,
            'counter_param' => json_encode(['pay_mode' => 'WECHAT'])
        ];
        
        $requestData = [
            'req_data' => $params,
            'version' => $this->version,
            'req_time' => date('YmdHis'),
        ];
        
        $body = json_encode($requestData, JSON_UNESCAPED_UNICODE);
        $authorization = $this->getAuthorization($body);
        return $this->post($this->uoUrl, $body, $authorization);   
    }

    //验签
    public function signatureVerification($authorization, $body) {
        $authorization = str_replace($this->schema . " ", "", $authorization);
        $authorization = str_replace(",","&", $authorization);
        $authorization = str_replace("\"","", $authorization);
        $authorization = $this->convertUrlQuery($authorization);
        
        $authorization['signature'] = base64_decode($authorization['signature']);

        $message = $authorization['timestamp'] . "\n" . $authorization['nonce_str'] . "\n" . $body . "\n";

        $key = openssl_get_publickey(file_get_contents($this->lklCertificatePath));
        $flag = openssl_verify($message, $authorization['signature'], $key, OPENSSL_ALGO_SHA256);
        openssl_free_key($key);
        if($flag) {
            return true;
        }
        return false;
    }

	//签名
	public function getAuthorization($body) {
		$nonceStr = $this->getRandom(12);
     	$timestamp = time();

      	$message = $this->appid . "\n" . $this->mchSerialNo . "\n" . $timestamp . "\n" . $nonceStr . "\n" . $body . "\n";

		$key = openssl_get_privatekey(file_get_contents($this->merchantPrivateKeyPath));

        openssl_sign($message, $signature, $key, OPENSSL_ALGO_SHA256);
        openssl_free_key($key);

        return $this->schema . " appid=\"" . $this->appid . "\"," . "serial_no=\"" . $this->mchSerialNo . "\"," . "timestamp=\"" . $timestamp . "\"," . "nonce_str=\"" . $nonceStr . "\"," . "signature=\"" . base64_encode($signature) . "\"";
	}

    //请求
    public function post($url, $data, $authorization) {

        $headers = [
            "Authorization: " . $authorization,
            "Accept: application/json",
            "Content-Type:application/json",
        ];

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);//设置HTTP头
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, 1);
        $res = curl_exec($ch);
        curl_close($ch);
        return json_decode($res, true);
    }

    //签名参数转数组
    private function convertUrlQuery($query) { 
        $queryParts = explode('&', $query); 
         
        $params = array(); 
        foreach ($queryParts as $param) { 
            $item = explode('=', $param); 
            $params[$item[0]] = $item[1]; 
        }
        if($params['signature']) {
            $params['signature'] = substr($query, strrpos($query, 'signature=') + 10);
        }
         
        return $params; 
    }

    /**
     * 获取随机字符串
     */
    public function getRandom($len)
    {
        $base = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        $count = strlen($base);
        $random = '';
        for ($i = 0; $i < $len; $i++) { 
            $random .= $base[rand(0, $count-1)];
        }
        return $random;
    }
}

  • 8
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
卡拉POSP系统-数据库设计说明书.doc, 使用所有表的设计文档 1数据结构 4 1.1 ORACLE TABLE数据模型 4 1.2 ORACLE TABLE结构 4 1.2.1 atmcrporg (上级结点表) 4 1.2.2 atmerrcsw (错误码对照表) 5 1.2.3 atminmerc (商户信息表) 6 1.2.4 atmposterm (POS终端信息表) 8 1.2.5 atmposopr (POS终端操作员表) 10 1.2.6 atmtxnjnl (交易流水表) 11 1.2.7 atmmtyprv (商户类别权限表) 15 1.2.8 atmmerprv (商户权限表) 16 1.2.9 atmposprv (POS终端权限表) 16 1.2.10 atmtxnrtr (交易路由表) 17 1.2.11 atmpfctl (批量文件传输控制表) 18 1.2.12 atmfeerat (商户费率信息表) 19 1.2.13 atmstlinf (商户结算信息表) 20 1.2.14 atmchkcl (银联对账控制表) 21 1.2.15 atmchkrec (银联对账流水表) 22 1.2.16 posinmer (OMS商户同步明细信息表) 24 1.2.17 posterminf (OMS终端同步信息表) 26 1.2.18 postrmparm (POS终端参数模式表) 27 1.2.19 postcupk(POS终端银联密钥信息表) 28 1.2.20 postkey(POS终端本地密钥信息表) 28 1.2.21 poscominfo(pos厂商表) 29 1.2.22 posverinfo(pos版本表) 30 1.2.23 mnggrpinf (管理监控角色信息表) 30 1.2.24 mngusrgrp (管理监控用户角色信息表) 31 1.2.25 mngtxnprv (管理监控角色权限信息表) 32 1.2.26 mngusrinf (管理监控用户信息表) 32 1.2.27 mngaplinf (管理监控应用信息表) 33 1.2.28 usrtxnrul(交易码规则配置表) 34 1.2.29 epsunifit (银联非标卡表) 34 1.2.30 epsunibin (银联标卡卡bin段表) 36 1.2.31 epscupfit (银联贷记卡卡表) 36 1.2.32 pubgrpinf (角色信息表) 37 1.2.33 pubtxnpur (交易码表) 38 1.2.34 pubgrpprv (角色权限表) 38
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

swoole~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值