PHP code example of chenyongze / wechat-developer
1. Go to this page and download the library: Download chenyongze/wechat-developer 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/ */
chenyongze / wechat-developer example snippets
# 在项目中加载初始化文件
$config = [
'token' => 'test',
'appid' => 'wx60a43dd8161666d4',
'appsecret' => '71308e96a204296c57d7cd4b21b883e8',
'encodingaeskey' => 'BJIUzE0gqlWy0GxfPp4J1oPTBmOrNDIGPNav1YFH5Z5',
// 配置商户支付参数(可选,在使用支付功能时需要)
'mch_id' => "1235704602",
'mch_key' => 'IKI4kpHjU94ji3oqre5zYaQMwLHuZPmj',
// 配置商户支付双向证书目录(可选,在使用退款|打款|红包时需要)
'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' => 'o38gpszoJoC9oJYz3UHHf6bEp0Lo',
'trade_type' => 'JSAPI',
'notify_url' => 'http://a.com/text.html',
'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' => '2016090900468879',
// 支付宝公钥(1行填写)
'public_key' => 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtU71NY53U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sx93zY/LGzIHgHcQKBgQDoZlcs9RWxTdGDdtH8kk0J/r+QtMijNzWI0a+t+ZsWOyd3rw+uM/8O4JTNP4Y98TvvxhJXewITbfiuOIbW1mxh8bnO/fcz7+RXZKgPDeoTeNo717tZFZGBEyUdH9M9Inqvht7+hjVDIMCYBDomYebdk3Xqo4mDBjLRdVNGrhGmVQKBgQDKS/MgTMK8Ktfnu1KzwCbn/FfHTOrp1a1t1wWPv9AW0rJPYeaP6lOkgIoO/1odG9qDDhdB6njqM+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.thinkadmin.top/test/alipay-notify.php';
$config['return_url'] = 'http://pay.thinkadmin.top/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.thinkadmin.top/test/alipay-notify.php';
$config['return_url'] = 'http://pay.thinkadmin.top/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();
}