PHP code example of xhat / payjs
1. Go to this page and download the library: Download xhat/payjs 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/ */
xhat / payjs example snippets
hat\Payjs\Payjs;
// 配置通信参数
$config = [
'mchid' => '12323412323', // 配置商户号
'key' => 'sadfsaddsaf', // 配置通信密钥
];
// 初始化
$payjs = new Payjs($config);
// 构造订单基础信息
$data = [
'body' => '订单测试', // 订单标题
'total_fee' => 2, // 订单金额
'out_trade_no' => time(), // 订单号
'attach' => 'test_order_attach', // 订单附加信息(可选参数)
'notify_url' => 'https://www.baidu.com', // 异步通知地址(可选参数)
];
$result = $payjs->native($data);
print_r($result);
// 构造订单基础信息
$data = [
'body' => '订单测试', // 订单标题
'total_fee' => 2, // 订单金额
'out_trade_no' => time(), // 订单号
'attach' => 'test_order_attach', // 订单附加信息(可选参数)
'notify_url' => 'https://www.baidu.com', // 异步通知地址(可选参数)
'callback_url' => 'https://www.baidu.com', // 支付后前端跳转地址(可选参数)
];
$url = $payjs->cashier($data);
header('Location:'.$url);
exit;
// 构造订单基础信息
$data = [
'body' => '订单测试', // 订单标题
'total_fee' => 2, // 订单金额
'out_trade_no' => time(), // 订单号
'attach' => 'test_order_attach', // 订单附加信息(可选参数)
'notify_url' => 'https://www.baidu.com', // 异步通知地址(可选参数)
'openid' => 'xxxxxxxxxxxxx', // OPENID (必选参数)
];
$result = $payjs->jsapi($data);
print_r($result);
// 构造订单基础信息
$data = [
'body' => '订单测试', // 订单标题
'total_fee' => 2, // 订单金额
'out_trade_no' => time(), // 订单号
'attach' => 'test_order_attach', // 订单附加信息(可选参数)
'notify_url' => 'https://www.baidu.com', // 异步通知地址(可选参数)
'callback_url' => 'https://www.qq.com', // 前端跳转url(可选参数)
];
$result = $payjs->mweb($data);
print_r($result);
// 根据订单号查询订单状态
$payjs_order_id = '********************';
$result = $payjs->check($payjs_order_id);
print_r($result);
// 根据订单号关闭订单
$payjs_order_id = '********************';
$result = $payjs->close($payjs_order_id);
print_r($result);
// 根据订单号退款
$payjs_order_id = '*********************';
$result = $payjs->refund($payjs_order_id);
print_r($result);
// 构造订单基础信息
$data = [
'mchid' => '123123', // 商户号
];
$result = $payjs->complaint($data);
print_r($result);
// 返回商户基础信息
$result = $payjs->info();
print_r($result);
// 根据订单信息中的 OPENID 查询用户资料
$openid = '*******************';
$result = $payjs->user($openid);
print_r($result);
// 根据订单信息中的银行编码查询银行中文名称
$bank = '*******************';
$result = $payjs->bank($bank);
print_r($result);
// 接收异步通知,无需关注验签动作,已自动处理
$notify_info = $payjs->notify();
// 接收信息后自行处理