PHP code example of overnic / payment

1. Go to this page and download the library: Download overnic/payment library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

overnic / payment example snippets



// 加载配置信息
$config =  OverNick\Payment\PaymentManage($config);



// 参考文档:https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_1
// 创建订单
$result = $pay->order->create([
    'out_trade_no' => 'D201705030000001',       // 商户订单号
    'body' => '测试商品',                        // 交易简介
    'total_fee' => 1,                           // 金额,已分为单位
    'notify_url' => 'http://www.baidu.com',     // 支付成功通知地址
    'trade_type' => 'NATIVE',                   // 支付方式,详见文档
])

    // 微信支付的实例 
    $pay = $pay = $manage->driver('wechatpay');
    // 实例化类,构造参数中需求的为微信支付的实例,并且使用 handle 方法进行处理,参数值为一个闭包
   (new OverNick\Payment\Wechat\Notify\Paid($pay))->handle(function($data){
        // $data 为微信提交的数据,信息参考 https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_7&index=8
        
        // 如果结果已处理,需要告诉微信服务器不再通知
        // return true;
        
        // 剩余逻辑,例如增加用户余额,订单支付状态更改等。
        // ...
        
        // 最终返回一个 boolean 值,如果返回为false,微信会再次通知
        return true;
   })

参考文档:https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_4
// 发起退款
$result = $pay->refund->create([
    'out_trade_no' => '商户订单号',
    'out_refund_no' => '商户退款单号',
    'total_fee' => 1,                   // 订单总金额
    'refund_fee' => 1                   // 退款金额
    ]);

    // 微信支付的实例 
    $pay = $pay = $manage->driver('wechatpay');
    // 实例化类,构造参数中需求的为微信支付的实例,并且使用 handle 方法进行处理,参数值为一个闭包
   (new OverNick\Payment\Wechat\Notify\Refunded($pay))->handle(function($data){
        // $data 为微信提交的数据,信息参考 https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_16&index=10
        
        // 如果结果已处理,需要告诉微信服务器不再通知
        // return true;
        
        // 剩余处理逻辑
        // ...
        
        // 最终返回一个 boolean 值,如果返回为false,微信会再次通知
        return true;
   }) 

// 参考文档:https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=9_10&index=1
$result = $pay->pay->create([
    'body' => '这是一个商品',             // 商品描述
    'out_trade_no' => '202004160001',   // 商户订单号
    'total_fee' => 1,                   // 金额,已分为单位
    'auth_code' => '120061098828009406' // 微信支付码,扫描二维码或条形码结果
]);

    // JSAPI支付,
    // 流程,先进行统一下单操作,完成后,获取到下单接口返回的预支付id,然后进行参数获取
    $orderInfo = $pay->order->create([
         'out_trade_no' => 'D201705030000001',       // 商户订单号
            'body' => '测试商品',                        // 交易简介
            'total_fee' => 1,                           // 金额,已分为单位
            'notify_url' => 'http://www.baidu.com',     // 支付成功通知地址
            'trade_type' => 'JSAPI',                   // 支付方式,详见文档
    ]);
    // 获取到预支付id
    $prepay_id = $orderInfo['prepay_id'];
    
    // 获取需要输出给js使用的结果
    $result = $pay->pay->jsApi($prepay_id);
    
    // 成功请求的最终$result结果格式如下
    {
    "appId":"xxxxxxx",
    "timeStamp":1230894414,
    "nonceStr":"123456",
    "package":"prepay_id=123456789",
    "signType":"MD5" 
    }
    // 参考文档:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_7&index=6

    // 小程序支付
    
    // !!!  重要
    // !!!  重要
    // !!!  重要
    // 使用小程序的appId进行操作
    $pay->useMiniAppId();
    
    // 流程,先进行统一下单操作,完成后,获取到下单接口返回的预支付id,然后进行参数获取
    $orderInfo = $pay->order->create([
        'out_trade_no' => 'D201705030000001',       // 商户订单号
        'body' => '测试商品',                        // 交易简介
        'total_fee' => 1,                           // 金额,已分为单位
        'notify_url' => 'http://www.baidu.com',     // 支付成功通知地址
        'trade_type' => 'JSAPI',                   // 支付方式,详见文档
    ]);
    // 获取到预支付id
    $prepay_id = $orderInfo['prepay_id'];
    
    // 获取需要输出给小程序使用的结果
    $result = $pay->pay->jsApi($prepay_id);
    
    // 成功请求的最终$result结果格式如下
    {
    "appId":"xxxxxxx",
    "timeStamp":1230894414,
    "nonceStr":"123456",
    "package":"prepay_id=123456789",
    "signType":"MD5" 
    }
    参考文档:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_7&index=6

