tp5.1 微信支付、支付宝、招商支付(Payment)

源地址:https://github.com/helei112g/payment

安装

当前 Payment 项目仅支持 PHP version > 7.0 的版本,并且仅支持通过 composer 进行安装。

需要 PHP 安装以下扩展:

- ext-curl
- ext-mbstring
- ext-bcmath
- package-Guzzle

Composer安装

composer require "riverslei/payment:*"

直接上代码

<?php
/**
 * Created by PhpStorm.
 * User: Zhangyongfeng
 * Date: 2020/11/24
 * Time: 11:27
 *
 * ━━━━━━━━━神兽出没━━━━━━━━━
 *
 *        ┏┓   ┏┓+ +
 *       ┏┛┻━━━┛┻┓ + +
 *       ┃       ┃  
 *       ┃   ━   ┃ ++ + + +
 *       ████━████ ┃+
 *       ┃       ┃ +
 *       ┃   ┻   ┃
 *       ┃       ┃ + +
 *       ┗━┓   ┏━┛
 *         ┃   ┃           
 *         ┃   ┃ + + + +
 *         ┃   ┃    Code is far away from bug with the animal protecting       
 *         ┃   ┃ +     神兽保佑,代码无bug  
 *         ┃   ┃
 *         ┃   ┃  +         
 *         ┃    ┗━━━┓ + +
 *         ┃        ┣┓
 *         ┃        ┏┛
 *         ┗┓┓┏━┳┓┏┛ + + + +
 *          ┃┫┫ ┃┫┫
 *          ┗┻┛ ┗┻┛+ + + +
 *
 * ━━━━━━━━━感觉萌萌哒━━━━━━━━━
 */

namespace app\base\controller;

use think\App;

class Pay extends Base
{
    protected $wxConfig = [];
    protected $aliConfig = [];

    public function __construct(App $app = null)
    {
        parent::__construct($app);

        $this->wxConfig = [
            ‘use_sandbox‘ => true, // 是否使用 微信支付仿真测试系统
            ‘app_id‘ => $this->config[‘cfg_wxAppid‘],  // 公众账号ID
            ‘mch_id‘ => $this->config[‘cfg_wxMchid‘],  // 商户id
            ‘md5_key‘ => $this->config[‘cfg_wxMd5key‘], // md5 秘钥
            ‘app_cert_pem‘ => dirname(__FILE__) . DIRECTORY_SEPARATOR . ‘wx‘ . DIRECTORY_SEPARATOR . ‘pem‘ . DIRECTORY_SEPARATOR . ‘apiclient_cert.pem‘,
            ‘app_key_pem‘ => dirname(__FILE__) . DIRECTORY_SEPARATOR . ‘wx‘ . DIRECTORY_SEPARATOR . ‘pem‘ . DIRECTORY_SEPARATOR . ‘apiclient_key.pem‘,
            ‘sign_type‘ => ‘MD5‘, // MD5  HMAC-SHA256
            ‘limit_pay‘ => [], // 指定不能使用信用卡支付   不传入,则均可使用
//            ‘limit_pay‘    => [ //‘no_credit‘, ], // 指定不能使用信用卡支付   不传入,则均可使用
            ‘fee_type‘ => ‘CNY‘, // 货币类型  当前仅支持该字段
            ‘notify_url‘ => $_SERVER[‘SERVER_NAME‘] . "/common/Pay/wxNotify",
            ‘redirect_url‘ => ‘https://dayutalk.cn/‘ // 如果是h5支付,可以设置该值,返回到指定页面
        ];

        $this->aliConfig = [
            ‘use_sandbox‘ => true, // 是否使用沙盒模式
            ‘app_id‘ => $this->config[‘cfg_aliAppid‘],
            ‘sign_type‘ => ‘RSA2‘, // RSA  RSA2
            // 支付宝公钥字符串
            ‘ali_public_key‘ => $this->config[‘cfg_aliPublicKey‘],
            // 自己生成的密钥字符串
            ‘rsa_private_key‘ => $this->config[‘cfg_aliPrivateKey‘],
            ‘limit_pay‘ => [
                //‘balance‘,// 余额
                //‘moneyFund‘,// 余额宝
                //‘debitCardExpress‘,//     借记卡快捷
                //‘creditCard‘,//信用卡
                //‘creditCardExpress‘,// 信用卡快捷
                //‘creditCardCartoon‘,//信用卡卡通
                //‘credit_group‘,// 信用支付类型(包含信用卡卡通、信用卡快捷、花呗、花呗分期)
            ], // 用户不可用指定渠道支付当有多个渠道时用“,”分隔
            // 与业务相关参数
            ‘notify_url‘ => ‘https://dayutalk.cn/notify/ali‘,
            ‘return_url‘ => ‘https://dayutalk.cn‘,
            ‘fee_type‘ => ‘CNY‘, // 货币类型  当前仅支持该字段
        ];

    }

