PHP uses yansongda/pay to pay Alipay and WeChat

Keywords: Programming PHP Laravel github Mobile

Pay

This document is v2.x version, if you want to find v1.x version document, Click https://github.com/yansongda/pay/tree/v1.x

Note: v1.x is incompatible with v2.x version

After developing Alipay and WeChat Payments several times, it is natural to have a feeling of disgust and inertia. I want to find the wheel related on the internet, but I have not found one that I feel satisfied with, either it is too difficult to understand, or the file structure is too messy, only I start sleeving dry.

!!Please familiarize yourself with Alipay/WeChat documentation first!!Please have basic debug ability!!

Welcome Star, Welcome PR!

laravel Extension Package Please Send here

QQ Group: 690027516

Characteristic

  • Rich event systems
  • Naming isn't that messy
  • Hide details developers don't need to focus on
  • Developed from the latest API of Alipay and WeChat
  • Highly abstract class to avoid the pain of spelling json and xml
  • PSR compliant, you can easily integrate with your framework
  • The file structure is clear and easy to understand and you can add payment gateways that are not included in this project
  • The method is more elegant and doesn't have to study what strange method or class names do.

Running Environment

  • PHP 7.0+
  • composer

php5 Please use v1.x Edition https://github.com/yansongda/pay/tree/v1.x

Supported payment methods

1. Alipay

  • Computer Payment
  • Mobile Website Payment
  • APP Payment
  • Brush Card Payment
  • Sweep Payment
  • Account Transfer
  • Applet Payment
method describe
web Computer Payment
wap Mobile Website Payment
app APP Payment
pos Brush Card Payment
scan Sweep Payment
transfer Account Transfer
mini Applet Payment

2. WeChat

  • Public Number Payment
  • Applet Payment
  • H5 Payment
  • Sweep Payment
  • Brush Card Payment
  • APP Payment
  • Business Payment
  • Ordinary red envelope
  • Split red envelope
method describe
mp Public Number Payment
miniapp Applet Payment
wap H5 Payment
scan Sweep Payment
pos Brush Card Payment
app APP Payment
transfer Business Payment
redpack Ordinary red envelope
groupRedpack Split red envelope

Supported methods

All gateways support the following methods

  • find(array/string $order)
    Description: Find Order Interface
    Parameters: $order is string type, please pass in the system order number corresponding to out_trade_no in Alipay or WeChat; parameter refer to official documents of Alipay or WeChat when array type.
    Return: The query succeeded, returning the instance of Yansongda\Supports\Collection, which can access the data returned by the server through $colletion->xxxx or $collection['xxx'].
    Exception: GatewayException or InvalidSignException

  • refund(array $order)
    Description: Refund interface
    Parameters: $order array format, refund parameter.
    Return: The refund is successful, return the instance of Yansongda\Supports\Collection, and access the data returned by the server through $colletion->xxx or $collection['xxx'].
    Exception: GatewayException or InvalidSignException

  • cancel(array/string $order)
    Description: Cancel order interface
    Parameters: $order is string type, please pass in the system order number corresponding to out_trade_no in Alipay or WeChat; parameter refer to official documents of Alipay or WeChat when array type.
    Return: Cancel succeeded, return the instance of Yansongda\Supports\Collection, and access the data returned by the server through $colletion->xxxx or $collection['xxx'].
    Exception: GatewayException or InvalidSignException

  • close(array/string $order)
    Description: Close order interface
    Parameters: $order is string type, please pass in the system order number corresponding to out_trade_no in Alipay or WeChat; parameter refer to official documents of Alipay or WeChat when array type.
    Return: Close successfully, return the Yansongda\Supports\Collection instance, and access the data returned by the server through $colletion->xxx or $collection['xxx'].
    Exception: GatewayException or InvalidSignException

  • verify()
    Description: Verify that the server returns a message legally
    Return: Validation succeeds, returns the instance of Yansongda\Supports\Collection, and accesses the data returned by the server through $colletion->xxxx or $collection['xxx'].
    Exception: GatewayException or InvalidSignException

  • PAYMETHOD(array $order)
    Description: Make payment; refer to the column "Supported payment methods" for the specific payment method name
    Return: Successfully, return the Yansongda\Supports\Collection instance, access the data returned by the server or the SymfonyComponent\HttpFoundation\Response instance through $colletion->xxx or $collection['xxx'], return through return $response->send () (return $response directly in the laravel framework), refer to the documentation.
    Exception: GatewayException or InvalidSignException

install

composer require yansongda/pay -vvv

Instructions

Alipay

<?php

namespace App\Http\Controllers;

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

