1.线上资金预授权冻结接口 说明:我用的是支付宝sdkv2,如果用use 别忘了加命名空间,而且预授权只能主动扣款,是不支持被动扣款的。
//线上资金预授权冻结接口
public function freeze($param) {
// 设置后付费项目
$postPayments = array();
$postPayments0 = array();
//后付费项目金额
$postPayments0['amount'] = $param['order_amount'];
//后付费项目名称 租金
$postPayments0['name'] = '租金';
//后付费项目计费说明
$postPayments0['description'] = $param['issue_rent'].'元/期,共'.$param['periods'].'期';
$postPayments[] = $postPayments0;
$model['post_payments'] = $postPayments;
// 设置订单标题
$model['order_title'] = $param['goods_name'];
// 设置需要冻结的金额
$model['amount'] = $param['order_amount'];
// 设置收款账户的支付宝用户号
$model['payee_user_id'] = '****************';
// 设置商户授权资金订单号
$model['out_order_no'] = $param['order_sn'];
// 设置销售产品码
$model['product_code'] = "PRE_AUTH_ONLINE";
// 设置免押受理台模式
// POSTPAY 表示后付金额已知
// POSTPAY_UNCERTAIN 表示后付金额未知
// DEPOSIT_ONLY 表示纯免押
$model['deposit_product_mode'] = "POSTPAY";
// 设置预授权订单相对超时时间
$model['timeout_express'] = "2d";
// 设置商户本次资金操作的请求流水号
$model['out_request_no'] = $param['order_sn'];
// 设置业务扩展参数
$model['extra_param'] = "{\"category\":\"RENT_PHONE\",\"serviceId\":\"2023072500000000000098546400\"}";
$this->alipayClient = new AopClient($this->getAlipayConfig());
$request = new AlipayFundAuthOrderAppFreezeRequest();
$request->setNotifyUrl($this->notify_url(1));
$request->setBizContent(json_encode($model,JSON_UNESCAPED_UNICODE));
// 如果是第三方代调用模式,请设置app_auth_token(应用授权令牌)
$responseResult = $this->alipayClient->sdkExecute($request);
return $responseResult;
}
上述代码:需要注意的地方就是项目计费说明
如果和符合支付宝方面的业务要求,一定要按照支付宝的规则写
2.将支付宝返回的字符串在返给前端 前端会调起支付宝 用户在支付宝上操作就可以了
3.冻结回调
//线上资金冻结回调
public function freeze_notify_url() {
$refundData = input();
if ($refundData && $refundData['status'] == 'SUCCESS') {
//授权成功,修改订单冻结状态
/** @var StoreOrderRepository $orderService */
$order = Db::name('lease_order')->where(['order_sn' => $refundData['out_order_no']])->find();
$order_model = new LeaseOrder();
$order2 = $order_model
->with('lease_order_goods')
->where('order_sn', $refundData['out_order_no'])
->find();
if (!$order) {
Log::info('冻结订单不存在或已处理冻结状态1:订单号:' . $refundData['out_order_no']);
} else {
$data = [
'deposit_status' => 1, //押金支付状态
'order_status' => 1,
'pay_status' => 1,
'ant_status' => 1,
'pay_time' => time(),
'auth_no' => $refundData['auth_no'],
'transaction_id' => $refundData['operation_id'],
'pay_way' => 2,
];
$res = Db::name('lease_order')->where(['id' => $order['id']])->update($data);
if ($res === false) {
Log::info('冻结订单状态修改为已冻结失败1:订单号:' . $refundData['out_order_no']);
}
$res = Db::name('lease_contract')->where(['lease_order_id' => $order['id']])->update(['contract_status'=>3]);
//这个是调用支付
$res1 = $this->trade_pay1($refundData['out_order_no']);
echo 'success';
}
}
//记录返回值日志
Log::info('预授权异步通知1:'. json_encode($refundData, true), $refundData);
}
注意:如果是冻结转支付 就得调用支付接口 别忘了echo 'sccess';
然后之后每次主动付款,直接调取支付就可以了 代码如下:
//统一收单交易支付接口
public function tradePay($order){
$model = array();
// 设置商户订单号
$model['out_trade_no'] = $order["out_request_no"];
// 设置订单总金额
$model['total_amount'] = $order['amount'];
// 设置订单标题
$model['subject'] = $order['remark'];
// 设置产品码
$model['product_code'] = "PREAUTH_PAY";
// 设置资金预授权单号
$model['auth_no'] = $order['auth_no'];
// 设置预授权确认模式
$model['auth_confirm_mode'] = "COMPLETE";
// 设置返回参数选项
$queryOptions = array();
$queryOptions[] = "repayment_order";
$model['query_options'] = $queryOptions;
$this->alipayClient = new AopClient($this->getAlipayConfig());
$request = new AlipayTradePayRequest();
$request->setBizContent(json_encode($model,JSON_UNESCAPED_UNICODE));
// 如果是第三方代调用模式,请设置app_auth_token(应用授权令牌)
$responseResult = $this->alipayClient->execute($request);
$responseApiName = str_replace(".","_",$request->getApiMethodName())."_response";
$response = $responseResult->$responseApiName;
return $response;
}