    /**
     * 微信支付
     * @param string $type ‘app‘ =>     APP
     * @param string $type ‘pub‘ =>     公众号
     * @param string $type ‘qr‘ =>      扫码支付(可以使用app的帐号,也可以用公众的帐号完成)
     * @param string $type ‘single‘ =>  单次分账
     * @param string $type ‘bar‘ =>     刷卡支付,与支付宝的条码支付对应
     * @param string $type ‘lite‘ =>    小程序
     * @param string $type ‘wap‘ =>     wap支付,针对特定用户
     * @param string $type ‘multi‘ =>   多次分账
     * @param array $data[‘body‘]           商品描述
     * @param array $data[‘subject‘]        商品的标题
     * @param array $data[‘trade_no‘]       商户网站唯一订单号
     * @param array $data[‘time_expire‘]    该笔订单允许的最晚付款时间,逾期将关闭交易
     * @param array $data[‘amount‘]         订单总金额,单位为元,精确到小数点后两位,取值范围[0.01,100000000]
     * @param array $data[‘return_param‘]   公用回传参数,如果请求时传递了该参数,则返回给商户时会回传该参数。
     * @param array $data[‘client_ip‘]      必须传正确的用户端IP,支持ipv4、ipv6格式,获取方式详见
     * @return string|array
     * */
    public function Pay($type = "app", $data = [])
    {
        // 请求参数,完整参数见具体表格
        $payData = [
            ‘body‘ => ‘test body‘,
            ‘subject‘ => ‘test subject‘,
            ‘trade_no‘ => ‘trade no‘,// 自己实现生成
            ‘time_expire‘ => time() + 600, // 表示必须 600s 内付款
            ‘amount‘ => ‘5.52‘, // 微信沙箱模式,需要金额固定为3.01
            ‘return_param‘ => ‘123‘,
            ‘client_ip‘ => isset($_SERVER[‘REMOTE_ADDR‘]) ? $_SERVER[‘REMOTE_ADDR‘] : ‘127.0.0.1‘, // 客户地址
        ];
        if($data){ foreach($data as $k => $v){ if($payData[$k]){ $payData[$k] = $v; } } }
        // 使用
        try {
            $client = new \Payment\Client(\Payment\Client::WECHAT, $this->wxConfig);
            switch ($type){
                case "app":
                    $res = $client->pay(\Payment\Client::WX_CHANNEL_APP, $payData);
                    break;
                case "pub":
                    $res = $client->pay(\Payment\Client::WX_CHANNEL_PUB, $payData);
                    break;
                case "qr":
                    $res = $client->pay(\Payment\Client::WX_CHANNEL_QR, $payData);
                    break;
                case "single":
                    $res = $client->pay(\Payment\Client::WX_CHANNEL_BAR, $payData);
                    break;
                case "bar":
                    $res = $client->pay(\Payment\Client::WX_CHANNEL_LITE, $payData);
                    break;
                case "lite":
                    $res = $client->pay(\Payment\Client::WX_CHANNEL_WAP, $payData);
                    break;
                case "wap":
                    $res = $client->pay(\Payment\Client::WX_SETTLE_SINGLE, $payData);
                    break;
                case "multi":
                    $res = $client->pay(\Payment\Client::WX_SETTLE_MULTI, $payData);
                    break;
            }
            return $res;
        } catch (\InvalidArgumentException $e) {
            echo $e->getMessage();
            exit;
        } catch (\Payment\Exceptions\GatewayException $e) {
            echo $e->getMessage();
            var_dump($e->getRaw());
            exit;
        } catch (\Payment\Exceptions\ClassNotFoundException $e) {
            echo $e->getMessage();
            exit;
        } catch (\Exception $e) {
            echo $e->getMessage();
            exit;
        }
    }

