PHP code example of xiaowei007 / payment-v2
1. Go to this page and download the library: Download xiaowei007/payment-v2 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/ */
xiaowei007 / payment-v2 example snippets
// =====================================================
// 配置缓存处理函数(适配不同环境)
// -----------------------------------------------------
// - 数据缓存(set|get|del):可存储到本地或 Redis
// - 文件缓存(put):仅支持本地存储,并返回可读的文件路径
// - 若未设置自定义缓存处理,默认存储在 cache_path 目录
// =====================================================
// \WeChat\Contracts\Tools::$cache_callable = [
// 'set' => function ($name, $value, $expired = 360) {
// var_dump(func_get_args());
// return $value;
// },
// 'get' => function ($name) {
// var_dump(func_get_args());
// return $value;
// },
// 'del' => function ($name) {
// var_dump(func_get_args());
// return true;
// },
// 'put' => function ($name) {
// var_dump(func_get_args());
// return $filePath;
// },
// ];
return [
// 公众号 APPID(可选)
'appid' => 'wx3760xxxxxxxxxxxx',
// 微信商户号(必填)
'mch_id' => '15293xxxxxx',
// 微信商户 V3 接口密钥(必填)
'mch_v3_key' => '98b7fxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
// 商户证书序列号(可选):用于请求签名
'cert_serial' => '49055D67B2XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
// 微信商户证书公钥(必填):可填写证书内容或文件路径,仅用于提取序列号
'cert_public' => $certPublic,
// 微信商户证书私钥(必填):可填写证书内容或文件路径,用于请求数据签名
'cert_private' => $certPrivate,
// 自定义证书包:支持平台证书或支付公钥(可填写文件路径或证书内容)
'cert_package' => [
'PUB_KEY_ID_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' => $certPayment
],
// 微信平台证书或支付证书序列号(可选)
// 'mp_cert_serial' => 'PUB_KEY_ID_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
// 微信平台证书或支付证书内容(可选)
// 'mp_cert_content' => $certPayment,
// 运行时文件缓存路径(可选)
'cache_path' => ''
];
return [
// 服务商应用ID
'sp_appid' => 'wx3760xxxxxxxxxxxx',
// 服务商户号
'sp_mchid' => '15293xxxxxx',
// 子商户应用ID(可选)
'appid' => 'wx3760xxxxxxxxxxxx',
// 子商户号(可选)
'mch_id' => '15293xxxxxx',
// 微信商户 V3 接口密钥(必填)
'mch_v3_key' => '98b7fxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
// 商户证书序列号(可选):用于请求签名
'cert_serial' => '49055D67B2XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
// 微信商户证书公钥(必填):可填写证书内容或文件路径,仅用于提取序列号
'cert_public' => $certPublic,
// 微信商户证书私钥(必填):可填写证书内容或文件路径,用于请求数据签名
'cert_private' => $certPrivate,
// 自定义证书包:支持平台证书或支付公钥(可填写文件路径或证书内容)
'cert_package' => [
'PUB_KEY_ID_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' => $certPayment
],
// 微信平台证书或支付证书序列号(可选)
// 'mp_cert_serial' => 'PUB_KEY_ID_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
// 微信平台证书或支付证书内容(可选)
// 'mp_cert_content' => $certPayment,
// 运行时文件缓存路径(可选)
'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;
}
// 创建接口实例
$wechat = \We::WePayPartnerOrder($config);
// $wechat = new \WePayPartner\Order($config);
// 组装参数,可以参考官方商户文档
$options = [
'sp_appid' => $config['sp_appid'],
'sp_mchid' => $config['sp_mchid'],
'sub_appid' => $config['appid'],
'sub_mchid' => $config['mch_id'],
'description' => '测试商品',
'out_trade_no' => time(),
'notify_url' => 'http://a.com/text.html',
'amount' => [
'total' => 1,
'currency' => 'CNY'
],
'payer' => [
'sp_openid' => 'o38gpszoJoC9oJYz3UHHf6bEp0Lo'
]
];
try {
// 生成预支付码
$result = $wechat->create(\WePayPartner\Order::WXPAY_JSAPI, $options);
// @todo 把 $result 传到前端用js发起支付就可以了
} catch (Exception $e) {
// 出错啦,处理下吧
echo $e->getMessage() . PHP_EOL;
}
$config = [
// 沙箱模式
'debug' => true,
// 签名类型 ( RSA|RSA2 )
'sign_type' => 'RSA2',
// 应用ID
'appid' => '2021000122667306',
// 应用私钥内容 ( 需1行填写,特别注意:这里的应用私钥通常由支付宝密钥管理工具生成 )
'private_key' => 'MIIEowIBAAKCAQEAn...',
// 公钥模式,支付宝公钥内容 ( 需1行填写,特别注意:这里不是应用公钥而是支付宝公钥,通常是上传应用公钥换取支付宝公钥,在网页可以复制 )
'public_key' => '',
// 证书模式,应用公钥证书路径 ( 新版资金类接口转 app_cert_sn,如文件 appCertPublicKey.crt )
'app_cert_path' => __DIR__ . '/alipay/appPublicCert.crt', // 'app_cert' => '证书内容',
// 证书模式,支付宝根证书路径 ( 新版资金类接口转 alipay_root_cert_sn,如文件 alipayRootCert.crt )
'alipay_root_path' => __DIR__ . '/alipay/alipayRootCert.crt', // 'root_cert' => '证书内容',
// 证书模式,支付宝公钥证书路径 ( 未填写 public_key 时启用此参数,如文件 alipayPublicCert.crt )
'alipay_cert_path' => __DIR__ . '/alipay/alipayPublicCert.crt', // 'public_key' => '证书内容'
// 支付成功通知地址
'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();
}
try {
// 实例化芝麻先享对象
$zhima = We::AliPayZhimaCreditPePromiseOrder($config);
// $zhima = new \AliPay\ZhimaCreditPePromiseOrder($config);
// 创建芝麻先享订单
$result = $zhima->create([
'out_order_no' => time(), // 商户订单号
'product_code' => 'w1010100100000000000', // 产品码
'subject' => '测试商品', // 商品标题
'amount' => '0.01', // 订单总金额
'seller_id' => '2088102146222222', // 卖家支付宝用户ID
]);
var_export($result);
} catch (Exception $e) {
// 异常处理
echo $e->getMessage();
}
try {
// 实例化芝麻免押对象
$zhima = We::AliPayZhimaCreditEpSceneAgreement($config);
// $zhima = new \AliPay\ZhimaCreditEpSceneAgreement($config);
// 创建芝麻免押订单
$result = $zhima->create([
'credit_order_no' => time(), // 信用订单号
'product_code' => 'w1010100100000000000', // 产品码
'subject' => '测试免押商品', // 商品标题
'amount' => '0.01', // 订单总金额
'seller_id' => '2088102146222222', // 卖家支付宝用户ID
]);
var_export($result);
} catch (Exception $e) {
// 异常处理
echo $e->getMessage();
}
try {
// 实例化微信支付分对象
$payscore = We::WePayV3PayScore($config);
// $payscore = new \WePayV3\PayScore($config);
// 创建支付分订单
$result = $payscore->create([
'out_order_no' => time(), // 商户订单号
'service_id' => 'your_service_id', // 服务ID
'service_introduction' => '测试服务', // 服务介绍
'risk_amount' => 10000, // 风险金额,单位为分
'time_range' => [
'start_time' => date('Y-m-d H:i:s'),
'end_time' => date('Y-m-d H:i:s', strtotime('+1 hour')),
],
'notify_url' => 'https://yourdomain.com/notify',
]);
var_export($result);
} catch (Exception $e) {
// 异常处理
echo $e->getMessage();
}
try {
// 实例化微信支付分停车服务对象
$parking = We::WePayV3Parking($config);
// $parking = new \WePayV3\Parking($config);
// 创建停车入场
$result = $parking->create([
'out_parking_no' => time(), // 商户停车入场号
'plate_number' => '粤B12345', // 车牌号
'plate_color' => 'BLUE', // 车牌颜色
'start_time' => date('Y-m-d H:i:s'), // 入场时间
'parking_name' => '测试停车场', // 停车场名称
'free_duration' => 30, // 免费时长,单位分钟
'notify_url' => 'https://yourdomain.com/notify',
]);
var_export($result);
} catch (Exception $e) {
// 异常处理
echo $e->getMessage();
}
try {
// 实例化微信服务商商户开户意愿确认对象
$subject = We::WePayPartnerSubject($config);
// $subject = new \WePayPartner\Subject($config);
// 提交商户开户意愿申请单
$result = $subject->apply([
'business_code' => 'business_' . time(), // 业务申请编号
'contact_info' => [
'contact_type' => 'LEGAL_PERSON', // 联系人类型
'contact_name' => '张三', // 联系人姓名
'contact_id_card_number' => '11010119900307XXXX', // 联系人身份证号码
'mobile' => '13800138000', // 联系人手机号
'email' => '[email protected] ' // 联系人邮箱
],
'subject_info' => [
'subject_type' => 'SUBJECT_TYPE_INDIVIDUAL', // 主体类型
'business_licence_info' => [
'licence_number' => '9144030076543210XX', // 营业执照编号
'merchant_name' => '深圳市某某科技有限公司', // 商户名称
'company_address' => '深圳市南山区某某街道某某号', // 公司地址
'legal_person' => '张三' // 法人姓名
]
],
'notify_url' => 'https://yourdomain.com/notify', // 通知地址
]);
var_export($result);
// 撤销商户开户意愿申请单(通过申请单号)
$subject->cancelApplyment(['applyment_id' => $result['applyment_id']]);
// 或者通过业务申请编号撤销
// $subject->cancelApplyment(['business_code' => 'your_business_code']);
} catch (Exception $e) {
// 异常处理
echo $e->getMessage();
}
try {
// 实例化微信服务商商户平台处置通知对象
$violation = We::WePayPartnerViolation($config);
// $violation = new \WePayPartner\Violation($config);
// 创建商户违规通知回调地址
$result = $violation->createCallback([
'notify_url' => 'https://yourdomain.com/violation-notify' // 回调地址
]);
var_export($result);
// 查询商户违规通知回调地址
$violation->getCallback();
// 更新商户违规通知回调地址
$violation->updateCallback([
'notify_url' => 'https://yourdomain.com/violation-notify-update'
]);
// 查询商户违规记录
$violation->getViolations('sub_mchid', [
'limit' => 10,
'offset' => 0
]);
// 删除商户违规通知回调地址
$violation->deleteCallback();
} catch (Exception $e) {
// 异常处理
echo $e->getMessage();
}
try {
// 实例化微信服务商消费者投诉对象
$complaint = \We::WePayPartnerComplaint($config);
// $complaint = new \WePayPartner\Complaint($config);
// 查询投诉单列表
$complaint->list([
'limit' => 5,
'offset' => 0,
'begin_date' => '2023-01-01',
'end_date' => '2023-01-31'
]);
// 创建投诉通知回调地址
$complaint->createCallback([
'url' => 'https://yourdomain.com/complaint-notify'
]);
// 查询投诉通知回调地址
$complaint->getCallback();
// 更新投诉通知回调地址
$complaint->updateCallback([
'url' => 'https://yourdomain.com/complaint-notify-update'
]);
// 删除投诉通知回调地址
$complaint->deleteCallback();
} catch (Exception $e) {
// 异常处理
echo $e->getMessage();
}
try {
// 实例化微信服务商下载账单对象
$bill = \We::WePayPartnerBill($config);
// $bill = new \WePayPartner\Bill($config);
// 申请交易账单
$tradeBill = $bill->tradeBill('2023-01-01', [
'sub_mchid' => '1600000000', // 可选,指定子商户号
'bill_type' => 'ALL', // 可选,账单类型 ALL|SUCCESS|REFUND
'tar_type' => 'GZIP' // 可选,压缩类型 GZIP
]);
// 申请资金账单
$fundFlowBill = $bill->fundFlowBill('2023-01-01', [
'account_type' => 'BASIC', // 可选,资金账户类型 BASIC|OPERATION|FEES
'tar_type' => 'GZIP' // 可选,压缩类型 GZIP
]);
// 下载账单文件(如果获取到了下载链接)
if (isset($tradeBill['download_url'])) {
$bill->download($tradeBill['download_url'], './trade_bill.csv', [
'tar_type' => 'GZIP' // 如果是压缩文件需要指定
]);
}
} catch (Exception $e) {
// 异常处理
echo $e->getMessage();
}