下单:
/**
* @param Request $request
* @return \think\response\Json
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
* @throws \GuzzleHttp\Exception\GuzzleException
* 下单
*/
public function charge(Request $request)
{
$config = [
//商户微信支付配置
'app_id' => 'appID',
'mch_id' => '商户号',
'key' => '秘钥',
'notify_url' => 'http://lp.llons.com/api/chargeBack', // 支付回调地址,绝对地址
];
$app = Factory::payment($config);
$result = $app->order->unify([
'body' => '套餐充值',
'out_trade_no' => '订单号',
'total_fee' => '价钱',//分为单位
'trade_type' => 'JSAPI',//支付方式,更改为不同的支付方式即可(H5,APP等等)
// 'notify_url' => '/chargeBack',//如果不填入就默认配置里面的notify_url(支付回调地址)
'openid' => '',//该字段是在JSAPI下才有的字段
]);
//自己的逻辑
$jssdk = $app->jssdk;//生成调起支付接口的参数,不同支付方式接口不一样
$pay = $jssdk->sdkConfig($result['prepay_id']);
return success([
'result' =>$pay
]);
}
支付回调:就是一个非常纯粹的POST请求接口
public function chargeBack()
{
$config = [
//商户微信支付配置
'app_id' => 'appID',
'mch_id' => '商户号',
'key' => '秘钥',
];
$app = Factory::payment($config);
$response = $app->handlePaidNotify(function($message, $fail){
if ($message['return_code'] === 'SUCCESS' &&
$message['result_code'] === 'SUCCESS') {
//自己的逻辑$message['transaction_id'],$message['out_trade_no']
} else {
return $fail('通信失败,请稍后再通知我');
}
return true; // 返回处理完成
});
$response->send();
}
退款回调
/**
* @throws \EasyWeChat\Kernel\Exceptions\Exception
* 微信退款回调地址
*/
public function wecharRefundBack()
{
$config = [
//商户微信支付配置
'app_id' => 'appID',
'mch_id' => '商户号',
'key' => '秘钥',
'cert_path' => '', // XXX: 绝对路径!!!!(前往微信公众平台上下载)
'key_path' => '',//证书同上
'notify_url' => ''//退款回调地址
];
$app = Factory::payment($config);
$response = $app->handleRefundedNotify(function ($message, $reqInfo, $fail) {
if ($message['return_code'] === 'SUCCESS' && $reqInfo['refund_status'] === 'SUCCESS') {
//$reqInfo['out_trade_no']
//自己的逻辑
$this->focusSend($order['class_id'], $order['student_id']);
} else {
return $fail('通信失败,请稍后在通知我');
}
return true; // 返回 true 告诉微信“我已处理完成”
// 或返回错误原因 $fail('参数格式校验错误');
});
$response->send();
}
退款:可以根据订单号或者支付单号退款。这里自根据支付单号
//微信退款
public function wechatRefund($transactionId, $totalFee, $refundFee)
{
$config = [
//商户微信支付配置
'app_id' => 'appID',
'mch_id' => '商户号',
'key' => '秘钥',
'cert_path' => '', // XXX: 绝对路径!!!!(前往微信公众平台上下载)
'key_path' => '',//证书同上
'notify_url' => ''//退款回调地址
];
$app = Factory::payment($config);
$a = get_refund_num();//退款单号
$result = $app->refund->byTransactionId($transactionId, $a, $totalFee * 100, $refundFee * 100, [
'refund_desc' => '班级退课',
'notify_url' => ''//退款回调地址
]);
}
(做的笔记,自己看的)