    /*
     * 微信支付回调
     * */
    public function wxNotify()
    {
        // 实例化继承了接口的类
        $callback = new PayNotify();
        try {
            $client = new \Payment\Client(\Payment\Client::WECHAT, $this->wxConfig);
            $xml = $client->notify($callback);
            //写业务逻辑

        } catch (\InvalidArgumentException $e) {
            echo $e->getMessage();
            exit;
        } catch (\Payment\Exceptions\GatewayException $e) {
            echo $e->getMessage();
            exit;
        } catch (\Payment\Exceptions\ClassNotFoundException $e) {
            echo $e->getMessage();
            exit;
        } catch (\Exception $e) {
            echo $e->getMessage();
            exit;
        }
    }

    /**
     * 支付宝支付
     * @param string $type ‘app‘ => 手机app
     * @param string $type ‘wap‘ => 手机网页
     * @param string $type ‘web‘ => PC网页
     * @param string $type ‘qr‘ =>  扫码支付
     * @param string $type ‘bar‘ => 条码支付
     * @param array $data[‘body‘]           商品描述
     * @param array $data[‘subject‘]        商品的标题
     * @param array $data[‘trade_no‘]       商户网站唯一订单号
     * @param array $data[‘time_expire‘]    该笔订单允许的最晚付款时间,逾期将关闭交易
     * @param array $data[‘amount‘]         订单总金额,单位为元,精确到小数点后两位,取值范围[0.01,100000000]
     * @param array $data[‘return_param‘]   公用回传参数,如果请求时传递了该参数,则返回给商户时会回传该参数。
     * @param array $data[‘goods_type‘]     商品主类型 :0-虚拟类商品,1-实物类商品
     * @param array $data[‘store_id‘]       商户门店编号
     * @return string|array
     * */
    public function aliPay($type = "app", $data = [])
    {
        $payData = [
            ‘body‘         => ‘ali qr pay‘,
            ‘subject‘      => ‘测试支付宝APP支付‘,
            ‘trade_no‘     => time() . rand(1000, 9999),
            ‘time_expire‘  => time() + 600, // 表示必须 600s 内付款
            ‘amount‘       => ‘0.01‘, // 单位为元 ,最小为0.01
            ‘return_param‘ => ‘123123‘,
            ‘goods_type‘ => ‘1‘, // 0—虚拟类商品,1—实物类商品
            ‘store_id‘   => ‘‘,
        ];
        if($data){ foreach($data as $k => $v){ if($payData[$k]){ $payData[$k] = $v; } } }
        // 使用
        try {
            $client = new \Payment\Client(\Payment\Client::ALIPAY, $this->aliConfig);
            switch ($type){
                case "web":
                    $res    = $client->pay(\Payment\Client::ALI_CHANNEL_WEB, $payData);
                    break;
                case "app":
                    $res    = $client->pay(\Payment\Client::ALI_CHANNEL_APP, $payData);
                    break;
                case "wap":
                    $res    = $client->pay(\Payment\Client::ALI_CHANNEL_WAP, $payData);
                    break;
                case "qr":
                    $res    = $client->pay(\Payment\Client::ALI_CHANNEL_QR, $payData);
                    break;
                case "bar":
                    $res    = $client->pay(\Payment\Client::ALI_CHANNEL_BAR, $payData);
                    break;
            }
            return $res;
        } catch (\InvalidArgumentException $e) {
            echo $e->getMessage();
            exit;
        } catch (\Payment\Exceptions\GatewayException $e) {
            echo $e->getMessage();
            var_dump($e->getRaw());
            exit;
        } catch (\Payment\Exceptions\ClassNotFoundException $e) {
            echo $e->getMessage();
            exit;
        } catch (\Exception $e) {
            echo $e->getMessage();
            exit;
        }
    }

    /*
     * 支付宝支付回调
     * */
    public function aliNotify()
    {
        // 实例化继承了接口的类
        $callback = new PayNotify();
        try {
            $client = new \Payment\Client(\Payment\Client::ALIPAY, $this->aliConfig);
            $xml = $client->notify($callback);
            // 写业务逻辑

        } catch (\InvalidArgumentException $e) {
            echo $e->getMessage();
            exit;
        } catch (\Payment\Exceptions\GatewayException $e) {
            echo $e->getMessage();
            exit;
        } catch (\Payment\Exceptions\ClassNotFoundException $e) {
            echo $e->getMessage();
            exit;
        } catch (\Exception $e) {
            echo $e->getMessage();
            exit;
        }
    }



}

调用

$pay = new Pay();
header("location:". $pay->aliPay(‘pub‘));;

 

tp5.1 微信支付、支付宝、招商支付(Payment)

上一篇:使用kubeadm安装kubernetes1.19版本(一)基础环境配置和集群初始化


下一篇:DockerHub镜像上传下载