项目中用到了pc 扫码支付 微信+支付宝,稍作总结,方便以后使用
1.composer.json
更新扩展包 "yansongda/laravel-pay": "^2.1"
2.微信支付
扫码支付配置回调地址 两个都得配置。并且是post 方式
*我出现回调调不起来的问题,结合宝塔响应日志,在postman 中测试,发现问题。
页面代码很简单:如下:
if($data['pay'] == 'Wechat'){
$order = [
'out_trade_no' => $data['order_sn'],
'body' => $data['order_sn'].' 充值信息',
'total_fee' => $data['price'],
];
$config = PaymentHandler::getConfig();
// 验证服务器数据
$wechat = Pay::wechat($config);
// 扫码支付使用 模式二
$result = $wechat->scan($order)->toArray();
if($result['return_code'] == "SUCCESS"){
$ss = [
'order_no' => $data['order_sn'],
'user_id' => $this->userId,
//支付类型:1=发起支付,2=支付回调
'pay_type' => 1,
'money' => $data['price'],
// 类型:1=微信,2=支付宝
'type' => 1
];
$str_pay = json_encode($ss);
$path = storage_path('paylogs/wxpay/');
$filename=$path.date('Y-m-d').'.txt';
file_put_contents($filename, $str_pay.PHP_EOL, FILE_APPEND);
$tradePayment = [
'order_id' =>$orderinfo->id,
'order_no' => $data['order_sn'],
'user_id' => $this->userId,
// 支付渠道:1=支付宝,2=微信
'payment_channel' => 2,
'money' => $orderinfo->price ?? 0,
'type' => 1,
'code_url' => $result['code_url']
];
return success($tradePayment);
}
回调页面代码如下:
try {
$data = $pay->verify(); // 是的,验签就这么简单!
Log::debug('Wechat notify', $data->all());
$orderNo = $data->out_trade_no;
$cashFee = $data->cash_fee;
$payment = OrderInfoModel::query()->where('order_sn', $orderNo)->first();
if (OrderInfoModel::NO_PAY == $payment->status) {
// 更新订单表状态
$this->updateOrder($orderNo, $cashFee);
}
} catch (\Exception $e) {
Log::error('<--- 支付回调异常 --->', ['msg' => $e->getMessage(), 'line' => $e->getLine()]);
throw new Error(100000, $e->getMessage());
}
/**
* @return mixed
* @throws Error
*/
protected function getConfiginfo()
{
$testXml = file_get_contents("php://input");
$jsonXml = json_encode(simplexml_load_string($testXml, 'SimpleXMLElement', LIBXML_NOCDATA));
$path = storage_path('paylogs/wxpay/');
$filename=$path.date('Y-m-d').'.txt';
file_put_contents($filename, $jsonXml.PHP_EOL, FILE_APPEND);
$notifyData = json_decode($jsonXml, true);//转成数组
if (!$notifyData || !($notifyData['return_code'] == 'SUCCESS') || !($notifyData['result_code'] == 'SUCCESS')) {
throw new Error(100000, $notifyData['return_msg'] ?? '微信支付失败');
}
return PaymentHandler::getConfig();
}
网友评论