PHP 第三方支付实现


PHP 第三方支付实现


正文

微信支付

微信公众平台

地址 https://mp.weixin.qq.com/

小程序支付

文档地址

https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_5_5.shtml

https://pay.weixin.qq.com/wiki/doc/apiv3/open/pay/chapter2_8_2.shtml

支付功能页 https://developers.weixin.qq.com/miniprogram/dev/framework/plugin/functional-pages/request-payment.html

小程序接入支付 https://pay.weixin.qq.com/static/applyment_guide/applyment_detail_miniapp.shtml

其他

微信小程序支付流程详解及梳理 https://www.baoge.net/article/35581.html

小程序微信支付申请与配置完整版操作流程 https://xy.ev123.net/vip_doc/14580698.html

支付宝支付

整合包支付

EasyWeChat

EasyWeChat 帮助文档 v5.x https://www.wenjiangs.com/docs/easywechat-docs-v5

EasyWeChat v4.1 开发文档 https://www.bookstack.cn/books/EasyWeChat-v4.1

yansongda/pay

源码 Github 地址 https://github.com/yansongda/pay

官方地址 https://pay.yansongda.cn/

V2版文档 https://pay.yansongda.cn/docs/v2/

V3版文档 https://pay.yansongda.cn/docs/v3/

用例参考地址 http://www.voycn.com/article/phpshiyongyansongdapayshixianzhifubaoheweixindezhifu

epay插件

微信支付宝整合epay插件 https://www.fastadmin.net/store/epay.html

不过缺少退款这块,需要自己在 addons/epayyq/library/Service.php 文件中补充:

/**
 * 提交退款
 * @param null $amount  订单金额
 * @param $refund_money   退款金额
 * @param $orderid   订单号
 * @param $refund_sn    退款订单号
 * @param $type    支付类型,alipay,wechat
 * @param null $remark   退款原因
 * @param null $notifyurl   通知url
 * @param null $returnurl   跳转url
 * @param string $method    支付方法
 * @return null|\Yansongda\Supports\Collection|static
 */
public static function submitRefund($amount = null, $refund_money, $orderid, $refund_sn, $type, $remark = null, $notifyurl = null, $returnurl = null, $method = 'app')
{
    if (!is_array($amount)) {
        $params = [
            'amount' => $amount,
            'type' => $type,
            'notifyurl' => $notifyurl,
            'returnurl' => $returnurl,
            'method' => $method
        ];
    } else {
        $params = $amount;
    }

    $type = isset($params['type']) && in_array($params['type'], ['alipay', 'wechat']) ? $params['type'] : 'wechat';
    $request = request();
    $notifyurl = isset($params['notifyurl']) ? $params['notifyurl'] : $request->root(true) . '/addons/epay/index/' . $type . 'notify';
    $returnurl = isset($params['returnurl']) ? $params['returnurl'] : $request->root(true) . '/addons/epay/index/' . $type . 'return/out_trade_no/' . $orderid;

    $config = Service::getConfig($type);
    $config['notify_url'] = $notifyurl;
    $config['return_url'] = $returnurl;
    $result = null;

    $order_data = [
        'out_trade_no' => $orderid
    ];

    if ($type == 'wechat') {
        $pay = Pay::wechat($config);
        $total_fee = $amount * 100;
        $refund_fee = $refund_money * 100;
        $order_data = array_merge($order_data, [
            'out_refund_no' => $refund_sn,
            'total_fee' => $total_fee,
            'refund_fee' => $refund_fee,
            'refund_desc' => $remark,
            'type' => $method,
        ]);
    } else {
        $pay = Pay::alipay($config);
        $order_data = array_merge($order_data, [
            'out_request_no' => $refund_sn,
            'refund_amount' => $refund_money,
        ]);
    }

    $result = $pay->refund($order_data);

    if ($result instanceof \Symfony\Component\HttpFoundation\RedirectResponse) {
        $result = RedirectResponse::create($result->getTargetUrl());
    } elseif ($result instanceof \Symfony\Component\HttpFoundation\Response) {
        $result = Response::create($result->getContent());
    } elseif ($result instanceof \Yansongda\Supports\Collection) {
        $result = Collection::make($result->all());
    }

    return $result;
}

参考资料

PHP如何保留小数点后几位 https://www.pythonf.cn/read/95140

PHP四舍五入精确小数位及取整 https://mp.weixin.qq.com/s/QM5s3ZJ-atPLUlziaMnhvg

fastadmin 支付插件退款操作 https://blog.csdn.net/weixin_39104010/article/details/112903516


返回