// 参考文档:https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_4&index=3
$result = $pay->balance->redpack([
    'send_name' => '测试发红包',                   // 红包发送者名称
    'mch_billno' => '202004160001',             // 订单号
    're_openid' => 'xxxxxxxxxxxxxxxxxxxxs',    // 用户openid
    'total_amount' => 1,                       // 金额
    'total_num' => 1,                          // 红包发放人数
    'wishing' => '发红包啦',                    // 红包祝福语
    'act_name' => '红包活动',                    // 	活动名称
    'remark' => '这是备注',                     // 备注信息
]);

// 向用户转账
//参考文档:https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=14_2
$result = $pay->balance->transfer([
    'partner_trade_no' => '202004160001',       // 商户订单号
    'openid' => 'xxxxxxxxxxxxxxxxxxxxs',        // 用户openid
    'amount' => 1,                              // 金额
    'desc' => '测试'                            //企业付款备注
]);

// 实例化二维码类
$qrcode = new OverNick\Payment\Kerner\Tools\QrCode('二维码内容');
// 设置二维码长度
$qrcode->setWith(250);
// 设置二维码宽度
$qrcode->setHeight(250);

// 获取二维码图片内容
$content = $qrcode->content();

// 直接输出图片
$qrcode->write();

// 参考文档:https://docs.open.alipay.com/api_1/alipay.trade.create/
$params = [
    'notify_url' => 'http://123456789.cn',
    'out_trade_no' => '商户订单号',
    'total_amount' => 0.01,         // 金额,单位为元
    'subject' => '购买商品',         // 商品标题
    'body' => '购买一部iPhoneX'      // 商品内容
];
$result = $pay->order->create($params);

// 参考文档:https://docs.open.alipay.com/api_1/alipay.trade.precreate/
// 请求参数
$params = [
    'notify_url' => 'http://123456789.cn',
    'out_trade_no' => '商户订单号',
    'total_amount' => 0.01,         // 金额,单位为元
    'subject' => '购买商品',         // 商品标题
    'body' => '购买一部iPhoneX'      // 商品内容
];
$result = $pay->order->preCreate($params);

$result = $pay->order->queryByOutTradeNo('商户订单号');
$result = $pay->order->queryByTradeNo('支付宝订单号');

$result = $pay->order->closeByOutTradeNo('商户订单号');
$result = $pay->order->closeByTradeNo('支付宝订单号');

// 参考文档:https://docs.open.alipay.com/api_1/alipay.trade.refund/
$params = [
    'out_trade_no' => '商户订单号',
    'out_request_no' => '商户退单号',
    'refund_amount' => 0.01,
    'refund_reason' => '不想要了'
];
$result = $pay->refund->create($params);

// 参考地址:https://docs.open.alipay.com/api_1/alipay.trade.pay
$params = [
    'notify_url' => 'http://123456789.cn',      // 收款成功通知地址
    'subject' => '商品购买2',                    // 商品标题
    'out_trade_no' => '201904160011',           // 交易订单号
    'scene' => 'bar_code',                      // 支付方式,wave_code表示声波,bar_code表示条码,默认条码支付
    'body' => 'iPhone X 赠送贴膜',               // 交易内容
    'total_amount' => 0.01,                     // 金额
    'auto_code' => '12345678902123'             // 支付授权码
];
$result = $pay->refund->create($params);

// $pay 为支付宝的实例
(new OverNick\Payment\Alipay\Notify\Paid($pay)->handle(function($data) {
    // 验证是否支付成功
    if (!in_array($data['trade_status'], ['TRADE_SUCCESS', 'TRADE_FINISHED'])) {
        return false;
    }
    
    // 后续处理逻辑
    
    // 返回boolean , 如果返回为false,则支付宝会继续通知
    return true;

// 加载配置文件
$config =  = new OverNick\Payment\PaymentManage($config);

// 使用扩展方法
$payment->extend('baidupay', function(){
    // 要求扩展的类继承于
    // OverNick\Payment\Kernel\ServiceContainer 类
    // ...
    // ...
    return new BaiduApp();
})

// 使用
$payment->driver('baidupay')