Laravel5.5 Alipay mobile phone website payment

Time:2021-8-23

1. Install and use the laravel extension library yansongda / laravel pay

Yansongda / laravel pay GitHub address

Install through composer

$ composer require yansongda/laravel-pay

Generate profile

$ php artisan vendor:publish --provider="Yansongda\\LaravelPay\\PayServiceProvider" --tag=laravel-pay

2. Configure payment information and fill in the configuration file pay.php

Laravel5.5 Alipay mobile phone website payment

The tools provided by running Alipay are as follows:
Laravel5.5 Alipay mobile phone website payment

Remove —– begin public key —– and —– end public key —– from the public key file, merge the remaining contents into one line and add them to the application public key
Remove —– begin RSA private key —– and —– end RSA private key —– from the private key file, merge the remaining contents into one line and add them to the configuration file
Fill in the corresponding content in the configuration file
Laravel5.5 Alipay mobile phone website payment

3. Pull up payment

Pulling up payment in yansongda / laravel pay is to directly return the payment form information to the page. Considering the business requirements, it is more convenient to return it through JSON
Laravel5.5 Alipay mobile phone website payment

use Yansongda\Pay\Pay;

/**
 * Class SecondPay
 * @package App
 * Alipay mobile phone web payment interface
 */
class ZfbWapPay extends Model
{

    //Send payment request
    public function send(Request $request,$outTradeNum,$payAmount)
    {
        $config =  config('pay.alipay'); // Take out the configuration information and change it dynamically according to the requirements
        $config['return_url'] ='http://api.aiwangyouxi.com/web/pay/jump/'.$outTradeNum;

        $order = [
            'out_ trade_ No '= > $outtradenum, // our order No
            'total_ Amount '= > $payamount, // payment amount, unit (yuan)
            'subject '= >' game payment ',
        ];

        $alipay = Pay::alipay($config)->wap($order);
        return $alipay; // Return form information
    }
}

Successfully return the form information, insert the information into the page, and complete the pull-up payment

$("body").append(data.form_text);

4. Payment callback

use Yansongda\Pay\Pay;

/**
 * Class WxwebController
 * @package App\Http\Controllers\Web
 * Alipay H5 payment callback
 */
class ZfbwebController extends Controller
{
    private $pay_ order;     // Our order No
    private $call_ order;    // Third party order number

    public function notify(Request $request)
    {
        $pay_config = config('pay.alipay');
        $alipay = Pay::alipay($pay_config);

        $this->pay_order = $request->out_trade_no;
        $this->call_order = $request->trade_no;

        $save_ back = $m_ pay_ back->addback($this->pay_ order,json_ encode($request->all()));  // Record the payment callback information in the database
        $check_result = $alipay->verify();
        if(!$ check_ Result) {// the callback failed to verify the signature
            return '';
        }

        return $alipay->success();
    }
}