jh-admin/addon/coupon/event/weChatNotify.php

89 lines
3.8 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* SAAS应用系统 --- 十年开发经验汇集巨献!
* ==========================================================
* Copy right 2020-2050 成都众联思索科技有限公司,保留所有权利。
* ----------------------------------------------------------
* 官方网址: https://www.zoomtk.com
* 这不是自由软件!未经允许不得用于商业目或程序代码摘取及修改。
* 任何企业和个人未经允许对程序代码以任何形式任何目的再发布传播。
* 唯一发布渠道www.zoomtk.com;非官方渠道统一视为侵权行为。
* ==========================================================
*/
namespace addon\coupon\event;
use addon\coupon\model\Coupon;
use addon\wechatpay\model\Config;
use think\Exception;
use think\facade\Log;
class weChatNotify{
/**
* Common: 微信回调处理
* Author: wu-hui
* Time: 2023/07/18 15:18
* @param array $params
* @throws Exception\
*/
public function handle($params = []){
$eventType = $params['event_type'] ?? '';
$resourceType = $params['resource_type'] ?? '';
$requestUrl = $params['request_url'] ?? '';
// 通过请求地址获取site_id
$requestArr = explode('weChatNotify/',$requestUrl);
$params['site_id'] = (int)$requestArr[count($requestArr) - 1];
// 微信【代金券】回调通知
try{
if($eventType == 'COUPON.USE' && $resourceType == 'encrypt-resource' && $params['site_id'] > 0){
$this->voucherUseHandle($params);
}
}catch(Exception $e){
Log::debug('微信相关回调处理 - 错误抛出:'.$e->getMessage());
}
}
/**
* Common: 微信代金券使用回调处理
* Author: wu-hui
* Time: 2023/07/18 16:47
* @param $params
* @throws Exception
*/
private function voucherUseHandle($params){
sleep(3);// TODO由于支付回调在优惠券回调之后 这里延迟3秒执行 这里做临时调试方案,调试完成后需要修改为计划任务处理
$resource = $params['resource'] ?? [];
$payConfig = (new Config())->getPayConfig($params['site_id'])['data']['value'];
// 信息解密
$strBase64DecodeText = base64_decode($resource['ciphertext']);
$ctext = substr($strBase64DecodeText,0,-16);
$authTag = substr($strBase64DecodeText,-16);
$result = openssl_decrypt($ctext,'aes-256-gcm',$payConfig['v3_pay_signkey'],1,$resource['nonce'],$authTag,$resource['associated_data']);
if(!$result || empty($result)) throw new Exception('解密失败!');
// 信息获取
$notifyInfo = json_decode($result,TRUE);// 优惠券回调解密后的信息
$couponInfo = model('promotion_coupon')->getInfo([
['coupon_code','=',(int)$notifyInfo['coupon_id']]
],'member_id,coupon_id');
$transactionId = $notifyInfo['consume_information']['transaction_id'];
$tradeNo = model('pay')->getValue([
['trade_no','=',$transactionId]
],'out_trade_no');
$orderId = model('order')->getValue([
['out_trade_no','=',$tradeNo]
],'order_id');
// 判断:参数是否齐全
if((int)$couponInfo['coupon_id'] > 0 && (int)$couponInfo['member_id'] > 0 && (int)$orderId > 0){
// 优惠券使用处理
$couponModel = new Coupon();
$result = $couponModel->useCoupon($couponInfo['coupon_id'],$couponInfo['member_id'],$orderId);//使用优惠券
Log::debug('微信相关回调处理 - 优惠券代金券使用回调处理 - 使用结果:'.json_encode($result));
}
else{
$this->voucherUseHandle($params);
}
}
}