# Pay

# Wechat Pay

Support wechat official account, H5, scan code payment, small program payment, app payment

  1. Manage background Settings of wechat payment parameters, pay attention! APIV3 version, not APIV2
  2. Introduce namespace use sunphp\pay\SunPay
  3. Call the corresponding payment method
use sunphp\pay\SunPay;

/* 前端默认入口示例 */
class Pay {

     /* 微信公众号支付API-V3 推荐 使用util.js文件自动发起支付*/
    public function wechat(){
        //确保用户已经登录
        $account=SunAccount::create();
        $userinfo=$account->login();

        $pay_data=[
            'tid'=>date('YmdHis').'_'.mt_rand(1000000000, 9999999999),
            'money'=>0.01,//金额:元
            'title'=>'jsapi支付标题'
        ];
        return view('wechat',[
            'pay_data'=>json_encode($pay_data)
        ]);
    }


    /* 微信小程序支付API-V3 */
    public function wechatMini(){
        $wechat=SunPay::wechat();
        $order = [
            'tid'=>date('YmdHis').'_'.mt_rand(1000000000, 9999999999),
            'money'=>0.02,//金额:元
            'title'=>'微信小程序支付标题',
            'openid'=>'用户的openid'
        ];
        return $wechat->mini($order);
    }

    /* 微信手机网站H5支付API-V3 */
    /* 微信通过referer来判断来源,不能进入页面就支付 */
    public function wechatWap(){
        $wechat=SunPay::wechat();
        $order = [
            'tid'=>date('YmdHis').'_'.mt_rand(1000000000, 9999999999),
            'money'=>0.02,//金额:元
            'title'=>'微信H5网站支付标题'
        ];
        $result=$wechat->wap($order)->all();
        $h5_url=$result['h5_url'];
        return redirect($h5_url);
    }


    /* 微信APP支付API-V3 */
    public function wechatApp(){
        /*
        参数1:当前平台的id,默认为空,自动获取(也可以指定)
        参数2:微信开放平台app应用的appid,不能为空,需要设置
        */
        $wx_appid='微信开放平台app应用的appid';
        $wechat=SunPay::wechat('',$wx_appid);
        $order = [
            'tid'=>date('YmdHis').'_'.mt_rand(1000000000, 9999999999),
            'money'=>0.02,//金额:元
            'title'=>'微信APP支付标题'
        ];
        // 返回数组,作为app支付参数
        return $wechat->app($order)->toArray();
    }

     /* 微信扫码支付API-V3 */
     public function wechatScan(){
        $wechat=SunPay::wechat();
        $order = [
            'tid'=>date('YmdHis').'_'.mt_rand(1000000000, 9999999999),
            'money'=>0.02,//金额:元
            'title'=>'微信扫码支付标题'
        ];
        $result = $wechat->scan($order);
        $code_url = $result->code_url; // 二维码 url
        return view('wechatScan',[
            'code_url'=>$code_url
        ]);
    }

    /* 微信账户转账,查询,退款,关闭,取消……操作方法参考V3版本文档既可 */
    /* 微信文档地址:https://pay.yansongda.cn/docs/v3/alipay/pay.html */
    /* 功能很多,一个账户转账演示示例 */
    public function wechatTransfer(){
        $wechat=SunPay::wechat();
        // 自定义回调参数
        $order = [
            'out_batch_no' => time().'',
            'batch_name' => 'subject-测试',
            'batch_remark' => 'test',
            'total_amount' => 1,
            'total_num' => 1,
            'transfer_detail_list' => [
                [
                    'out_detail_no' => time().'-1',
                    'transfer_amount' => 1,
                    'transfer_remark' => 'test',
                    'openid' => 'MYE42l80oelYMDE34nYD456Xoy',
                    // 'user_name' => '名字'  // 明文传参即可,sdk 会自动加密
                ],
            ],
        ];
        $result = $wechat->transfer($order);
    }


}

# Alipay

Support web payment, H5, scan code payment, small program payment, app payment

  1. Manage background Settings of Alipay payment parameters, pay attention!
  2. Introduce namespace use sunphp\pay\SunPay
  3. Call the corresponding payment method
use sunphp\pay\SunPay;

/* 前端默认入口示例 */
class Pay {

/* 支付宝Web支付 */
    public function alipayWeb(){
        $alipay=SunPay::alipay();
        return $alipay->web([
            'tid' => date('YmdHis').'_'.mt_rand(1000000000, 9999999999),
            'money' => '0.01',//金额:元
            'title' => '支付宝web支付测试',
        ]);
    }

