Backend Development 6 min read

Single-File PHP Implementation for Alipay and WeChat Payments

This article presents a concise, single‑file PHP solution for integrating Alipay and WeChat payment services, detailing required environment settings, configuration steps, essential code snippets, and browser‑specific handling to help developers quickly add payment functionality to their web applications.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Single-File PHP Implementation for Alipay and WeChat Payments

Many online tutorials for PHP Alipay integration are overly complex and require numerous files; this guide consolidates the process into a single PHP file that also supports WeChat payments, with links to the GitHub repositories dedemao/alipay and dedemao/weixinPay .

To ensure compatibility with the WeChat browser, the solution depends on two additional files provided by Alipay’s official demo (alipay_in_weixin_demo): ap.js and pay.htm .

Environment requirements include PHP 5.0 or higher with CURL and SSL extensions enabled.

Key configuration items at the top of the file must be filled in: the App ID, return and notify URLs, a unique order number, payment amount, order title, and the merchant private key (RSA2 or RSA). The private key should be generated according to Alipay’s documentation ( link 1 , link 2 ).

The core PHP code defines an AlipayService class with methods to construct request parameters, generate signatures, and handle character‑set conversion. Important methods include:

<code>&lt;?php
header('Content-type:text/html; Charset=utf-8');
$appid = 'xxxxx';
$returnUrl = 'http://www.xxx.com/alipay/return.php';
$notifyUrl = 'http://www.xxx.com/alipay/notify.php';
$outTradeNo = uniqid();
$payAmount = 0.01;
$orderName = '支付测试';
$signType = 'RSA2';
$saPrivateKey = 'MIIEpAIBAAKCAQEA...';
$aliPay = new AlipayService($appid,$returnUrl,$notifyUrl,$saPrivateKey);
$payConfigs = $aliPay->doPay($payAmount,$outTradeNo,$orderName,$returnUrl,$notifyUrl);
// class definition follows with doPay, generateSign, sign, checkEmpty, getSignContent, characet
?&gt;</code>

After creating the payment configuration, the script builds a query string and checks whether the request originates from the WeChat browser using the isWeixin() function. If true, it loads ap.js and invokes _AP.pay(gotoUrl) ; otherwise, it redirects the user directly to Alipay’s gateway URL.

Additional documentation links are provided for reference, and the article concludes with a reminder to ensure the configuration header information is complete and the merchant private key matches the selected signature algorithm.

Payment IntegrationAlipayphp-sdkwechatpay
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.