class PayController
{
    protected $config = [
        'app_id' => '2016082000295641',
        'notify_url' => 'http://yansongda.cn/notify.php',
        'return_url' => 'http://yansongda.cn/return.php',
        'ali_public_key' => 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuWJKrQ6SWvS6niI+4vEVZiYfjkCfLQfoFI2nCp9ZLDS42QtiL4Ccyx8scgc3nhVwmVRte8f57TFvGhvJD0upT4O5O/lRxmTjechXAorirVdAODpOu0mFfQV9y/T9o9hHnU+VmO5spoVb3umqpq6D/Pt8p25Yk852/w01VTIczrXC4QlrbOEe3sr1E9auoC7rgYjjCO6lZUIDjX/oBmNXZxhRDrYx4Yf5X7y8FRBFvygIE2FgxV4Yw+SL3QAa2m5MLcbusJpxOml9YVQfP8iSurx41PvvXUMo49JG3BDVernaCYXQCoUJv9fJwbnfZd7J5YByC+5KM4sblJTq7bXZWQIDAQAB',
        // Encryption: **RSA2**  
        'private_key' => 'MIIEpAIBAAKCAQEAs6+F2leOgOrvj9jTeDhb5q46GewOjqLBlGSs/bVL4Z3fMr3p+Q1Tux/6uogeVi/eHd84xvQdfpZ87A1SfoWnEGH5z15yorccxSOwWUI+q8gz51IWqjgZxhWKe31BxNZ+prnQpyeMBtE25fXp5nQZ/pftgePyUUvUZRcAUisswntobDQKbwx28VCXw5XB2A+lvYEvxmMv/QexYjwKK4M54j435TuC3UctZbnuynSPpOmCu45ZhEYXd4YMsGMdZE5/077ZU1aU7wx/gk07PiHImEOCDkzqsFo0Buc/knGcdOiUDvm2hn2y1XvwjyFOThsqCsQYi4JmwZdRa8kvOf57nwIDAQABAoIBAQCw5QCqln4VTrTvcW+msB1ReX57nJgsNfDLbV2dG8mLYQemBa9833DqDK6iynTLNq69y88ylose33o2TVtEccGp8Dqluv6yUAED14G6LexS43KtrXPgugAtsXE253ZDGUNwUggnN1i0MW2RcMqHdQ9ORDWvJUCeZj/AEafgPN8AyiLrZeL07jJz/uaRfAuNqkImCVIarKUX3HBCjl9TpuoMjcMhz/MsOmQ0agtCatO1eoH1sqv5Odvxb1i59c8Hvq/mGEXyRuoiDo05SE6IyXYXr84/Nf2xvVNHNQA6kTckj8shSi+HGM4mO1Y4Pbb7XcnxNkT0Inn6oJMSiy56P+CpAoGBAO1O+5FE1ZuVGuLb48cY+0lHCD+nhSBd66B5FrxgPYCkFOQWR7pWyfNDBlmO3SSooQ8TQXA25blrkDxzOAEGX57EPiipXr/hy5e+WNoukpy09rsO1TMsvC+v0FXLvZ+TIAkqfnYBgaT56ku7yZ8aFGMwdCPL7WJYAwUIcZX8wZ3dAoGBAMHWplAqhe4bfkGOEEpfs6VvEQxCqYMYVyR65K0rI1LiDZn6Ij8fdVtwMjGKFSZZTspmsqnbbuCE/VTyDzF4NpAxdm3cBtZACv1Lpu2Om+aTzhK2PI6WTDVTKAJBYegXaahBCqVbSxieR62IWtmOMjggTtAKWZ1P5LQcRwdkaB2rAoGAWnAPT318Kp7YcDx8whOzMGnxqtCc24jvk2iSUZgb2Dqv+3zCOTF6JUsV0Guxu5bISoZ8GdfSFKf5gBAo97sGFeuUBMsHYPkcLehM1FmLZk1Q+ljcx3P1A/ds3kWXLolTXCrlpvNMBSN5NwOKAyhdPK/qkvnUrfX8sJ5XK2H4J8ECgYAGIZ0HIiE0Y+g9eJnpUFelXvsCEUW9YNK4065SD/BBGedmPHRC3OLgbo8X5A9BNEf6vP7fwpIiRfKhcjqqzOuk6fueA/yvYD04v+Da2MzzoS8+hkcqF3T3pta4I4tORRdRfCUzD80zTSZlRc/h286Y2eTETd+By1onnFFe2X01mwKBgQDaxo4PBcLL2OyVT5DoXiIdTCJ8KNZL9+kV1aiBuOWxnRgkDjPngslzNa1bK+klGgJNYDbQqohKNn1HeFX3mYNfCUpuSnD2Yag53Dd/1DLO+NxzwvTu4D6DCUnMMMBVaF42ig31Bs0jI3JQZVqeeFzSET8fkoFopJf3G6UXlrIEAQ==',
        'log' => [ // optional
            'file' => './logs/alipay.log',
            'level' => 'info', // It is recommended that the production environment level be adjusted to info and the development environment to debug.
            'type' => 'single', // Optional, optional daily.
            'max_file' => 30, // optional, valid when type is daily, default 30 days
        ],
        'http' => [ // optional
            'timeout' => 5.0,
            'connect_timeout' => 5.0,
            // Refer to [Guzzle](https://guzzle-cn.readthedocs.io/zh_CN/latest/request-options.html) for more configuration items
        ],
        'mode' => 'dev', // optional, setting this parameter will enter sandbox mode
    ];

