laravel lumen 微信支付宝支付扩展

github地址:

https://github.com/yansongda/pay

文档地址:

https://pay.yansongda.cn/

扩展小程序红包

https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=18_2&index=3

 

 新建一个小程序红包类

<?php

namespace Yansongda\Pay\Gateways\Wechat;

use Symfony\Component\HttpFoundation\Request;
use Yansongda\Pay\Events;
use Yansongda\Pay\Exceptions\GatewayException;
use Yansongda\Pay\Exceptions\InvalidArgumentException;
use Yansongda\Pay\Exceptions\InvalidSignException;
use Yansongda\Pay\Gateways\Wechat;
use Yansongda\Supports\Collection;

class MiniappRedpackGateway extends Gateway
{
    /**
     * Pay an order.
     *
     * @author yansongda <me@yansongda.cn>
     *
     * @param string $endpoint
     *
     * @throws GatewayException
     * @throws InvalidArgumentException
     * @throws InvalidSignException
     */
    public function pay($endpoint, array $payload): Collection
    {
        $payload['wxappid'] = $payload['appid'];

        if ('cli' !== php_sapi_name()) {
            $payload['client_ip'] = Request::createFromGlobals()->server->get('SERVER_ADDR');
        }

        if (Wechat::MODE_SERVICE === $this->mode) {
            $payload['msgappid'] = $payload['appid'];
        }

        unset($payload['appid'], $payload['trade_type'],
              $payload['notify_url'], $payload['spbill_create_ip']);

        $payload['sign'] = Support::generateSign($payload);

        Events::dispatch(new Events\PayStarted('Wechat', 'Redpack', $endpoint, $payload));

        return Support::requestApi(
            'mmpaymkttransfers/sendminiprogramhb',
            $payload,
            true
        );
    }

    /**
     * Get trade type config.
     *
     * @author yansongda <me@yansongda.cn>
     */
    protected function getTradeType(): string
    {
        return '';
    }
} 

 调用:

$order = [
    'mch_billno' => '自己生成的交易ID',
    'send_name' => '商户名称',
    'total_amount' => 100,//单位:分
    're_openid' => '用户openId',
    'total_num' => '1',
    'wishing' => '祝福语',
    'act_name' => '活动名称',
    'remark' => '备注',
];
$config = [
    'mch_id' => '商户号',
    'app_id' => '小程序ID',
    'key' => '在微信后台自定义的key',
    'cert_client' => '.../apiclient_cert.pem',
    'cert_key' => '.../apiclient_key.pem',
];
$payWechat = Pay::wechat($config);
$transfer_res = $payWechat->miniappRedpack($order);
var_dump($transfer_res);exit();

 临时调用,后面单独抽到外部代码

官方接口:https://pay.weixin.qq.com/wiki/doc/api/tools/miniprogram_hb.php?chapter=13_9 

 

posted @ 2021-04-12 11:09  程序生(Codey)  阅读(379)  评论(0编辑  收藏  举报