微信小程序支付宝支付
发布于 3 年前 作者 ming63 811 次浏览 来自 分享
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Yansongda\Pay\Pay;
use Yansongda\Pay\Log;

class PayController extends Controller
{
    //
    protected $config = [
        'app_id' => '',
        //异步回调地址
        'notify_url' => 'http://www.exam2.com/api/notify',
        //同步回调地址
        'return_url' => 'http://www.exam2.com/api/return',
        'ali_public_key' => '',
        // 加密方式: **RSA2**
        'private_key' => 'MIIEowIBAAKCAQEAlpF/znMZshLfi2QL+sNAKGykUmHi0vm8UKJ271hBkww95fEEMZki8SP0PLPM8AYYM8Tk8M8ccCrqMW6SKMtKuaSF+7i0VAqse3ODGJWiikiXrGvZ6XWpbLADrHKjf7As5U0Oek/1kajPKL7P6FMITVYzDhNv735VhwV8e/HFfg1ZViLm6aH406/PxwWggWHMlPnJtkB+SAkwzVKbCAg1ZbPitZFM/GQVP3O8XAcYY83P7m7wf2cjtoaAhDZp6jMiGrHxEkZPrs6lfF4kcAPV0hC8HgitS3+WpYmZj6Y0Fkw4g82YPUabR3xo0LhR82tqFLEU7VqJkbhZ+vqWCFZtIQIDAQABAoIBABDl1V0bB8g3XAUtvjZvUTT4NFzWhXk988ds5EIqOnnBRvUWXm0ejZBYg6GIYDiTxoWQ1Ey+QafI+S2BBkAzyHgbIbpFWV+RLxhPt2e761jvmq4MEJGl1dTP0Cm7V+QNpow5YKWTcbDeLBmbiIO/RdBAG0y6G7baC8kycW0Ljil+M9iNI8geuJ5eYkRckon6AX+IYct7+IZRl8BD0loMyE9rHX8WvcjU62gYHv27Pgv6Dyj8g452euauYJ7rpjfjlnkljh7JhClLJ/EfsdLPkIzqYTbabuZVR7pXvi9lVDPOO5Kkv3OoRshZt8ZCTSIRBxjdciv4RPZuq7KqsFqvVB0CgYEA0xSc2Ht4TVgdSj1/I3XyN4K3WPpoM7ei2bHfX1zOAEOEruhFymzVGItqRrTv4IPJLpolV9KqGUpT9GBhN6IzhVlG6tMjwliDNmfGOjFhW90DJqCFKc+p4ZK7g8a8vFAAGRL+sUUEAcIO+9mDw8SrEpo0rGfkDTwByQQV1n3iPXsCgYEAtpxDmYHPkMh+fGRbnFD+npbpC+eawOUvB9u8GI3Gz/HbxlRD7TjU55NM+xH4+rZ9k8DTIb4bAUNTQTn+tycdAtEUc7NLzJ8ZKlz34LCuZwGEISjgjy/PycLxbgxz+ddEhZnwI42odlhV/ycYl6MaYTpanDG0IUHZ/pemcbxghxMCgYB6MX2W6Os8wxy5pJ4cNiRRNE/Vp6h4//67TXhFKWFrz6pY4se8JoK6IcMVSZtv+nqVitDBZYfo+hld6Al/flua/5LJUA9sa3cSIMMlXv7AglJXoydTjA8JMbtxzrPlw946pb9hedWW+vNXopTf2NM8Ap8piz7+1lUmPEI0GE3HTwKBgEWJY8SExgGgF2pO3QO9Y/gIB15dJ3Tk0O/xIMNI2bg9pS70z5j51zkHwnyo72KpM1CcUMm8lpP4iy9/YhtkbYuxClS0pdyZiP+0vz90iDT5QtNNuZVM45c1ALKjU1aEPreRUvx26PuHz9eFj7vOhDEaA38O/kTbyqNEJEGTNj65AoGBAKsF0oBBMDtiihf8MYR1kVNlCcCNDTUTryrGNp+VkJniMvAbAopf26Jkrm6VuI8zxICF4RM3uNJ+fQpTRjtVCIUFoYxlzqrckv/hd+nSwvjUwsirAaJnL5u3cf00KWJN0q8INi1U/6K65VF+UOImN8UHgGipj/HPuHm9UrMjshUw',
        'log' => [ // optional
            'file' => './logs/alipay.log',
            'level' => 'info', // 建议生产环境等级调整为 info,开发环境为 debug
            'type' => 'single', // optional, 可选 daily.
            'max_file' => 30, // optional, 当 type 为 daily 时有效,默认 30 天
        ],
        'http' => [ // optional
            'timeout' => 5.0,
            'connect_timeout' => 5.0,
            // 更多配置项请参考 [Guzzle](https://guzzle-cn.readthedocs.io/zh_CN/latest/request-options.html)
        ],
        'mode' => 'dev', // optional,设置此参数,将进入沙箱模式
    ];

    public function index(request $request)
    {
        $price = $request->get('price');
        $title = $request->get('title');

        $order = [
            'out_trade_no' => time().rand(1111,9999),
            'total_amount' => $price,
            'subject' => $title,
        ];

        $alipay = Pay::alipay($this->config)->web($order);

        return  $alipay;// laravel 框架中请直接 `return $alipay`
    }

    public function return()
    {
        $data = Pay::alipay($this->config)->verify(); // 是的,验签就这么简单!

        // 订单号:$data->out_trade_no
        // 支付宝交易号:$data->trade_no
        // 订单总金额:$data->total_amount
    }

    public function notify()
    {
        $alipay = Pay::alipay($this->config);

        try{
            $data = $alipay->verify(); // 是的,验签就这么简单!
            dd($data);
            // 请自行对 trade_status 进行判断及其它逻辑进行判断,在支付宝的业务通知中,只有交易通知状态为 TRADE_SUCCESS 或 TRADE_FINISHED 时,支付宝才会认定为买家付款成功。
            // 1、商户需要验证该通知数据中的out_trade_no是否为商户系统中创建的订单号;
            // 2、判断total_amount是否确实为该订单的实际金额(即商户订单创建时的金额);
            // 3、校验通知中的seller_id(或者seller_email) 是否为out_trade_no这笔单据的对应的操作方(有的时候,一个商户可能有多个seller_id/seller_email);
            // 4、验证app_id是否为该商户本身。
            // 5、其它业务逻辑情况

            Log::debug('Alipay notify', $data->all());
        } catch (\Exception $e) {
             $e->getMessage();
        }

        return $alipay->success()->send();// laravel 框架中请直接 `return $alipay->success()`
    }

}
composer require yansongda/pay:^2.10 -vvv
回到顶部