    public function index()
    {
        $order = [
            'out_trade_no' => time(),
            'total_amount' => '1',
            'subject' => 'test subject - test',
        ];

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

        return $alipay->send();// In the laravel framework, please directly `return $alipay`
    }

    public function return()
    {
        $data = Pay::alipay($this->config)->verify(); // Yes, it's that simple!

        // Order Number: $data->out_trade_no
        // Alipay Trading Number: $data->trade_no
        // Total Order Amount: $data->total_amount
    }

    public function notify()
    {
        $alipay = Pay::alipay($this->config);
    
        try{
            $data = $alipay->verify(); // Yes, it's that simple!

            // Please make your own judgment on trade_status and other logic. In Alipay's business notification, Alipay will only be deemed successful if the transaction notification status is TRADE_SUCCESS or TRADE_FINISHED.
            // 1. The merchant needs to verify whether the out_trade_no in the notification data is the order number created in the merchant system;
            // 2. Determine whether total_amount is really the actual amount of the order (that is, the amount at the time the merchant order was created);
            // 3. Check whether the seller_id (or seller_email) in the notification is the corresponding operation of the out_trade_no document (sometimes a merchant may have more than one seller_id/seller_email);
            // 4. Verify that app_id is the merchant itself.
            // 5. Other business logic

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

        return $alipay->success()->send();// In the laravel framework, please directly `return $alipay->success()`
    }
}

WeChat

<?php

namespace App\Http\Controllers;

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

class PayController
{
    protected $config = [
        'appid' => 'wxb3fxxxxxxxxxxx', // APP APPID
        'app_id' => 'wxb3fxxxxxxxxxxx', // Public Number APPID
        'miniapp_id' => 'wxb3fxxxxxxxxxxx', // Applet APPID
        'mch_id' => '14577xxxx',
        'key' => 'mF2suE9sU6Mk1Cxxxxxxxxxxx',
        'notify_url' => 'http://yanda.net.cn/notify.php',
        'cert_client' => './cert/apiclient_cert.pem', // optional, used for refunds, etc.
        'cert_key' => './cert/apiclient_key.pem',// optional, used for refunds, etc.
        'log' => [ // optional
            'file' => './logs/wechat.log',
            'level' => 'info', // It is recommended that the production environment level be adjusted to info and the development environment to debug.
            'type' => 'single', // Optional, optional daily.
            'max_file' => 30, // optional, valid when type is daily, default 30 days
        ],
        'http' => [ // optional
            'timeout' => 5.0,
            'connect_timeout' => 5.0,
            // Refer to [Guzzle](https://guzzle-cn.readthedocs.io/zh_CN/latest/request-options.html) for more configuration items
        ],
        'mode' => 'dev', // optional, dev/hk; when `hk', it is the Hong Kong gateway.
    ];

    public function index()
    {
        $order = [
            'out_trade_no' => time(),
            'total_fee' => '1', // **Units: Minutes**
            'body' => 'test body - test',
            'openid' => 'onkVf1FjWS5SBIixxxxxxx',
        ];

        $pay = Pay::wechat($this->config)->mp($order);

        // $pay->appId
        // $pay->timeStamp
        // $pay->nonceStr
        // $pay->package
        // $pay->signType
    }

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

        try{
            $data = $pay->verify(); // Yes, it's that simple!

            Log::debug('Wechat notify', $data->all());
        } catch (\Exception $e) {
            // $e->getMessage();
        }
        
        return $pay->success()->send();// In the laravel framework, please directly `return $pay->success()`
    }
}

Event System

See detailed documentation

Detailed Documentation

Detailed documentation

error

If an error occurs when calling the associated Payment Gateway API, a GatewayException is thrown, an InvalidSignException error can be viewed through $e->getMessage(), and the original data returned after calling the API can also be viewed through $e->ray, which is in array format.

All Exceptions

  • YansongdaPayExceptionsInvalidGatewayException indicates that a payment gateway other than this SDK is used.
  • YansongdaPayExceptionsInvalidSignException, indicating that the check-out failed.
  • Yansongda\Pay\Exceptions\InvalidConfigException indicates a lack of configuration parameters, such as ali_public_key, private_key, etc.
  • YansongdaPayExceptionsGatewayException indicates that the data returned by the Alipay/WeChat server is not normal, such as incorrect parameters, non-existent statement, etc.

Code Contribution

Due to the limitations of testing and usage environment, only related payment gateways for Alipay and WeChat Payment have been developed in this project.

If you have other requirements for payment gateways or find code that needs improvement in this project, welcome Fork and submit PR!

LICENSE

MIT

Buy me a cup of coffee :)

Posted by MarcB on Wed, 18 Dec 2019 19:59:07 -0800