文件
mashangyoujiu/Application/ShopCoupon/Business/ShopCoupon.class.php

911 行
38 KiB
PHP
可执行文件

<?php
namespace ShopCoupon\Business;
use Addons\Pay\WxPayV3;
use Common\Business\AppInfo;
use Common\Business\CreateFile;
use Common\Business\MerchantConfig;
use Common\Business\Store;
use Common\Business\WxUpload;
use Common\Model\RetailMerchantConfigModel;
use Common\Model\RetailPaySetModel;
use ShopCoupon\Model\ShopCouponModel;
/**
* 商家券类
* Class ShopCoupon
* [
* 微信参数 wxInfo
* 下载券码 downloadCouponCode
* 生成券码 generateCouponCode
* 创建券码目录 createCodeCatalog
* 下载券码的参数验证 downloadCouponCodeVerify
* 核销商家券 verificationCoupon
* 发放状态修改或者删除 suspendOperation
* 图片上传至微信 wxUpload
* 创建商家券 createCoupon
* 创建商家券存表 addCoupon
* 创建商家券参数转换 createCouponReplace
* 创建商家券的参数验证 createCouponVerify
* ]
* @package ShopCoupon\Business
*/
class ShopCoupon
{
protected $mch_id; //商户号
protected $serial_no; //证书序列号
protected $apiclient_key; //私钥地址
protected $son_mch_id; //子商户
protected $appid; //创建时的appid
protected $path; //小程序path
protected $xcx_appid; //小程序的ppid
protected $wx_appid; //公众号ppid
/**
* 微信参数
* @param $param
* [
* merchant_id
* user_type 1普通支付模式 2服务商支付模式
* type 1小程序 2公众号
* ]
*/
protected function wxInfo($param)
{
$merchant_id = $param['merchant_id'];
$type = $param['type'];
$user_type = $param['user_type'];
$paySetModel = new RetailPaySetModel();
$paySet = $paySetModel->getFindMerchantPaySet($merchant_id);
$this->path = '/pages/store/store';
$this->wx_appid = $paySet['config']['wx_appid'];
switch ($user_type) {
case 1:
//普通支付模式
$this->xcx_appid = $paySet['config']['xcx_appid'];
switch ($type) {
case 1:
//小程序
$this->mch_id = $paySet['config']['xcx_mer_id'];
$this->serial_no = $paySet['config']['wx_xcx_serial_no'];
$this->apiclient_key = $paySet['config']['xcx_apiclient_key'];
$this->appid = $paySet['config']['xcx_appid'];
break;
case 2:
//公众号
$this->mch_id = $paySet['config']['wx_mch_id'];
$this->serial_no = $paySet['config']['wx_serial_no'];
$this->apiclient_key = $paySet['config']['apiclient_key'];
$this->appid = $paySet['config']['wx_appid'];
break;
}
break;
case 2:
//服务商支付模式
$this->xcx_appid = $paySet['config']['xcx_sub_appid'];
$this->son_mch_id = $paySet['config']['xcx_sub_mchid'];
$this->mch_id = $paySet['config']['systemConfig']['mch_id'];
$this->serial_no = $paySet['config']['systemConfig']['wx_serial_no'];
$this->apiclient_key = $paySet['config']['systemConfig']['wx_apiclient_key'];
$this->appid = $paySet['config']['systemConfig']['wx_appid'];
break;
}
}
/**
* 下载券码
* @param $param
* [
* merchant_id
* couponList 券列表
* ]
* @return mixed
*/
public function downloadCouponCode($param)
{
$verifyResult = $this->downloadCouponCodeVerify($param); //参数验证
if ($verifyResult['status'] != 1) {
return $verifyResult;
}
$path = $this->createCodeCatalog($param); //创建目录
$param['path'] = $path;
$codeResult = $this->generateCouponCode($param); //生成券码
$store_arr = json_encode($codeResult['data']['store_arr']);//门店集合
$coupon_arr = json_encode($codeResult['data']['coupon_arr']); //券集合
return callBack(statusCode()['success'], "生成成功", ['path' => $path, 'store_arr' => $store_arr, 'coupon_arr' => $coupon_arr]);
}
/**
* 生成券码
* @param $param
* [
* merchant_id
* couponList 券列表
* path 目录
* ]
* @return bool
*/
public function generateCouponCode($param)
{
$merchant_id = $param['merchant_id'];
$couponList = $param['couponList'];
$path = $param['path'];
$time = time();
$CreateFile = new CreateFile();
$StoreClass = new Store();
$storeInfo = $StoreClass->getStoreList([
'merchant_id' => $merchant_id,
]);//所有门店信息
$arr1 = array_column($storeInfo, "id");
$arr2 = array_column($storeInfo, "storetag");
$store_arr = array_combine($arr1, $arr2);//门店新数组
$arr1 = array_column($couponList, "id");
$arr2 = array_column($couponList, "stock_name");
$coupon_arr = array_combine($arr1, $arr2); //券新数组
foreach ($couponList as $value) {
if ($value['store_type'] == 1) {
//所有门店
$storeList = $storeInfo;
} else {
//指定门店
$storeList = $StoreClass->getAppointStoreInfo([
'merchant_id' => $merchant_id,
'store_id_gather' => $value['store_id_gather'],
'fields' => "id,storetag"
]);
}
if (!empty($storeList)) {
foreach ($storeList as $val) {
$data = [
'path' => "/pages/index/merCouponDetail/merCouponDetail?store_id=" . $val['id'] . '&genre=3&id=' . $value['id'],
'merchant_id' => $merchant_id,
'file_name' => $path . '/' . $val['id'] . '_' . $value['id'] . '_' . $time . '.jpg'
];
$CreateFile->createWxCode($data);
}
}
}
return callBack(statusCode()['success'], '生成成功', ['store_arr' => $store_arr, 'coupon_arr' => $coupon_arr]);
}
/**
* 创建券码目录
* @param $param
* [
* merchant_id
* ]
* @return string
*/
public function createCodeCatalog($param)
{
$num = createKey();
$path = "./Uploads/Merchant/ShopCoupon/{$param['merchant_id']}/{$num}";
$CreateFileClass = new CreateFile();
$CreateFileClass->createDir($path);
return $path;
}
/**
* 下载券码的参数验证
* @param $param
* [
* merchant_id
* couponList 券列表
* ]
* @return mixed
*/
public function downloadCouponCodeVerify($param)
{
if (empty($param['merchant_id'])) {
return callBack(statusCode()['error'], "缺少身份必要参数");
}
if (empty($param['couponList'])) {
return callBack(statusCode()['error'], "请选择商家券");
}
$AppInfoClass = new AppInfo();
$appInfo = $AppInfoClass->getAppInfo($param['merchant_id']);
if (empty($appInfo)) {
return callBack(statusCode()['error'], "请先去授权您的小程序再进行此操作");
}
return callBack(statusCode()['success'], "验证成功");
}
/**
* 发放状态修改或者删除
* @param $param
* [
* merchant_id
* coupon_id 商家券id
* operation_type 1发放 2暂停 3删除
* ]
* @return mixed
*/
public function suspendOperation($param)
{
if (empty($param['merchant_id']) || empty($param['coupon_id']) || empty($param['operation_type'])) {
return callBack(statusCode()['error'], "参数缺失");
}
switch ($param['operation_type']) {
case 1:
//开始发放
$data['suspend_status'] = 1;
break;
case 2:
//暂停发放
$data['suspend_status'] = 2;
break;
case 3:
//删除
$data['status'] = 2;
break;
}
$where = [
'merchant_id' => $param['merchant_id'],
'id' => $param['coupon_id']
];
$ShopCouponModel = new ShopCouponModel();
$result = $ShopCouponModel->updateOrder($where, $data);
if ($result) {
return callBack(statusCode()['success'], "操作成功");
} else {
return callBack(statusCode()['success'], "暂无修改");
}
}
/**
* 核销商家券
* @param $param
* [
* openid 用户小程序openid
* service_openid 服务商下的openid
* ordinary_openid 商户公众号的openid
* merchant_id
* user_type 1普通支付模式 2服务商支付模式
* type 1小程序 2公众号
* coupon_code 券code
* app_id 公众账号ID
* verification_available_type 核销场景 1小程序 2收银机 3公众号
* verification_store_id 核销的门店
* operator_id 核销人id 0为小程序用户购买核销
* ]
* @return mixed
*/
public function verificationCoupon($param)
{
if (empty($param['merchant_id']) || empty($param['user_type']) || empty($param['type']) || empty($param['coupon_code']) || empty($param['app_id']) || empty($param['verification_available_type']) || empty($param['verification_store_id'])) {
return callBack(statusCode()['error'], "缺少参数");
}
$this->wxInfo($param);
$time = time();
$use_time = date("Y-m-d H:i:s", $time) . '+08:00';
$use_time = str_replace(" ", "T", $use_time);
$use_request_no = onlyCreateKey();
$data = [
'mch_id' => $this->mch_id,
'serial_no' => $this->serial_no,
'apiclient_key' => $this->apiclient_key,
'data' => [
'coupon_code' => $param['coupon_code'],
'appid' => $param['app_id'],
'use_time' => $use_time,
'use_request_no' => $use_request_no,
]
];
switch ($param['user_type']) {
case 1:
//普通支付模式
switch ($param['type']) {
case 1:
//小程序
if (!empty($param['openid'])) {
$data['data']['openid'] = $param['openid'];
}
break;
case 2:
//公众号
if (!empty($param['ordinary_openid'])) {
$data['data']['openid'] = $param['ordinary_openid'];
}
break;
}
break;
case 2:
//服务商支付模式
if (!empty($param['service_openid'])) {
$data['data']['openid'] = $param['service_openid'];
}
break;
}
$WxPayV3Class = new WxPayV3($data);
$result = $WxPayV3Class->verificationCoupon();
file_put_contents('./shop_coupon_write.txt', $result . PHP_EOL, FILE_APPEND);
// file_put_contents('./shop_coupon2.txt',json_encode($data));
$result = json_decode($result, true);
if (!empty($result['stock_id'])) {
return callBack(statusCode()['success'], "核销成功", ['use_time' => $time, 'wechatpay_use_time' => $result['wechatpay_use_time'], 'use_request_no' => $use_request_no]);
} else {
return callBack(statusCode()['error'], $result['message'], ['use_time' => $time, 'wechatpay_use_time' => $time, 'use_request_no' => $use_request_no]);
}
}
/**
* 图片上传至微信
* @param $param
* [
* user_type 1普通支付模式 2服务商支付模式
* merchant_id
* ]
* @return array
*/
public function wxUpload($param)
{
if (empty($param['user_type'])) {
return callBack(statusCode()['error'], "请选择支付模式");
}
$this->wxInfo([
'merchant_id' => $param['merchant_id'],
'type' => 1,
'user_type' => $param['user_type']
]);
switch ($param['user_type']) {
case 1:
$image = $_FILES['label_img'];
break;
case 2:
$image = $_FILES['label_img_service'];
break;
}
$wxUploadClass = new WxUpload();
$result = $wxUploadClass->wxUpload([
'image' => $image,
'mch_id' => $this->mch_id,
'serial_no' => $this->serial_no,
'apiclient_key' => $this->apiclient_key,
'user_type' => $param['user_type']
]);
if ($result['status'] == 1) {
$data['merchant_id'] = $param['merchant_id'];
switch ($param['user_type']) {
case 1:
$data['shop_coupon_simple_image'] = $result['data']['image_path'];
$data['shop_coupon_simple_wx_image'] = $result['data']['media_url'];
break;
case 2:
$data['shop_coupon_service_image'] = $result['data']['image_path'];
$data['shop_coupon_service_wx_image'] = $result['data']['media_url'];
break;
}
$configClass = new MerchantConfig();
$configClass->shopCouponImgSet($data);
}
return $result;
}
/**
* 创建商家券
* @param $param
* [
* merchant_id
* operator_id
* data=>[
* store_id_gather 门店集合
* user_type 1普通支付模式 2服务商支付模式
* type 1小程序 2公众号
* stock_name 名称
* goods_name 适用商品范围
* stock_type 1满减 2折扣 3换购
* available_time 有效时间
* valid_time_type 生效时间类型
* coupon_use_rule=>[
* fixed_normal_coupon=>[
* discount_amount 优惠金额
* transaction_minimum 门槛
* ],
* coupon_available_time=>[
* available_week=>[
* week_day=>[],
* available_day_time=>[
* begin_time
* end_time
* ],
* ]
* irregulary_avaliable_time=>[
* begin_time
* end_time
* ]
* ]
* ],
* stock_send_rule=>[
* max_amount 批次总预算
* max_coupons_per_user 用户最大可领个数
* ],
* display_pattern_info=>[
* merchant_name 商户名称
* ]
* ]
* ]
* @return mixed
*/
public function createCoupon($param)
{
$paySetModel = new RetailPaySetModel();
$paySet = $paySetModel->getFindMerchantPaySet($param['merchant_id']);
if (!empty($paySet['config']['xcx_sub_mchid']) && !empty($paySet['config']['systemConfig']['mch_id']) && !empty($paySet['config']['systemConfig']['wx_serial_no']) && !empty($paySet['config']['systemConfig']['wx_apiclient_key']) && !empty($paySet['config']['systemConfig']['wx_appid'])) {
$param['data']['user_type'] = 2; //服务商模式
} else if (!empty($paySet['config']['xcx_mer_id']) && !empty($paySet['config']['wx_xcx_serial_no']) && !empty($paySet['config']['xcx_apiclient_key']) && !empty($paySet['config']['xcx_appid'])) {
$param['data']['user_type'] = 1; //普通模式
} else {
return callBack(statusCode()['error'], '请配置小程序支付参数');
}
if ($param['data']['user_type'] == 1) {
if (count($param['data']['available_type']) > 1 || in_array(2, $param['data']['available_type'])) {
$param['data']['type'] = 2; //公众号创建
} else if (in_array(1, $param['data']['available_type'])) {
$param['data']['type'] = 1; //小程序创建
}
} else {
$param['data']['type'] = 2; //公众号创建
}
$this->wxInfo([
'merchant_id' => $param['merchant_id'],
'user_type' => $param['data']['user_type'],
'type' => $param['data']['type'], //小程序创建
]); //获取微信配置参数
$verify = $this->createCouponVerify($param); //参数验证
if ($verify['status'] != 1) {
return $verify;
}
$new_param = $this->createCouponReplace($param); //去除多余参数
switch ($param['data']['user_type']) {
case 1:
$new_param['belong_merchant'] = $this->mch_id;
break;
case 2:
$new_param['belong_merchant'] = $this->son_mch_id;
break;
}
$data = [
'mch_id' => $this->mch_id,
'serial_no' => $this->serial_no,
'apiclient_key' => $this->apiclient_key,
'data' => $new_param
];
$WxPayV3Class = new WxPayV3($data);
$result = $WxPayV3Class->createShopCoupon();
file_put_contents('./shop_coupon.txt', $result . PHP_EOL, FILE_APPEND);
$result = json_decode($result, true);
if (!empty($result['stock_id'])) {
$param['stock_id'] = $result['stock_id'];
$param['out_request_no'] = $new_param['out_request_no'];
$result = $this->addCoupon($param); //存入表
return $result;
} else {
return callBack(statusCode()['error'], $result['message']);
}
}
/**
* 创建商家券存表
* @param $param
* [
* merchant_id
* operator_id
* data=>[
* store_id_gather 门店集合
* user_type 1普通支付模式 2服务商支付模式
* type 1小程序 2公众号
* stock_name 名称
* goods_name 适用商品范围
* stock_type 1满减 2折扣 3换购
* available_time 有效时间
* valid_time_type 生效时间类型
* coupon_use_rule=>[
* fixed_normal_coupon=>[
* discount_amount 优惠金额
* transaction_minimum 门槛
* ],
* coupon_available_time=>[
* available_week=>[
* week_day=>[],
* available_day_time=>[
* begin_time
* end_time
* ],
* ]
* irregulary_avaliable_time=>[
* begin_time
* end_time
* ]
* ]
* ],
* stock_send_rule=>[
* max_amount 批次总预算
* max_coupons_per_user 用户最大可领个数
* ],
* display_pattern_info=>[
* merchant_name 商户名称
* ]
* ]
* ]
* @return mixed
*/
public function addCoupon($param)
{
$data = $param['data'];
$discount_amount = $data['coupon_use_rule']['fixed_normal_coupon']['discount_amount'];
$max_amount = $data['stock_send_rule']['max_amount'];
$coupon_num = intval(floor($max_amount / $discount_amount));
$data['coupon_use_rule']['coupon_available_time']['available_begin_time'] = $data['available_time'][0];
$data['coupon_use_rule']['coupon_available_time']['available_end_time'] = $data['available_time'][1];
$content = [
'merchant_id' => $param['merchant_id'],
'app_id' => $this->appid,
'operator_id' => $param['operator_id'],
'belong_merchant' => $this->mch_id,
'stock_id' => $param['stock_id'],
'user_type' => $data['user_type'],
'type' => $data['type'],
'stock_name' => $data['stock_name'],
'goods_name' => $data['goods_name'],
'stock_type' => $data['stock_type'],
'available_begin_time' => strtotime($data['coupon_use_rule']['coupon_available_time']['available_begin_time']),
'available_end_time' => strtotime($data['coupon_use_rule']['coupon_available_time']['available_end_time']),
'valid_time_type' => $data['valid_time_type'] ? $data['valid_time_type'] : 0,
'coupon_use_rule' => json_encode($data['coupon_use_rule']),
'stock_send_rule' => json_encode($data['stock_send_rule']),
'display_pattern_info' => json_encode($data['display_pattern_info']),
'create_time' => time(),
'out_request_no' => $param['out_request_no'],
'store_type' => $data['store_type'],
'goods_type' => $data['goods_type'],
'available_type' => implode(',', $data['available_type']),
'coupon_num' => $coupon_num,
'operation_num' => $coupon_num
];
if (!empty($data['store_id_gather'])) {
$content['store_id_gather'] = implode(',', $data['store_id_gather']);
}
if (!empty($data['goodsList'])) {
$content['goods_list'] = json_encode($data['goodsList']);
$content['goods_id_gather'] = implode(',', array_column($data['goodsList'], 'goods_id'));
}
if (!empty($data['logo_image'])) {
$content['logo_image'] = $data['logo_image'];
}
$ShopCouponModel = new ShopCouponModel();
$coupon_id = $ShopCouponModel->addOrder($content);
if (!empty($coupon_id)) {
return callBack(statusCode()['success'], "添加成功");
} else {
return callBack(statusCode()['error'], "创建成功,记录添加失败");
}
}
/**
* 创建商家券参数转换
* @param $param
* [
* merchant_id
* operator_id
* data=>[
* store_id_gather 门店集合
* user_type 1普通支付模式 2服务商支付模式
* type 1小程序 2公众号
* stock_name 名称
* goods_name 适用商品范围
* stock_type 1满减 2折扣 3换购
* available_time 有效时间
* valid_time_type 生效时间类型
* coupon_use_rule=>[
* fixed_normal_coupon=>[
* discount_amount 优惠金额
* transaction_minimum 门槛
* ],
* coupon_available_time=>[
* available_week=>[
* week_day=>[],
* available_day_time=>[
* begin_time
* end_time
* ],
* ]
* irregulary_avaliable_time=>[
* begin_time
* end_time
* ]
* ]
* ],
* stock_send_rule=>[
* max_amount 批次总预算
* max_coupons_per_user 用户最大可领个数
* ],
* display_pattern_info=>[
* merchant_name 商户名称
* ]
* ]
* ]
* @return mixed
*/
public function createCouponReplace($param)
{
$new_param = $param['data'];
switch ($new_param['stock_type']) {
case 1:
$new_param['stock_type'] = 'NORMAL';
break;
case 2:
$new_param['stock_type'] = 'DISCOUNT';
break;
case 3:
$new_param['stock_type'] = 'EXCHANGE';
break;
}
if (!empty($new_param['coupon_use_rule']['coupon_available_time']['available_day_after_receive'])) {
$new_param['coupon_use_rule']['coupon_available_time']['available_day_after_receive'] = intval($new_param['coupon_use_rule']['coupon_available_time']['available_day_after_receive']);
} else {
unset($new_param['coupon_use_rule']['coupon_available_time']['available_day_after_receive']);
}
if (!empty($new_param['valid_time_type'])) {
switch ($new_param['valid_time_type']) {
case 1:
//无规律时间段
$new_param['coupon_use_rule']['coupon_available_time']['irregulary_avaliable_time'][] = ['begin_time' => $new_param['coupon_use_rule']['coupon_available_time']['irregulary_avaliable_time']['irregular_time'][0] . '+08:00', 'end_time' => $new_param['coupon_use_rule']['coupon_available_time']['irregulary_avaliable_time']['irregular_time'][1] . '+08:00'];
unset($new_param['coupon_use_rule']['coupon_available_time']['available_week']);
unset($new_param['coupon_use_rule']['coupon_available_time']['wait_days_after_receive']);
break;
case 2:
//固定周期
$time = strtotime(date('Y-m-d', time()));
$new_param['coupon_use_rule']['coupon_available_time']['available_week']['available_day_time'][] = ['begin_time' => strtotime($new_param['coupon_use_rule']['coupon_available_time']['available_week']['available_day_time']['begin_time']) - $time, 'end_time' => strtotime($new_param['coupon_use_rule']['coupon_available_time']['available_week']['available_day_time']['end_time']) - $time];
if (empty($new_param['coupon_use_rule']['coupon_available_time']['available_week']['week_day'])) {
unset($new_param['coupon_use_rule']['coupon_available_time']['available_week']['week_day']);
} else {
$new_param['coupon_use_rule']['coupon_available_time']['available_week']['week_day'] = array_map('int', $new_param['coupon_use_rule']['coupon_available_time']['available_week']['week_day']);
}
unset($new_param['coupon_use_rule']['coupon_available_time']['irregulary_avaliable_time']);
unset($new_param['coupon_use_rule']['coupon_available_time']['wait_days_after_receive']);
unset($new_param['coupon_use_rule']['coupon_available_time']['available_week']['available_day_time']['begin_time']);
unset($new_param['coupon_use_rule']['coupon_available_time']['available_week']['available_day_time']['end_time']);
break;
case 3:
//领取后N天开始生效
$new_param['coupon_use_rule']['coupon_available_time']['wait_days_after_receive'] = intval($new_param['coupon_use_rule']['coupon_available_time']['wait_days_after_receive']);
unset($new_param['coupon_use_rule']['coupon_available_time']['irregulary_avaliable_time']);
unset($new_param['coupon_use_rule']['coupon_available_time']['available_week']);
break;
}
} else {
unset($new_param['coupon_use_rule']['coupon_available_time']);
}
$new_param['coupon_use_rule']['fixed_normal_coupon']['discount_amount'] = $new_param['coupon_use_rule']['fixed_normal_coupon']['discount_amount'] * 100;
$new_param['coupon_use_rule']['fixed_normal_coupon']['transaction_minimum'] = $new_param['coupon_use_rule']['fixed_normal_coupon']['transaction_minimum'] * 100;
$new_param['stock_send_rule']['max_amount'] = $new_param['stock_send_rule']['max_amount'] * 100;
$new_param['stock_send_rule']['max_coupons_per_user'] = intval($new_param['stock_send_rule']['max_coupons_per_user']);
$new_param['coupon_use_rule']['coupon_available_time']['available_begin_time'] = $new_param['available_time'][0] . '+08:00';
$new_param['coupon_use_rule']['coupon_available_time']['available_end_time'] = $new_param['available_time'][1] . '+08:00';
if (count($param['data']['available_type']) > 1 || in_array(2, $param['data']['available_type'])) {
$new_param['coupon_use_rule']['use_method'] = 'PAYMENT_CODE'; //线下实体店
} else {
$new_param['coupon_use_rule']['use_method'] = 'MINI_PROGRAMS';
}
$new_param['coupon_use_rule']['mini_programs_appid'] = $this->xcx_appid;
$new_param['coupon_use_rule']['mini_programs_path'] = $this->path;
$new_param['out_request_no'] = createOrderTradeNo($param['merchant_id']); //随机字符串
$new_param['coupon_code_mode'] = 'WECHATPAY_MODE';
$new_param['custom_entrance']['code_display_mode'] = 'BARCODE';
$new_param['custom_entrance']['mini_programs_info']['mini_programs_appid'] = $this->xcx_appid;
$new_param['custom_entrance']['mini_programs_info']['mini_programs_path'] = $this->path;
$new_param['custom_entrance']['mini_programs_info']['entrance_words'] = '欢迎选购';
$new_param['custom_entrance']['appid'] = $this->wx_appid;
if (empty($new_param['display_pattern_info']['merchant_name'])) {
unset($new_param['display_pattern_info']['merchant_name']);
}
switch ($new_param['user_type']) {
case 1:
//普通
$image_path = 'shop_coupon_simple_wx_image';
break;
case 2:
//服务商
$image_path = 'shop_coupon_service_wx_image';
break;
}
$configModel = new RetailMerchantConfigModel();
$merchant_logo_url = $configModel->getFindField([
'merchant_id' => $param['merchant_id']
], $image_path);
if (!empty($merchant_logo_url)) {
$new_param['display_pattern_info']['merchant_logo_url'] = $merchant_logo_url;
}
$description = '不能与其他优惠券同时使用, '; //使用须知
if ($new_param['stock_type'] == 2) {
$StoreClass = new Store();
$storeResult = $StoreClass->storeNameList([
'store_id_gather' => $new_param['store_id_gather']
]);
if ($storeResult['status'] == 1) {
$description .= '可使用门店为' . implode(',', $storeResult['data']['list']) . ' ';
}
}
if (count($new_param['available_type']) > 1) {
$description .= '支持小程序商城与实体店使用 ';
} else {
if (in_array(1, $new_param['available_type'])) {
$description .= '支持小程序商城使用 ';
}
if (in_array(2, $new_param['available_type'])) {
$description .= '支持实体店使用 ';
}
}
if ($new_param['goods_type'] == 1) {
$description .= '针对全部商品';
} else if ($new_param['goods_type'] == 2) {
$goods_name = array_column($new_param['goodsList'], 'goods_name');
$description .= '针对' . implode(',', $goods_name) . '商品';
}
$new_param['display_pattern_info']['description'] = $description;
unset($new_param['goodsList']);
unset($new_param['store_id_gather']);
unset($new_param['store_type']);
unset($new_param['goods_type']);
unset($new_param['user_type']);
unset($new_param['type']);
unset($new_param['available_time']);
unset($new_param['valid_time_type']);
unset($new_param['coupon_use_rule']['coupon_available_time']['irregulary_avaliable_time']['irregular_time']);
unset($new_param['available_type']);
// print_r($new_param);exit;
return $new_param;
}
/**
* 创建商家券的参数验证
* @param $param
* [
* merchant_id
* operator_id
* data=>[
* store_id_gather 门店集合
* user_type 1普通支付模式 2服务商支付模式
* type 1小程序 2公众号
* stock_name 名称
* goods_name 适用商品范围
* stock_type 1满减 2折扣 3换购
* available_time 有效时间
* valid_time_type 生效时间类型
* coupon_use_rule=>[
* fixed_normal_coupon=>[
* discount_amount 优惠金额
* transaction_minimum 门槛
* ],
* coupon_available_time=>[
* available_week=>[
* week_day=>[],
* available_day_time=>[
* begin_time
* end_time
* ],
* ]
* irregulary_avaliable_time=>[
* begin_time
* end_time
* ]
* ]
* ],
* stock_send_rule=>[
* max_amount 批次总预算
* max_coupons_per_user 用户最大可领个数
* ],
* display_pattern_info=>[
* merchant_name 商户名称
* ]
* ]
* ]
* @return mixed
*/
public function createCouponVerify($param)
{
$reg = "/^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d{0,2})$/";
$reg2 = "/^\+?[0-9][0-9]*$/";
if (empty($param['merchant_id']) || empty($param['operator_id'])) {
return callBack(statusCode()['error'], "登录超时");
}
if (empty($this->mch_id) || empty($this->serial_no) || empty($this->apiclient_key)) {
return callBack(statusCode()['error'], "请检查支付配置,商户号,Api证书号,私钥证书");
}
$data = $param['data'];
$discount_amount = $data['coupon_use_rule']['fixed_normal_coupon']['discount_amount'];
$transaction_minimum = $data['coupon_use_rule']['fixed_normal_coupon']['transaction_minimum'];
$max_amount = $data['stock_send_rule']['max_amount'];
$max_coupons_per_user = $data['stock_send_rule']['max_coupons_per_user'];
if (empty($data['available_type'])) return callBack(statusCode()['error'], "请选择可用场景");
if (empty($data['stock_type'])) return callBack(statusCode()['error'], "请选择商家券类型");
if (empty($data['stock_name'])) return callBack(statusCode()['error'], "请输入商家券名称");
if (strlen($data['stock_name']) > 20) return callBack(statusCode()['error'], "商家券名称最多20个字符");
if (empty($data['goods_name'])) return callBack(statusCode()['error'], "请输入适用商品范围");
if (strlen($data['goods_name']) > 15) return callBack(statusCode()['error'], "适用商品范围最多15个字符");
switch ($data['stock_type']) {
case 1:
//满减券
if (empty($discount_amount) || $discount_amount < 0.01 || $discount_amount > 100000 || !preg_match($reg, $discount_amount)) {
return callBack(statusCode()['error'], "请输入正确优惠金额");
}
if (empty($transaction_minimum) || $transaction_minimum <= $discount_amount || $transaction_minimum > 100000 || !preg_match($reg, $transaction_minimum)) {
return callBack(statusCode()['error'], "请输入正确消费门槛");
}
if (empty($max_amount) || $max_amount < 0.01 || $max_amount > 1000000000 || !preg_match($reg, $max_amount)) {
return callBack(statusCode()['error'], "请输入正确批次总预算");
}
if (intval($max_amount * 10000 / ($discount_amount * 10000)) != ($max_amount * 10000 / ($discount_amount * 10000))) {
return callBack(statusCode()['error'], "总预算必须能除尽优惠金额");
}
break;
}
if (empty($max_coupons_per_user) || $max_coupons_per_user < 1 || $max_coupons_per_user > 100 || !preg_match($reg2, $max_coupons_per_user)) {
return callBack(statusCode()['error'], "请输入正确用户最大可领个数");
}
if (empty($data['available_time'])) {
return callBack(statusCode()['error'], "请选择券可核销时间");
}
if (!empty($data['coupon_use_rule']['coupon_available_time']['available_day_after_receive']) && !preg_match($reg2, $data['coupon_use_rule']['coupon_available_time']['available_day_after_receive'])) {
return callBack(statusCode()['error'], "请输入正确的生效后N天内有效");
}
if (!empty($data['valid_time_type'])) {
switch ($data['valid_time_type']) {
case 1:
if (empty($data['coupon_use_rule']['coupon_available_time']['irregulary_avaliable_time']['irregular_time'])) {
return callBack(statusCode()['error'], "请选择无规律有效时间段");
}
break;
case 2:
if (empty($data['coupon_use_rule']['coupon_available_time']['available_week']['available_day_time']['begin_time']) || empty($data['coupon_use_rule']['coupon_available_time']['available_week']['available_day_time']['end_time'])) {
return callBack(statusCode()['error'], "请选择固定周期有效时间段");
}
break;
case 3:
if (empty($data['coupon_use_rule']['coupon_available_time']['wait_days_after_receive']) || !preg_match($reg2, $data['coupon_use_rule']['coupon_available_time']['wait_days_after_receive'])) {
return callBack(statusCode()['error'], "请输入领取后N天开始生效");
}
if (empty($data['coupon_use_rule']['coupon_available_time']['available_day_after_receive'])) {
return callBack(statusCode()['error'], "该类型必须与生效后N天内有效一起使用");
}
break;
}
}
switch ($data['store_type']) {
case 1:
if ($data['goods_type'] != 1) {
return callBack(statusCode()['error'], "所有门店必须选择所有商品");
}
break;
case 2:
if (count($data['store_id_gather']) == 0) {
return callBack(statusCode()['error'], "请选择门店");
}
if ($data['goods_type'] == 2) {
if (count($data['store_id_gather']) != 1) {
return callBack(statusCode()['error'], "该类型只能选择一个门店");
}
if (count($data['goodsList']) == 0) {
return callBack(statusCode()['error'], "请选择商品");
}
if (count($data['goodsList']) > 20) {
return callBack(statusCode()['error'], "最多只能选择20种商品");
}
}
break;
}
return callBack(statusCode()['success'], "验证成功");
}
}