laravel 微信支付

1.composer加载laravel微信支付第三方文件

composer require "overtrue/laravel-wechat:~4.0"

composer require simplesoftwareio/simple-qrcode 1.3.*  //composer生成二维码文件

 

2.改config\app.php文件
'providers' => [
    // ...
    Overtrue\LaravelWeChat\ServiceProvider::class,
],
'aliases' => [
    // ...
    'EasyWeChat' => Overtrue\LaravelWeChat\Facade::class,
],

在 config/app.php 注册 ServiceProvider 和 Facade (Laravel 5.5 无需手动注册)

 

3.写微信支付控制器

<?php

namespace App\Http\Api;


use App\Model\Order;
use App\Model\OrderGoods;
use EasyWeChat\Factory;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redirect;
use SimpleSoftwareIO\QrCode\Facades\QrCode;


class WeChatController extends Controller
{
    //
    private function get()
    {
        $config = [
            'app_id' => 'your app_id',
            'mch_id' => 'your mch_id',
            'key' => 'your key',
            // 'token' => 'TestToken',
            //'response_type' => 'array',
            //'notify_url' => '',
            // 如需使用敏感接口(如退款、发送红包等)需要配置 API 证书路径(登录商户平台下载 API 证书)
            //'cert_path'          => 'path/to/your/cert.pem', // XXX: 绝对路径!!!!
            //'key_path'           => 'path/to/your/key',      // XXX: 绝对路径!!!!
            'log' => [
                'level' => 'debug',
               'permission' => 0777,
                'file' => base_path().'\wechat.log',
            ],
        ];
        //$app = app('wechat.payment');
        $app = Factory::payment($config);
        return $app;
    }

    public function code(Request $request)
    {

        $app = $this->get();

       //dd(unserialize(file_get_contents(base_path().'\massage.txt')));
//        $order = Order::find($request->order_id);
        //dd($app->order->queryByOutTradeNumber($order->sn));
        $result = $app->order->unify([
            'body' => '购买产品',
           'out_trade_no' => 's545a64',//$order->sn,
           // 'total_fee' => $order->total_price*100,
            'total_fee' => 0.01*100,
            //'spbill_create_ip' => '127.0.0.1', // 可选,如不传该参数,SDK 将会自动获取相应 IP 地址
            'notify_url' => 'http://9a58e240.ngrok.io/api/success', // 支付结果通知网址,如果不设置则会使用配置里的默认地址
            //'notify_url' => 'http://www.yzyp.com/api/success', // 支付结果通知网址,如果不设置则会使用配置里的默认地址
            'trade_type' => 'NATIVE', // 请对应换成你的支付方式对应的值类型
            'product_id' => 'wx4706a9fcbbca10f2',
            //'openid' => 'oUpF8uMuAJO_M2pxb1Q9zNjWeS6o',
        ]);
        if($result['result_code'] === 'FAIL'){
            return [
                'code'=>0,
                'message'=>$result['err_code_des'],
            ];
        }
        if($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS'){
            $re = QrCode::format('svg')->size(100)->errorCorrection("L")->generate($result['code_url']);
            return $re;
        }
    }


    public function success()
    {
        $app = $this->get();
        $response = $app->handlePaidNotify(function($message, $fail) use($app){
            //判断订单状态
            $order = Order::where('sn',$message['out_trade_no'])->first();
            if(!$order || $order->status ==1){
                return true;
              }
              //判断用户支付状态
            if ($message['return_code'] === 'SUCCESS') {
                // 用户是否支付成功
                if ($message['result_code'] === 'SUCCESS') {
                    //修改订单状态
                    $order->update([
                        'status' => 1,
                    ]);
                }
            } else {
                return $fail('通信失败,请稍后再通知我');
            }
            return true;
;        });
        return $response;
    }


    public function test()
    {
        $message = unserialize(file_get_contents(base_path().'\massage.txt'));
        file_put_contents(storage_path().'\logs\WeChat.txt',serialize($message).PHP_EOL,FILE_APPEND);

    }

}

4.调用code接口

 

PS:错误一

GuzzleHttp \ Exception \ RequestException
cURL error 60: SSL certificate problem: unable to get local issuer certificate (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)

回答:这是由于是在本地测试,不是安全路径,所以需要添加文件cacert.pem到WWW同级目录,

     例:如果使用的是phpstudy,则目录为phpStudy\PHPTutorial\cacert.pem

在php.ini中任意位置添加    curl.cainfo = "D:\phpStudy\PHPTutorial\cacert.pem"

重启phpStudy,访问code接口,扫码完成支付

转载于:https://my.oschina.net/vioyangx/blog/2247154

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值