PHP code example of emmtltd / wechat-developer-client
1. Go to this page and download the library: Download emmtltd/wechat-developer-client library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
emmtltd / wechat-developer-client example snippets
# 在项目中加载初始化文件
$config = [
'token' => '',
'appid' => '',
'appsecret' => '',
'encodingaeskey' => '',
// 配置商户支付参数(可选,在使用支付功能时需要)
'mch_id' => "",
'mch_key' => '',
// 配置商户支付双向证书目录(可选,在使用退款|打款|红包时需要)
'ssl_key' => '',
'ssl_cer' => '',
// 缓存目录配置(可选,需拥有读写权限)
'cache_path' => '',
];
try {
// 实例对应的接口对象
$user = new \WeChat\User($config);
// 调用接口对象方法
$list = $user->getUserList();
// 处理返回的结果
echo '<pre>';
var_export($list);
} catch (Exception $e) {
// 出错啦,处理下吧
echo $e->getMessage() . PHP_EOL;
}
// 创建接口实例
$wechat = new \WeChat\Pay($config);
// 组装参数,可以参考官方商户文档
$options = [
'body' => '',
'out_trade_no' => time(),
'total_fee' => '1',
'openid' => '',
'trade_type' => 'JSAPI',
'notify_url' => '',
'spbill_create_ip' => '127.0.0.1',
];
try {
// 生成预支付码
$result = $wechat->createOrder($options);
// 创建JSAPI参数签名
$options = $wechat->createParamsForJsApi($result['prepay_id']);
// @todo 把 $options 传到前端用js发起支付就可以了
} catch (Exception $e) {
// 出错啦,处理下吧
echo $e->getMessage() . PHP_EOL;
}
$config = [
// 沙箱模式
'debug' => true,
// 应用ID
'appid' => '12345',
// 支付宝公钥(1行填写)
'public_key' => '+taTF6KthIHJmGgdio9bkqeJGhHk6ttkTKkLqFgwIfgAkHpdKiOv1uZw6gVGZ7TCu5LfHTqKrCd6Uz+N7hxhY+/cmDcrY6K1bSxnGmfRxFSb8lRfhe0V0UM6pKq2SGGSeovrKHN0OLp+Nn5wcULVnFgATXGCENshRlp96piPEBFwneXs19n+sX1jx60FTR7/rME3sW3AHug0fhZ9mSqW4x401WjdnwIDAQAB',
// 支付宝私钥(1行填写)
'private_key' => 'MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC3pbN7esinxgjE8uxXAsccgGNKIq+PR1LteNTFOy0fsete43ObQCrzd9DO0zaUeBUzpIOnxrKxez7QoZROZMYrinttFZ/V5rbObEM9E5AR5Tv/Fr4IByDhdB6njqM+mKY5Yr3N94PHamHbwJUCmbkqEunCWpGzgcQZ1Q254xk9D7UKq/XUqW2WDqDq80GQeNial+fBc46yelQzokwdA+JdIFKoyinQKBgQCBems9V/rTAtkk1nFdt6EGXZEbLS3PiXXhGXo4gqV+OEzf6H/i/YMwJb2hsK+5GQrcps0XQihA7PctEb9GOMa/tu5fva0ZmaDtc94SLR1p5d4okyQFGPgtIp594HpPSEN0Qb9BrUJFeRz0VP6U3dzDPGHo7V4yyqRLgIN6EIcy1QKBgAqdh6mHPaTAHspDMyjJiYEc5cJIj/8rPkmIQft0FkhMUB0IRyAALNlyAUyeK61hW8sKvz+vPR8VEEk5xpSQp41YpuU6pDZc5YILZLfca8F+8yfQbZ/jll6Foi694efezl4yE/rUQG9cbOAJfEJt4o4TEOaEK5XoMbRBKc8pl22lAoGARTq0qOr9SStihRAy9a+8wi2WEwL4QHcmOjH7iAuJxy5b5TRDSjlk6h+0dnTItiFlTXdfpO8KhWA8EoSJVBZ1kcACQDFgMIA+VM+yXydtzMotOn21W4stfZ4I6dHFiujMsnKpNYVpQh3oCrJf4SeXiQDdiSCodqb1HlKkEc6naHQ=',
// 支付成功通知地址
'notify_url' => '', // 可以应用的时候配置哦
// 网页支付回跳地址
'return_url' => '', // 可以应用的时候配置哦
];
// 参考公共参数 https://docs.open.alipay.com/203/107090/
$config['notify_url'] = 'http://pay.emmetled.com/test/alipay-notify.php';
$config['return_url'] = 'http://pay.emmetled.com/test/alipay-success.php';
try {
// 实例支付对象
$pay = We::AliPayWeb($config);
// $pay = new \AliPay\Web($config);
// 参考链接:https://docs.open.alipay.com/api_1/alipay.trade.page.pay
$result = $pay->apply([
'out_trade_no' => time(), // 商户订单号
'total_amount' => '1', // 支付金额
'subject' => '支付订单描述', // 支付订单描述
]);
echo $result; // 直接输出HTML(提交表单跳转)
} catch (Exception $e) {
// 异常处理
echo $e->getMessage();
}
// 参考公共参数 https://docs.open.alipay.com/203/107090/
$config['notify_url'] = 'http://pay.emmetled.com/test/alipay-notify.php';
$config['return_url'] = 'http://pay.emmetled.com/test/alipay-success.php';
try {
// 实例支付对象
$pay = We::AliPayWap($config);
// $pay = new \AliPay\Wap($config);
// 参考链接:https://docs.open.alipay.com/api_1/alipay.trade.wap.pay
$result = $pay->apply([
'out_trade_no' => time(), // 商户订单号
'total_amount' => '1', // 支付金额
'subject' => '支付订单描述', // 支付订单描述
]);
echo $result; // 直接输出HTML(提交表单跳转)
} catch (Exception $e) {
// 异常处理
echo $e->getMessage();
}