    /* 支付宝手机H5网页支付 */
    public function alipayH5(){
        $alipay=SunPay::alipay();
        // 自定义回调参数
        return $alipay->wap([
            'tid' =>date('YmdHis').'_'.mt_rand(1000000000, 9999999999),
            'money' => '0.01',//金额:元
            'title' => '支付宝H5支付测试'
         ]);
    }

    /* 支付宝App支付 */
    public function alipayApp(){
        $alipay=SunPay::alipay();
        // 自定义回调参数
        return $alipay->app([
            'tid' =>date('YmdHis').'_'.mt_rand(1000000000, 9999999999),
            'money' => '0.01',//金额:元
            'title' => '支付宝App支付测试'
         ])->getBody()->getContents();
    }


    /* 支付宝小程序支付 */
    public function alipayMini(){
        $alipay=SunPay::alipay();
        // 自定义回调参数
        $result=$alipay->mini([
            'tid' =>date('YmdHis').'_'.mt_rand(1000000000, 9999999999),
            'money' => '0.01',//金额:元
            'title' => '支付宝小程序支付测试',
            'buyer_id' => '2088622190161234',
         ]);
        return $result->get('trade_no');  // 支付宝交易号
    }


    /* 支付宝刷卡支付 */
    public function alipayPos(){
        $alipay=SunPay::alipay();
        // 自定义回调参数
        $result=$alipay->pos([
            'tid' =>date('YmdHis').'_'.mt_rand(1000000000, 9999999999),
            'money' => '0.01',//金额:元
            'title' => '支付宝刷卡支付测试',
            'auth_code' => '2088622190161234',
         ]);
    }


    /* 支付宝扫码支付 */
    public function alipayScan(){
        $alipay=SunPay::alipay();
        // 自定义回调参数
        $result=$alipay->scan([
            'tid' =>date('YmdHis').'_'.mt_rand(1000000000, 9999999999),
            'money' => '0.01',//金额:元
            'title' => '支付宝H5支付测试'
         ]);
        $qr_code = $result->qr_code; // 二维码 url
        return response($qr_code);
    }


    /* 支付账户转账,查询,退款,关闭,取消……操作方法参考V3版本文档既可 */
    /* 支付宝文档地址:https://pay.yansongda.cn/docs/v3/alipay/pay.html */
    /* 功能很多,一个账户转账演示示例 */
    public function alipayTransfer(){
        $alipay=SunPay::alipay();
        // 自定义回调参数
        $result = $alipay->transfer([
            'out_biz_no' => '202106051432',
            'trans_amount' => '0.01',
            'product_code' => 'TRANS_ACCOUNT_NO_PWD',
            'biz_scene' => 'DIRECT_TRANSFER',
            'payee_info' => [
                'identity' => 'ghdhjw7124@sandbox.com',
                'identity_type' => 'ALIPAY_LOGON_ID',
                'name' => '沙箱环境'
            ],
        ]);
    }



}

# Callback notification

After the payment is successful, the system will automatically call back the notification. The method of receiving the notification is as follows

  1. In the/module /controller/ directory, create the PayResult.php file
  2. The notify() method in the PayResult controller receives asynchronous notifications
  3. The return() method in the PayResult controller receives synchronization notifications
  4. Pay attention! Do not rely on synchronous notifications!!

# Creates a PayResult controller

└ ─ controller # Controller directory
└ ── PayResult.php # PayResult Controller

# notify() The parameters of the asynchronous notification are as follows

$post=request()->post();

$post=[
            'from'=>'notify',
            'result'=>'success',
            'type'=>'wechat',
            'acid'=>'平台的id',
            'module'=>'支付的模块标识',
            'tid'=>'统一支付订单号out_trade_no',
            'fee'=>'订单总金额,单位元'
        ];

The full PayResult.php file is referenced below


class PayResult{

    /* 支付结果异步通知 */
    public function notify(){
        // 模块内支付回调,post参数如下
        $post=request()->post();

        /* 处理好业务逻辑,并且标记是否已经处理过 */

        // $post=[
        //     'from'=>'notify',
        //     'result'=>'success',
        //     'type'=>'wechat',
        //     'acid'=>'平台的id',
        //     'module'=>'支付的模块标识',
        //     'tid'=>'统一支付订单号out_trade_no',
        //     'fee'=>'订单总金额,单位元'
        // ];
        //无需返回值,处理业务逻辑既可!
    }


    /* 支付结果同步通知 */
    public function return(){
        // 模块内支付回调,get参数如下
        $get=request()->get();

        // 模块内支付回调return通知

        // 请勿处理业务逻辑,仅做跳转既可
        return redirect(request()->domain());
    }



}