323 行
16 KiB
PHP
可执行文件
323 行
16 KiB
PHP
可执行文件
<?php
|
|
|
|
namespace SmallProgramApi\Controller;
|
|
|
|
use Common\Business\PayCoupon;
|
|
use Common\Model\PayInfoModel;
|
|
use Common\Model\RetailBillDiscountModel;
|
|
use Common\Model\RetailBillOrderModel;
|
|
use Think\Controller;
|
|
|
|
//[1.8.68]
|
|
|
|
|
|
/**
|
|
* 微信支付
|
|
* Class WxPayController
|
|
* @package SmallProgramApi\Controller
|
|
*/
|
|
class WxPayController extends Controller
|
|
{
|
|
static $bill_id = 0; // bill表id
|
|
static $pay_order_id = 0; // pay_order_id
|
|
static $pay_order; // pay_order
|
|
|
|
public function _initialize()
|
|
{
|
|
// $this->return_success();exit;
|
|
// $xml = $GLOBALS['HTTP_RAW_POST_DATA'];
|
|
file_put_contents('./xcxPay.txt', "[_initialize] 方法开始执行\r\n", FILE_APPEND);
|
|
|
|
$xml = file_get_contents('php://input');
|
|
file_put_contents('./xcxPay.txt', "[_initialize] 获取到原始XML数据长度: " . strlen($xml) . "\r\n", FILE_APPEND);
|
|
|
|
$extend_content = json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA));//转换格式
|
|
file_put_contents('./xcxPay.txt', 'xcx_post: ' . $extend_content . "\r\n", FILE_APPEND);
|
|
file_put_contents('./xcxPay.txt', "[_initialize] XML转换为JSON成功\r\n", FILE_APPEND);
|
|
|
|
$post = json_decode($extend_content, true);
|
|
file_put_contents('./xcxPay.txt', "[_initialize] JSON解析结果: " . json_encode($post) . "\r\n", FILE_APPEND);
|
|
|
|
if (empty($post['out_trade_no'])) {
|
|
file_put_contents('./xcxPay.txt', "[_initialize] 错误: out_trade_no为空,请求有误\r\n", FILE_APPEND);
|
|
$this->return_error('请求有误');
|
|
}
|
|
file_put_contents('./xcxPay.txt', "[_initialize] out_trade_no: " . $post['out_trade_no'] . "\r\n", FILE_APPEND);
|
|
|
|
$PayOrderModel = D('RetailPayOrder');
|
|
$payOrder = $PayOrderModel->getFind(array($PayOrderModel::OUT_TRADE_NO => strval($post['out_trade_no'])));
|
|
file_put_contents('./xcxPay.txt', "[_initialize] 查询支付订单结果: " . json_encode($payOrder) . "\r\n", FILE_APPEND);
|
|
|
|
if (empty($payOrder)) {
|
|
file_put_contents('./xcxPay.txt', "[_initialize] 错误: 订单不存在\r\n", FILE_APPEND);
|
|
$this->return_error('订单不存在');
|
|
}
|
|
/*
|
|
* 首先判断,订单是否已经更新为ok,因为微信会总共发送8次回调确认
|
|
* 其次,订单已经为ok的,直接返回SUCCESS
|
|
* 最后,订单没有为ok的,更新状态为ok,返回SUCCESS
|
|
*/
|
|
file_put_contents('./xcxPay.txt', "[_initialize] 检查订单状态: " . $payOrder[$PayOrderModel::ORDER_STATE] . "\r\n", FILE_APPEND);
|
|
|
|
if ($payOrder[$PayOrderModel::ORDER_STATE] == 'SUCCESS') {
|
|
file_put_contents('./xcxPay.txt', "[_initialize] 订单已经是SUCCESS状态,直接返回成功\r\n", FILE_APPEND);
|
|
$this->return_success();
|
|
}
|
|
|
|
$PaySetModel = D('RetailPaySet');
|
|
$paySet = $PaySetModel->getFindMerchantConfig($payOrder[$PayOrderModel::MERCHANT_ID], 3);
|
|
file_put_contents('./xcxPay.txt', "[_initialize] 获取商户支付配置: " . json_encode($paySet) . "\r\n", FILE_APPEND);
|
|
|
|
$payConfig = $paySet['config'];
|
|
$postSign = $post['sign'];
|
|
file_put_contents('./xcxPay.txt', "[_initialize] 微信回调签名: " . $postSign . "\r\n", FILE_APPEND);
|
|
|
|
unset($post['sign']);
|
|
/* 微信官方提醒:
|
|
* 商户系统对于支付结果通知的内容一定要做【签名验证】,
|
|
* 并校验返回的【订单金额是否与商户侧的订单金额】一致,
|
|
* 防止数据泄漏导致出现"假通知",造成资金损失。
|
|
*/
|
|
file_put_contents('./xcxPay.txt', "[_initialize] 支付类型: " . $payOrder[$PayOrderModel::M_PAY_TYPE] . "\r\n", FILE_APPEND);
|
|
|
|
if ($payOrder[$PayOrderModel::M_PAY_TYPE] == 1) {
|
|
//普通商户模式
|
|
$wx_key = $payConfig[$PaySetModel::XCX_SECRET_KEY];
|
|
file_put_contents('./xcxPay.txt', "[_initialize] 使用普通商户模式密钥\r\n", FILE_APPEND);
|
|
} else {
|
|
//服务商模式
|
|
$SystemConfigModel = D('SystemConfig');
|
|
$wx_key = $payConfig['systemConfig'][$SystemConfigModel::WX_KEY];
|
|
file_put_contents('./xcxPay.txt', "[_initialize] 使用服务商模式密钥\r\n", FILE_APPEND);
|
|
}
|
|
|
|
$user_sign = createSign($post, $wx_key); //再次生成签名,与$postSign比较
|
|
file_put_contents('./xcxPay.txt', "[_initialize] 本地生成签名: " . $user_sign . "\r\n", FILE_APPEND);
|
|
file_put_contents('./xcxPay.txt', "[_initialize] 签名验证结果: " . ($postSign == $user_sign ? '通过' : '失败') . "\r\n", FILE_APPEND);
|
|
file_put_contents('./xcxPay.txt', "[_initialize] return_code: " . $post['return_code'] . ", result_code: " . $post['result_code'] . "\r\n", FILE_APPEND);
|
|
if ($post['return_code'] == 'SUCCESS' && $post['result_code'] == 'SUCCESS' && $postSign == $user_sign) {
|
|
//未进行更新订单
|
|
file_put_contents('./xcxPay.txt', "[_initialize] 支付成功且签名验证通过,开始更新订单\r\n", FILE_APPEND);
|
|
|
|
define('IsPayToUpdateOrder', 'YES');//存入常量
|
|
$updateData = array(
|
|
$PayOrderModel::ORDER_STATE => 'SUCCESS',
|
|
$PayOrderModel::EXTEND_CONTENT => json_encode($extend_content),
|
|
$PayOrderModel::TRADE_DATE => time(),
|
|
$PayOrderModel::TRADE_NO => $post['transaction_id'],
|
|
);
|
|
file_put_contents('./xcxPay.txt', "[_initialize] 准备更新订单数据: " . json_encode($updateData) . "\r\n", FILE_APPEND);
|
|
|
|
if (!empty($post['promotion_detail']) || $post['coupon_count'] > 0) {
|
|
file_put_contents('./xcxPay.txt', "[_initialize] 检测到代金券使用,开始处理\r\n", FILE_APPEND);
|
|
//代金券使用状态修改,微信暂无该文档
|
|
if(!empty($post['promotion_detail'])){
|
|
file_put_contents('./xcxPay.txt', "[_initialize] 处理promotion_detail代金券\r\n", FILE_APPEND);
|
|
$promotion_detail = json_decode($post['promotion_detail'], true)['promotion_detail'];
|
|
if(!empty($promotion_detail) && !empty($promotion_detail[0]['promotion_id'])){
|
|
$pay_info = [
|
|
'coupon_count' => 1,
|
|
'coupon_id_0' => $promotion_detail[0]['promotion_id'],
|
|
'coupon_fee' => $promotion_detail[0]['amount'],
|
|
'cash_fee' => $post['cash_fee']
|
|
];
|
|
file_put_contents('./xcxPay.txt', "[_initialize] promotion_detail代金券信息: " . json_encode($pay_info) . "\r\n", FILE_APPEND);
|
|
}
|
|
}else if($post['coupon_count'] > 0){
|
|
file_put_contents('./xcxPay.txt', "[_initialize] 处理coupon_count代金券,数量: " . $post['coupon_count'] . "\r\n", FILE_APPEND);
|
|
$pay_info = $post;
|
|
}
|
|
if(!empty($pay_info)){
|
|
file_put_contents('./xcxPay.txt', "[_initialize] 开始代金券验证,参数: " . json_encode($pay_info) . "\r\n", FILE_APPEND);
|
|
|
|
$PayCouponClass = new PayCoupon();
|
|
$couponResult = $PayCouponClass->couponVerification_v1(json_encode([
|
|
'pay_info' => $pay_info,
|
|
'use_type' => $payOrder['order_type'],
|
|
'order_id' => $payOrder['bill_id'] ? $payOrder['bill_id'] : $payOrder['id'],
|
|
'store_id' => $payOrder['store_id'],
|
|
'use_scene' => 1,
|
|
'merchant_id' => $payOrder['merchant_id'],
|
|
'coupon_way' => 1
|
|
]));//修改代金券使用状态
|
|
file_put_contents('./xcxPay.txt', "[_initialize] 代金券验证结果: " . json_encode($couponResult) . "\r\n", FILE_APPEND);
|
|
|
|
if ($couponResult['status'] == 1) {
|
|
file_put_contents('./xcxPay.txt', "[_initialize] 代金券验证成功,更新订单金额\r\n", FILE_APPEND);
|
|
$updateData['trade_amount'] = $couponResult['data']['cash_fee'];
|
|
$updateData['pay_coupon_id_str'] = $couponResult['data']['pay_coupon_id_str'];
|
|
$updateData['pay_coupon_fee'] = $couponResult['data']['pay_coupon_fee'];
|
|
|
|
if (!empty($payOrder['bill_id'])) {
|
|
file_put_contents('./xcxPay.txt', "[_initialize] 存在bill_id,更新订单金额,bill_id: " . $payOrder['bill_id'] . "\r\n", FILE_APPEND);
|
|
//如果存在订单id修改订单金额
|
|
$BillOrderModel = new RetailBillOrderModel();
|
|
$BillDiscountModel = new RetailBillDiscountModel();
|
|
$BillOrderModel->toSetDec([
|
|
'id' => $payOrder['bill_id']
|
|
], 'all_money', $updateData['pay_coupon_fee']);
|
|
$BillOrderModel->toSetDec([
|
|
'id' => $payOrder['bill_id']
|
|
], 'amount_received', $updateData['pay_coupon_fee']);
|
|
|
|
$discountInfo = $BillDiscountModel->getFind([
|
|
'bill_id' => $payOrder['bill_id']
|
|
], 'pay_coupon_id_str,pay_coupon_fee');
|
|
file_put_contents('./xcxPay.txt', "[_initialize] 查询折扣信息: " . json_encode($discountInfo) . "\r\n", FILE_APPEND);
|
|
|
|
if (empty($discountInfo['pay_coupon_id_str'])) {
|
|
$dis_pay_coupon_id_str = $updateData['pay_coupon_id_str'];
|
|
} else {
|
|
$dis_pay_coupon_id_str = $discountInfo['pay_coupon_id_str'] . ',' . $updateData['pay_coupon_id_str'];
|
|
}
|
|
if ($discountInfo['pay_coupon_fee'] > 0) {
|
|
$dis_pay_coupon_fee = round($discountInfo['pay_coupon_fee'] + $updateData['pay_coupon_fee'], 2);
|
|
} else {
|
|
$dis_pay_coupon_fee = $updateData['pay_coupon_fee'];
|
|
}
|
|
|
|
$BillDiscountModel->updateOrder([
|
|
'bill_id' => $payOrder['bill_id']
|
|
], [
|
|
'pay_coupon_id_str' => $dis_pay_coupon_id_str,
|
|
'pay_coupon_fee' => $dis_pay_coupon_fee,
|
|
]);
|
|
file_put_contents('./xcxPay.txt', "[_initialize] 更新折扣信息完成,代金券ID: " . $dis_pay_coupon_id_str . ", 金额: " . $dis_pay_coupon_fee . "\r\n", FILE_APPEND);
|
|
}
|
|
$payOrder['pay_coupon_id_str'] = $couponResult['data']['pay_coupon_id_str'];
|
|
$payOrder['pay_coupon_fee'] = $couponResult['data']['pay_coupon_fee'];
|
|
} else {
|
|
file_put_contents('./xcxPay.txt', "[_initialize] 代金券验证失败\r\n", FILE_APPEND);
|
|
}
|
|
} else {
|
|
file_put_contents('./xcxPay.txt', "[_initialize] 无代金券信息需要处理\r\n", FILE_APPEND);
|
|
}
|
|
}
|
|
$PayOrderModel->updateOrder(array($PayOrderModel::ID => $payOrder[$PayOrderModel::ID]), $updateData); //更新pay_order表
|
|
file_put_contents('./xcxPay.txt', "[_initialize] 订单更新完成,订单ID: " . $payOrder[$PayOrderModel::ID] . "\r\n", FILE_APPEND);
|
|
|
|
$this::$bill_id = $payOrder[$PayOrderModel::BILL_ID];
|
|
$this::$pay_order_id = $payOrder[$PayOrderModel::ID];
|
|
$this::$pay_order = $payOrder;
|
|
file_put_contents('./xcxPay.txt', "[_initialize] 设置静态变量 - bill_id: " . $this::$bill_id . ", pay_order_id: " . $this::$pay_order_id . "\r\n", FILE_APPEND);
|
|
} else {
|
|
file_put_contents('./xcxPay.txt', "[_initialize] 支付验证失败或状态异常,直接返回成功\r\n", FILE_APPEND);
|
|
$this->return_success();
|
|
}
|
|
file_put_contents('./xcxPay.txt', "[_initialize] 方法执行完成\r\n", FILE_APPEND);
|
|
}
|
|
|
|
/**
|
|
* 普通下单回调接口
|
|
*/
|
|
public function normal_order_notify_url_v1()
|
|
{
|
|
$pay = A('PayApi/Order')->paySuccess($this::$bill_id);
|
|
// file_put_contents('./xcxPay.txt', 'update_bill: ' . json_encode($pay) . "\r\n", FILE_APPEND);
|
|
$this->return_success();
|
|
}
|
|
|
|
/**
|
|
* 购买礼品卡回调接口
|
|
*/
|
|
public function gift_notify_url_v1()
|
|
{
|
|
A('PayApi/Pay')->gift_continent_pay_v1($this::$pay_order_id);
|
|
|
|
$this->return_success();
|
|
}
|
|
|
|
/**
|
|
* 购买plus会员回调接口
|
|
*/
|
|
public function plus_notify_url_v1()
|
|
{
|
|
A('PayApi/Pay')->plus_continent_pay_v1($this::$pay_order_id);
|
|
$this->return_success();
|
|
}
|
|
|
|
/**
|
|
* 购买plus会员回调接口
|
|
*/
|
|
public function plus_notify_url_v2()
|
|
{
|
|
A('PayApi/Pay')->plus_continent_pay_v2($this::$pay_order_id);
|
|
$this->return_success();
|
|
}
|
|
|
|
/**
|
|
*拼团订单回调
|
|
*/
|
|
public function group_notify_url()
|
|
{
|
|
A('PayApi/ActivityOrder')->groupOrderContinentPay($this::$pay_order);
|
|
$this->return_success();
|
|
}
|
|
|
|
/**
|
|
* 秒杀订单回调
|
|
*/
|
|
public function spike_notify_url()
|
|
{
|
|
A('PayApi/ActivityOrder')->spikeOrderContinentPay($this::$pay_order);
|
|
$this->return_success();
|
|
}
|
|
|
|
/**
|
|
* 预售回调
|
|
*/
|
|
public function advance_notify_url()
|
|
{
|
|
A('PayApi/ActivityOrder')->advance_pay_notify($this::$pay_order);
|
|
$this->return_success();
|
|
}
|
|
|
|
/**
|
|
* 小程序充值回调
|
|
*/
|
|
public function recharge_notify_url()
|
|
{
|
|
A('PayApi/Recharge')->updateOrder($this::$pay_order);
|
|
$this->return_success();
|
|
}
|
|
|
|
/**
|
|
* 购买次卡
|
|
*/
|
|
public function card_notify_url()
|
|
{
|
|
A('Api/SubCard')->updataCardOrder($this::$pay_order);
|
|
$this->return_success();
|
|
}
|
|
|
|
|
|
/**
|
|
* 2019-11-12 普通下单回调接口 plus会员时长套餐自定义版
|
|
*/
|
|
public function normal_order_notify_url_v2()
|
|
{
|
|
$pay = A('PayApi/Order')->paySuccess_v1($this::$bill_id);
|
|
file_put_contents('./xcxPay.txt', 'update_bill: ' . json_encode($pay) . "\r\n", FILE_APPEND);
|
|
$this->return_success();
|
|
}
|
|
|
|
private function return_success()
|
|
{
|
|
$xml_post = '<xml>
|
|
<return_code>SUCCESS</return_code>
|
|
<return_msg>OK</return_msg>
|
|
</xml>';
|
|
echo $xml_post;
|
|
exit;
|
|
}
|
|
|
|
private function return_error($msg)
|
|
{
|
|
$xml_post = '<xml>
|
|
<return_code>FAIL</return_code>
|
|
<return_msg>' . $msg . '</return_msg>
|
|
</xml>';
|
|
echo $xml_post;
|
|
exit;
|
|
}
|
|
} |