Mastering yansongda/pay: A Complete PHP Guide to Multi‑Platform Payments
This article provides a comprehensive overview of the yansongda/pay PHP SDK, detailing its key features, supported payment channels (Alipay, WeChat, Douyin, UnionPay, Jiangsu Bank), installation via Composer, configuration files, and step‑by‑step code examples for initializing the SDK and handling web, asynchronous, and synchronous payment flows within a Webman application.
Overview
yansongda/pay is a PHP SDK that offers an elegant, extensible solution for processing payments through Alipay, WeChat, Douyin, UnionPay, and Jiangsu Bank. It is designed for PHP environments and follows modern standards.
Key Features
Multi‑tenant support
Swoole compatibility
Flexible plugin mechanism
Rich event system
Clean naming conventions
Abstracted handling of JSON and XML
Clear file structure for easy extension
Elegant method names
Automatic retrieval of WeChat public certificates
PSR‑2, PSR‑3, PSR‑4, PSR‑7, PSR‑11, PSR‑14, PSR‑18 compliance
Supported Payment Methods
The SDK fully supports all functions of Alipay, WeChat, UnionPay, and Jiangsu Bank (e‑Rong Pay) through a plugin system.
Alipay
Computer web payment
Mobile web payment
APP payment
Card swipe payment
QR code payment
Account transfer
Mini‑program payment
Official account payment
Mini‑program payment
H5 payment
QR code payment
APP payment
Card swipe payment
Douyin
Mini‑program payment
UnionPay
Mobile web payment
Computer web payment
Card swipe payment
QR code payment
Jiangsu Bank (e‑Rong Pay)
Aggregated QR code payment (WeChat, Alipay, UnionPay, e‑Rong)
Installation
composer require yansongda/pay:~3.7.0 -vvvUsing the SDK in Webman
Configuration File (config/payment.php)
<?php
/**
* @desc Payment configuration file
*/
return [
'alipay' => [
'default' => [
// Required fields
'app_id' => '20160909004708941',
'app_secret_cert' => 'MIIEpAIBAAKCxxxxxxxxxxxxxxP4r3m4OUmD/+XDgCg=',
'app_public_cert_path' => base_path().'/payment/appCertPublicKey_2016090900470841.crt',
'alipay_public_cert_path' => base_path().'/payment/alipayCertPublicKey_RSA2.crt',
'alipay_root_cert_path' => base_path().'/payment/alipayRootCert.crt',
// Optional URLs
'return_url' => 'https://webman.tinywan.cn/payment/alipay-return',
'notify_url' => 'https://webman.tinywan.cn/payment/alipay-notify',
// Mode selection
'mode' => \Yansongda\Pay\Pay::MODE_SANDBOX,
],
],
'wechat' => [
'default' => [
'mch_id' => '',
'mch_secret_key' => '',
// ... other optional fields omitted for brevity
'mode' => \Yansongda\Pay\Pay::MODE_SANDBOX,
],
],
'logger' => [
'enable' => false,
'file' => runtime_path().'/logs/alipay.log',
'level' => 'debug',
'type' => 'single',
'max_file' => 30,
],
'http' => [
'timeout' => 5.0,
'connect_timeout' => 5.0,
],
'_force' => true,
];Initialization
// Load configuration
$config = config('payment');
Pay::config($config);Web Payment (Alipay example)
use support\Request;
use Yansongda\Pay\Pay;
public function payment(Request $request): string
{
// 1. Initialize configuration
Pay::config(config('payment'));
// 2. Prepare order data
$order = [
'out_trade_no' => time(),
'total_amount' => '8888.88',
'subject' => 'webman payment',
'_method' => 'get', // redirect via GET
];
return Pay::alipay()->web($order)->getBody()->getContents();
}Asynchronous Callback (Alipay)
use support\Request;
use support\Response;
use Yansongda\Pay\Pay;
public function alipayNotify(Request $request): Response
{
// 1. Initialize configuration
Pay::config(config('payment'));
// 2. Process callback
$result = Pay::alipay()->callback($request->post());
// Business logic: verify trade_status, amount, seller_id, app_id, etc.
return new Response(200, [], 'success');
}Synchronous Callback (Alipay)
use support\Request;
use Yansongda\Pay\Pay;
public function alipayReturn(Request $request): string
{
Log::info('Alipay sync callback: '.json_encode($request->get()));
return 'success';
}Basic Usage Examples
Alipay Controller
<?php
namespace App\Http\Controllers;
use Yansongda\Pay\Pay;
class AlipayController
{
protected $config = [/* configuration array omitted for brevity */];
public function web()
{
Pay::config($this->config);
$result = Pay::alipay()->web([
'out_trade_no' => time(),
'total_amount' => '0.01',
'subject' => 'yansongda test - 1',
]);
return $result;
}
public function returnCallback()
{
Pay::config($this->config);
$data = Pay::alipay()->callback();
// $data->out_trade_no, $data->trade_no, $data->total_amount
}
public function notifyCallback()
{
Pay::config($this->config);
try {
$data = Pay::alipay()->callback();
// Verify trade_status, amount, etc.
} catch (\Throwable $e) {
dd($e);
}
return Pay::alipay()->success();
}
}WeChat Controller
<?php
namespace App\Http\Controllers;
use Yansongda\Pay\Pay;
class WechatController
{
protected $config = [/* configuration array omitted for brevity */];
public function index()
{
Pay::config($this->config);
$order = [
'out_trade_no' => time().'',
'description' => 'subject-test',
'amount' => ['total' => 1],
'payer' => ['openid' => 'onkVf1FjWS5SBxxxxxxxx'],
];
$pay = Pay::wechat()->mp($order);
// $pay contains appId, timeStamp, nonceStr, package, signType
}
public function callback()
{
Pay::config($this->config);
try {
$data = Pay::wechat()->callback();
} catch (\Throwable $e) {
dd($e);
}
return Pay::wechat()->success();
}
}Douyin Controller
<?php
namespace App\Http\Controllers;
use Yansongda\Pay\Pay;
class DouyinController
{
protected $config = [/* configuration array omitted for brevity */];
public function pay()
{
Pay::config($this->config);
$result = Pay::douyin()->mini([
'out_order_no' => date('YmdHis').mt_rand(1000,9999),
'total_amount' => 1,
'subject' => 'yansongda test - subject - 01',
'body' => 'yansongda test - body - 01',
'valid_time' => 600,
'expand_order_info' => json_encode([
'original_delivery_fee' => 15,
'actual_delivery_fee' => 10,
]),
]);
return $result;
}
public function callback()
{
Pay::config($this->config);
try {
$data = Pay::douyin()->callback();
} catch (\Throwable $e) {
dd($e);
}
return Pay::douyin()->success();
}
}Jiangsu Bank (e‑Rong Pay) Controller
<?php
namespace App\Http\Controllers;
use Yansongda\Pay\Pay;
class JsbController
{
protected $config = [/* configuration array omitted for brevity */];
public function index()
{
Pay::config($this->config);
$order = [
'outTradeNo' => time().'',
'proInfo' => 'subject-test',
'totalFee' => 1,
];
$pay = Pay::jsb()->scan($order);
}
public function notifyCallback()
{
Pay::config($this->config);
try {
$data = Pay::jsb()->callback();
} catch (\Throwable $e) {
dd($e);
}
return Pay::jsb()->success();
}
}Directory Structure for Certificates
├── payment
│ ├── alipayCertPublicKey_RSA2.crt
│ ├── alipayRootCert.crt
│ └── appCertPublicKey_2016090900470841.crtSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
