Laravel5.5 Alipay payment

Posted by sms.sanal on Sat, 02 Oct 2021 20:27:12 +0200

to configure

The application for merchant account service is very troublesome. In order to quickly test, you can use the sandbox beta app, but currently only support Android phones

The service to be tested can only be tested after real name authentication and identity extension. In other words, you can fill in some personal information. It's very simple. I won't introduce it in detail here

Ant gold open platform
Ant gold sandbox beta

explain

private_key private key

private_ The key is the private key of the merchant application. I download the Mac version (the download address of RSA signature tool). The format of generating the secret key should be PKCS1 (not JAVA), but I always fail to generate it here. I don't know if it's because of black apple_ T. So here's how I deal with it:

  1. Select the format of secret key first, pkcs8 (applicable to Java), and click generate secret key
  2. Copy the private key of the merchant application, click the format change and paste in the label column, click transfer to PKCS1 (non JAVA) private key, and the generated content is private in the configuration below_ Value of key


ali_public_key public key

Be careful, ali_public_key is not the public key generated by the RSA signature tool, but the RSA2 (SHA256) key Alipay public key in the background application of Alipay.

Get method:

  1. Click to generate the copied content of the public key file in the format conversion of RSA signature verification tool
  2. Enter the background, set the secret key of RSA2 (SHS256), apply the public key, and paste the content just copied

  1. After you save it, click the Alipay public key to get the Alipay public key.



Secret key debugging tool

configuration file

Create a new alipay.php file in the config directory to configure the following

<?php
return [
    'pay' => [
        // APPID
        'app_id' => '************',
        // When Alipay pays successfully, it will notify post server's address.
        'notify_url' => 'http://192.168.0.110:9555/api/home/ali_pay_ntify',
        // After Alipay paid successfully, callback page get
        'return_url' => 'http://192.168.0.110:9528/#/pay_success',
        // The public key (note is Alipay's public key, not the merchant's public key).
        'ali_public_key' => '********',
        // Encryption method: * * RSA2 * * private key merchant application private key
        'private_key' => '********',
        'log' => [ // optional
            'file' => '../storage/logs/alipay.log',
            'level' => 'info', // It is recommended to adjust the production environment level to info and the development environment to debug
            'type' => 'single', // optional, daily
            'max_file' => 30, // optional, valid when the type is daily. The default is 30 days
        ],
        'http' => [
            'timeout' => 5.0,
            'connect_timeout' => 5.0,
            // Please refer to [guzzle] for more configuration items( https://guzzle-cn.readthedocs.io/zh_CN/latest/request-options.html )
        ],
        'mode' => 'dev', // optional, set this parameter to enter sandbox mode
    ]
];

Payment component

composer require yansongda/pay

BeautyQR PRO

composer require simplesoftwareio/simple-qrcode

controller

This example only briefly introduces the commonly used mobile website payment, app payment, code scanning payment and refund. Please refer to for more functions

Official documents

use Yansongda\Pay\Pay;
use App\Services\OrderService;
use SimpleSoftwareIO\QrCode\Facades\QrCode;
class AliPayController extends Controller
{
    // Mobile web payment interface
    public function aliPay(Request $request)
    {
        $aliPayOrder = [
            'out_trade_no' => time(),
            'total_amount' => $order->total_amount, // Payment amount
            'subject'      => $request->subject ?? 'Alipay mobile phone web payment' // remarks
        ];

        $config = config('alipay.pay');

        $config['return_url'] = $config['return_url'].'?id='.$request->id;

        $config['notify_url'] = $config['notify_url'].'?id='.$request->id;

        return Pay::alipay($config)->wap($aliPayOrder);
    }

    // app payment interface
    public function aliPayApp(Request $request)
    {
        $aliPayOrder = [
            'out_trade_no' => time(),
            'total_amount' => $order->total_amount, // Payment amount
            'subject'      => $request->subject ?? 'default' // remarks
        ];

        $config = config('alipay.pay');

        $config['return_url'] = $config['return_url'].'?id='.$request->id;

        return Pay::alipay($config)->app($aliPayOrder);
    }

    // Alipay sweep code payment
    public function aliPayScan(Request $request)
    {
        $aliPayOrder = [
            'out_trade_no' => time(),
            'total_amount' => $order->total_amount, // Payment amount
            'subject'      => $request->subject ?? 'Code scanning payment' // remarks
        ];

        $config = config('alipay.pay');

        $config['return_url'] = $config['return_url'].'?order_guid='.$request->order_guid;

        $scan = Pay::alipay($config)->scan($aliPayOrder);

        if(empty($scan->code) || $scan->code !== '10000') return false;

        $url = $scan->code.'?order_guid='.$request->order_guid;
        // Generate QR code
        return  QrCode::encoding('UTF-8')->size(300)->generate($url);

    }

    // Alipay service notifies this project server after successful payment.
    // post request
    // Here is just a brief description of the logic, and the specific security protection will be limited by yourself
    public function aliPayNtify(Request $request, OrderService $orderService)
    {
        $order = Order::find($request->id);
        // Update your project order status
        if(!empty($order))  $orderService->payOrder($order);
    }

    // Alipay refund
    public function aliPayRefund(Request $request)
    {
        try {
            $payOrder = [
                'out_trade_no' => $order->out_trade_no, // Merchant order number
                'refund_amount' => $order->total_amount, // The refund amount shall not exceed the total amount of the order
                 'out_request_no' => Common::getUuid() // Multiple refund marks for the same transaction (partial refund marks)
            ];

            $config = config('alipay.pay');

            // Return status code 10000 success
            $result = Pay::alipay($config)->refund($payOrder);
            if (empty($result->code) || $result->code !== '10000') throw new \Exception('Request Alipay refund interface failed');
            // Order changed to refunded status
            // ~~Order status modification logic of your own mall
        } catch (\Exception $exception) {
            \Log::error($exception->getMessage());
            return false;
        }
    }
}

Topics: Laravel