1040 行
42 KiB
PHP
可执行文件
1040 行
42 KiB
PHP
可执行文件
<?php
|
|
|
|
namespace PayCoupon\Business;
|
|
|
|
use Addons\Pay\WxPayV3;
|
|
use Common\Business\AppInfo;
|
|
use Common\Business\CreateFile;
|
|
use Common\Business\Store;
|
|
use Common\Business\WxUpload;
|
|
use Common\Model\RetailPaySetModel;
|
|
use PayCoupon\Model\PayCouponModel;
|
|
use PayCoupon\Model\PayCouponRecordModel;
|
|
use Vip\Controller\MarkController;
|
|
|
|
/**
|
|
* 代金券
|
|
* Class PayCoupon
|
|
* [
|
|
* 下载券码 downloadCouponCode
|
|
* 生成券码 generateCouponCode
|
|
* 生成券码 generateCouponCode
|
|
* 创建券码目录 createCodeCatalog
|
|
* 下载券码的参数验证 downloadCouponCodeVerify
|
|
* 获取必要信息 getWxInfo
|
|
* 公共参数验证 commonVerify
|
|
* 暂停或者重启代金券 suspendRestart
|
|
* 发放代金券 grantCoupon
|
|
* 发放代金券参数验证 grantCouponVerify
|
|
* 公众号信息 getWxInfo
|
|
* 激活代金券 couponActivation
|
|
* 图片上传至微信wxUpload
|
|
* 创建代金券 createCoupon
|
|
* 创建代金券参数替换 createCouponReplace
|
|
* 创建代金券记录表 payCouponAdd
|
|
* 代金券参数验证 createCouponVerify
|
|
* ]
|
|
* @package PayCoupon\Business
|
|
*/
|
|
class PayCoupon
|
|
{
|
|
protected $mch_id; //商户号
|
|
protected $serial_no; //证书序列号
|
|
protected $apiclient_key; //私钥地址
|
|
protected $son_mch_id; //子商户
|
|
protected $appid; //appid
|
|
|
|
/**
|
|
* 获取必要信息
|
|
* @param $param
|
|
* [
|
|
* merchant_id
|
|
* user_type 1普通商户类型 2服务商类型
|
|
* type 1小程序 2公众号
|
|
* ]
|
|
*/
|
|
public function getWxInfo($param)
|
|
{
|
|
$merchant_id = $param['merchant_id'];
|
|
$type = $param['type'];
|
|
$user_type = $param['user_type'];
|
|
$paySetModel = new RetailPaySetModel();
|
|
$paySet = $paySetModel->getFindMerchantPaySet($merchant_id);
|
|
switch ($user_type) {
|
|
case 1:
|
|
//普通支付模式
|
|
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->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['coupon_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=4&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/PayCoupon/{$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重启
|
|
* ]
|
|
* @return mixed
|
|
*/
|
|
public function suspendRestart($param)
|
|
{
|
|
if (empty($param['operation_type']) || !in_array($param['operation_type'], [1, 2])) {
|
|
return callBack(statusCode()['error'], "未知请求源");
|
|
}
|
|
switch ($param['operation_type']) {
|
|
case 1:
|
|
//暂停
|
|
$verify = $this->commonVerify($param, 3);
|
|
break;
|
|
case 2:
|
|
//重启
|
|
$verify = $this->commonVerify($param, 4);
|
|
break;
|
|
}
|
|
if ($verify['status'] != 1) {
|
|
return $verify;
|
|
}
|
|
$coupon = $verify['data']['coupon']; //代金券信息
|
|
try {
|
|
$this->getWxInfo([
|
|
'merchant_id' => $param['merchant_id'],
|
|
'type' => $coupon['type'],
|
|
'user_type' => $coupon['user_type']
|
|
]); //必要信息
|
|
$data = [
|
|
'mch_id' => $this->mch_id,
|
|
'serial_no' => $this->serial_no,
|
|
'apiclient_key' => $this->apiclient_key,
|
|
'stock_id' => $coupon['stock_id'],
|
|
'data' => [
|
|
'stock_creator_mchid' => $coupon['mch_id'],
|
|
]
|
|
];
|
|
$wxPayV3Class = new WxPayV3($data);
|
|
switch ($param['operation_type']) {
|
|
case 1:
|
|
$result = $wxPayV3Class->suspend();
|
|
break;
|
|
case 2:
|
|
$result = $wxPayV3Class->restart();
|
|
break;
|
|
}
|
|
$result = json_decode($result, true);
|
|
if (!empty($result['stock_id'])) {
|
|
switch ($param['operation_type']) {
|
|
case 1:
|
|
$save['suspend_status'] = 2;
|
|
break;
|
|
case 2:
|
|
$save['suspend_status'] = 1;
|
|
break;
|
|
}
|
|
$payCouponModel = new PayCouponModel();
|
|
$payCouponModel->updateOrder([
|
|
'id' => $param['coupon_id']
|
|
], $save);
|
|
return callBack(statusCode()['success'], "操作成功");
|
|
} else {
|
|
return callBack(statusCode()['success'], $result['message']);
|
|
}
|
|
} catch (\Exception $e) {
|
|
return callBack(statusCode()['success'], "操作失败");
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* 发放代金券
|
|
* @param $param
|
|
* [
|
|
* merchant_id
|
|
* coupon_id 代金券id
|
|
* openid 用户openid
|
|
* service_openid 用户在服务商公众号的openid
|
|
* ordinary_openid 商户公众号的openid
|
|
* operator_id 操作人id
|
|
* uid 小程序用户id
|
|
* vip_id 会员id
|
|
* receive_store_id 领取门店
|
|
* ]
|
|
* @return mixed
|
|
*/
|
|
public function grantCoupon($param)
|
|
{
|
|
$verify = $this->grantCouponVerify($param);
|
|
if ($verify['status'] != 1) {
|
|
return $verify;
|
|
}
|
|
$coupon = $verify['data']['coupon']; //代金券信息
|
|
$this->getWxInfo([
|
|
'merchant_id' => $param['merchant_id'],
|
|
'type' => $coupon['type'],
|
|
'user_type' => $coupon['user_type']
|
|
]);
|
|
try {
|
|
$data = [
|
|
'serial_no' => $this->serial_no,
|
|
'apiclient_key' => $this->apiclient_key,
|
|
'data' => [
|
|
'stock_id' => $coupon['stock_id'],
|
|
'out_request_no' => createOrderTradeNo($param['merchant_id']),
|
|
'appid' => $this->appid,
|
|
'stock_creator_mchid' => $coupon['mch_id'],
|
|
]
|
|
];
|
|
switch ($coupon['user_type']) {
|
|
case 1:
|
|
//普通支付模式
|
|
switch ($coupon['type']) {
|
|
case 1:
|
|
//小程序
|
|
$data['openid'] = $param['openid'];
|
|
break;
|
|
case 2:
|
|
//公众号
|
|
$data['openid'] = $param['ordinary_openid'];
|
|
break;
|
|
}
|
|
break;
|
|
case 2:
|
|
//服务商模式
|
|
$data['openid'] = $param['service_openid'];
|
|
break;
|
|
}
|
|
$data['mch_id'] = $this->mch_id;
|
|
$WxPayV3Class = new WxPayV3($data);
|
|
$result = $WxPayV3Class->grant();
|
|
$result = json_decode($result, true);
|
|
if (!empty($result['coupon_id'])) {
|
|
$payCouponRecordModel = new PayCouponRecordModel();
|
|
$where = [
|
|
'merchant_id' => $param['merchant_id'],
|
|
'user_coupon_id' => $result['coupon_id'],
|
|
[
|
|
'openid' => $param['openid'],
|
|
'uid' => $param['uid'],
|
|
'_logic' => "OR"
|
|
]
|
|
];
|
|
$record = $payCouponRecordModel->getFind($where);
|
|
if (!empty($record)) {
|
|
return callBack(statusCode()['error'], "已发放成功请勿重复操作");
|
|
} else {
|
|
$coupon_use_rule = json_decode($coupon['coupon_use_rule'], true);
|
|
$content = [
|
|
'merchant_id' => $param['merchant_id'],
|
|
'coupon_id' => $param['coupon_id'],
|
|
'operator_id' => $param['operator_id'] ? $param['operator_id'] : 0,
|
|
'user_coupon_id' => $result['coupon_id'],
|
|
'stock_id' => $coupon['stock_id'],
|
|
'stock_name' => $coupon['stock_name'],
|
|
'stock_type' => $coupon['stock_type'],
|
|
'user_type' => $coupon['user_type'],
|
|
'type' => $coupon['type'],
|
|
'add_time' => time(),
|
|
'uid' => $param['uid'],
|
|
'vip_id' => $param['vip_id'],
|
|
'openid' => $param['openid'],
|
|
'receive_store_id' => $param['receive_store_id'] ? $param['receive_store_id'] : 0,
|
|
'coupon_fee' => $coupon_use_rule['fixed_normal_coupon']['coupon_amount']
|
|
];
|
|
$record_id = $payCouponRecordModel->addOrder($content);
|
|
$PayCouponModel = new PayCouponModel();
|
|
$PayCouponModel->toSetDec(['id' => $param['coupon_id']], "operation_num", 1);
|
|
if (!empty($param['vip_id'])) {
|
|
$arr['starttime'] = $coupon['available_begin_time'];
|
|
$arr['endtime'] = $coupon['available_end_time'];
|
|
$arr['coupon_name'] = $coupon['stock_name'];
|
|
$Mark = new MarkController();
|
|
$Mark->payCoupon($param['vip_id'], "", "", $arr);
|
|
}
|
|
if (!empty($record_id)) {
|
|
return callBack(statusCode()['success'], "发放成功");
|
|
} else {
|
|
return callBack(statusCode()['success'], "发放成功,记录添加失败");
|
|
}
|
|
}
|
|
} else {
|
|
return callBack(statusCode()['error'], $result['message']);
|
|
}
|
|
} catch (\Exception $e) {
|
|
return callBack(statusCode()['error'], "发放失败");
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* 发放代金券的验证
|
|
* @param $param
|
|
* [
|
|
* merchant_id
|
|
* coupon_id
|
|
* ]
|
|
* @return mixed
|
|
*/
|
|
public function grantCouponVerify($param)
|
|
{
|
|
if (empty($param['merchant_id']) || empty($param['coupon_id'])) {
|
|
return callBack(statusCode()['error'], "缺少参数");
|
|
}
|
|
$result = $this->commonVerify($param, 2);
|
|
return $result;
|
|
}
|
|
|
|
|
|
/**
|
|
* 激活代金券
|
|
* @param $param
|
|
* [
|
|
* id
|
|
* merchant_id
|
|
* ]
|
|
* @return mixed
|
|
*/
|
|
public function couponActivation($param)
|
|
{
|
|
$verify = $this->couponActivationVerify($param);
|
|
if ($verify['status'] != 1) {
|
|
return $verify;
|
|
}
|
|
try {
|
|
$coupon = $verify['data']['coupon'];
|
|
$stock_id = $coupon['stock_id'];
|
|
$stock_creator_mchid = $coupon['mch_id'];
|
|
$this->getWxInfo(['merchant_id' => $param['merchant_id'], 'type' => $coupon['type'], 'user_type' => $coupon['user_type']]);
|
|
$data = [
|
|
'mch_id' => $this->mch_id,
|
|
'serial_no' => $this->serial_no,
|
|
'apiclient_key' => $this->apiclient_key,
|
|
'stock_id' => $stock_id,
|
|
'data' => [
|
|
'stock_creator_mchid' => $stock_creator_mchid
|
|
]
|
|
];
|
|
$wxPayV3Class = new WxPayV3($data);
|
|
$result = $wxPayV3Class->activateCoupon();
|
|
$result = json_decode($result, true);
|
|
if (!empty($result['stock_id'])) {
|
|
$payCouponModel = new PayCouponModel();
|
|
$payCouponModel->updateOrder([
|
|
'id' => $param['id'],
|
|
], [
|
|
'activate_status' => 1,
|
|
'activate_time' => strtotime($result['start_time'])
|
|
]);
|
|
return callBack(statusCode()['success'], "激活成功");
|
|
} else {
|
|
return callBack(statusCode()['success'], $result['message']);
|
|
}
|
|
} catch (\Exception $e) {
|
|
return callBack(statusCode()['success'], "激活失败");
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* 激活代金券验证
|
|
* @param $param
|
|
* [
|
|
* id
|
|
* merchant_id
|
|
* ]
|
|
* @return mixed
|
|
*/
|
|
public function couponActivationVerify($param)
|
|
{
|
|
if (empty($param['id']) || empty($param['merchant_id'])) {
|
|
return callBack(statusCode()['error'], '缺少参数');
|
|
}
|
|
$param['coupon_id'] = $param['id'];
|
|
$result = $this->commonVerify($param, 1);
|
|
return $result;
|
|
}
|
|
|
|
|
|
/**
|
|
* 公共参数验证
|
|
* @param $param
|
|
* [
|
|
* merchant_id
|
|
* coupon_id 代金券主键id
|
|
* ]
|
|
* @param $type 1激活验证 2发放 3暂停 4重启
|
|
* @return mixed
|
|
*/
|
|
public function commonVerify($param, $type)
|
|
{
|
|
if (empty($param['coupon_id']) || empty($param['merchant_id'])) {
|
|
return callBack(statusCode()['error'], "缺少必要参数");
|
|
}
|
|
try {
|
|
$payCouponModel = new PayCouponModel();
|
|
$coupon = $payCouponModel->getFind([
|
|
'id' => $param['coupon_id'],
|
|
'merchant_id' => $param['merchant_id'],
|
|
]);
|
|
} catch (\Exception $e) {
|
|
return callBack(statusCode()['error'], '未找到该代金券');
|
|
}
|
|
if (empty($coupon)) {
|
|
return callBack(statusCode()['error'], '未找到该代金券');
|
|
}
|
|
if ($coupon['status'] == 2) {
|
|
return callBack(statusCode()['error'], '该代金券已删除');
|
|
}
|
|
switch ($type) {
|
|
case 1:
|
|
//激活验证
|
|
if ($coupon['activate_status'] == 1) {
|
|
return callBack(statusCode()['error'], '该代金券已激活');
|
|
}
|
|
break;
|
|
case 2:
|
|
//发放
|
|
if ($coupon['activate_status'] != 1) {
|
|
return callBack(statusCode()['error'], '该代金券未激活');
|
|
}
|
|
if ($coupon['suspend_status'] != 1) {
|
|
return callBack(statusCode()['error'], '该代金券已暂停发放');
|
|
}
|
|
if ($coupon['operation_num'] <= 0) {
|
|
return callBack(statusCode()['error'], '该代金券暂无库存');
|
|
}
|
|
break;
|
|
case 3:
|
|
//暂停
|
|
if ($coupon['suspend_status'] == 2) {
|
|
return callBack(statusCode()['error'], '该代金券已暂停发放');
|
|
}
|
|
break;
|
|
case 4:
|
|
//重启
|
|
if ($coupon['suspend_status'] == 1) {
|
|
return callBack(statusCode()['error'], '该代金券重启');
|
|
}
|
|
break;
|
|
default:
|
|
return callBack(statusCode()['error'], "未知验证源");
|
|
break;
|
|
}
|
|
return callBack(statusCode()['success'], "验证成功", ['coupon' => $coupon]);
|
|
}
|
|
|
|
|
|
/**
|
|
* 图片上传
|
|
* @param $param
|
|
* @return array
|
|
*/
|
|
public function wxUpload($param)
|
|
{
|
|
$this->getWxInfo([
|
|
'merchant_id' => $param['merchant_id'],
|
|
'type' => 1,
|
|
'user_type' => 1
|
|
]);
|
|
$wxUploadClass = new WxUpload();
|
|
$result = $wxUploadClass->wxUpload([
|
|
'image' => $_FILES['label_img'],
|
|
'mch_id' => $this->mch_id,
|
|
'serial_no' => $this->serial_no,
|
|
'apiclient_key' => $this->apiclient_key,
|
|
]);
|
|
return $result;
|
|
}
|
|
|
|
|
|
/**
|
|
* 创建代金券
|
|
* @param $param
|
|
* [
|
|
* merchant_id
|
|
* type 1小程序 2公众号
|
|
* data=>[
|
|
* stock_name 批次名称必填
|
|
* available_begin_time 开始时间必选
|
|
* available_end_time 结束时间必选
|
|
* stock_type 批次类型 NORMAL 固定面额满减券使用规则 DISCOUNT 折扣券使用规则 EXCHANGE 换购券使用规则
|
|
* coupon_use_rule=>[
|
|
* available_items 可核销商品编码
|
|
* unavailable_items 不参与优惠商品编码
|
|
* ],
|
|
* stock_use_rule=>[
|
|
* max_coupons 请设置最大发券数
|
|
* max_amount 请设置总消耗金额
|
|
* max_coupons_per_user 单个用户可领个数,每个用户最多60张券
|
|
* ],
|
|
* fixed_normal_coupon=>[ NORMAL
|
|
* coupon_amount 请填写满减面额
|
|
* transaction_minimum 请填写满减门槛
|
|
* ],
|
|
* disscount_coupon=>[
|
|
* discount_amount_max 请填写最高折扣金额
|
|
* discount_percent 请填写折扣百分比
|
|
* ]
|
|
* ]
|
|
* ]
|
|
* @return mixed
|
|
*/
|
|
public function createCoupon($param)
|
|
{
|
|
try {
|
|
$this->getWxInfo([
|
|
'merchant_id' => $param['merchant_id'],
|
|
'type' => $param['data']['type'],
|
|
'user_type' => $param['data']['user_type']
|
|
]);
|
|
$verify = $this->createCouponVerify($param); //参数验证
|
|
if ($verify['status'] != 1) return $verify;
|
|
$add_data = $param;
|
|
$param_new = $this->createCouponReplace($param); //参数替换
|
|
switch ($param['data']['user_type']) {
|
|
case 1:
|
|
//普通模式
|
|
$param_new['coupon_use_rule']['available_merchants'] = [$this->mch_id];//可核销商户号
|
|
break;
|
|
case 2:
|
|
$param_new['coupon_use_rule']['available_merchants'] = [$this->son_mch_id];//可核销商户号
|
|
break;
|
|
}
|
|
$data = [
|
|
'mch_id' => $this->mch_id,
|
|
'serial_no' => $this->serial_no,
|
|
'apiclient_key' => $this->apiclient_key,
|
|
'data' => $param_new
|
|
];
|
|
$wxPay = new WxPayV3($data);
|
|
$result = $wxPay->createCoupon();
|
|
file_put_contents('./pay_coupon.txt', $result . PHP_EOL, FILE_APPEND);
|
|
$result = json_decode($result, true);
|
|
if (!empty($result['stock_id'])) {
|
|
$add_data['stock_id'] = $result['stock_id'];
|
|
$add_data['merchant_id'] = $param['merchant_id'];
|
|
$add_data['out_request_no'] = $param_new['out_request_no'];
|
|
$add_data['goods_tag'] = $param_new['coupon_use_rule']['goods_tag'];
|
|
$result = $this->payCouponAdd($add_data);
|
|
if ($result['status'] == 1) {
|
|
return callBack(statusCode()['success'], "创建成功");
|
|
} else {
|
|
return callBack(statusCode()['error'], "创建成功,记录失败");
|
|
}
|
|
} else {
|
|
return callBack(statusCode()['error'], $result['message']);
|
|
}
|
|
} catch (\Exception $e) {
|
|
return callBack(statusCode()['error'], "创建失败");
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* 创建代金券记录表 payCouponAdd
|
|
* @param $param
|
|
* [
|
|
* merchant_id
|
|
* store_id
|
|
* data=>[
|
|
* stock_name 批次名称必填
|
|
* available_begin_time 开始时间必选
|
|
* available_end_time 结束时间必选
|
|
* stock_type 批次类型 NORMAL 固定面额满减券使用规则 DISCOUNT 折扣券使用规则 EXCHANGE 换购券使用规则
|
|
* coupon_use_rule=>[
|
|
* available_items 可核销商品编码
|
|
* unavailable_items 不参与优惠商品编码
|
|
* ],
|
|
* stock_use_rule=>[
|
|
* max_coupons 请设置最大发券数
|
|
* max_amount 请设置总消耗金额
|
|
* max_coupons_per_user 单个用户可领个数,每个用户最多60张券
|
|
* ],
|
|
* fixed_normal_coupon=>[ NORMAL
|
|
* coupon_amount 请填写满减面额
|
|
* transaction_minimum 请填写满减门槛
|
|
* ],
|
|
* disscount_coupon=>[
|
|
* discount_amount_max 请填写最高折扣金额
|
|
* discount_percent 请填写折扣百分比
|
|
* ]
|
|
* ]
|
|
* ]
|
|
* @return mixed
|
|
*/
|
|
public function payCouponAdd($param)
|
|
{
|
|
$data = $param['data'];
|
|
$content = [
|
|
'merchant_id' => $param['merchant_id'],
|
|
'stock_name' => $data['stock_name'],
|
|
'comment' => $data['comment'],
|
|
'belong_merchant' => $this->mch_id,
|
|
'available_begin_time' => strtotime($data['available_time'][0]),
|
|
'available_end_time' => strtotime($data['available_time'][1]),
|
|
'stock_use_rule' => json_encode($data['stock_use_rule']),
|
|
'pattern_info' => json_encode($data['pattern_info']),
|
|
'coupon_use_rule' => json_encode($data['coupon_use_rule']),
|
|
'no_cash' => $data['no_cash'],
|
|
'stock_type' => $data['stock_type'],
|
|
'out_request_no' => $param['out_request_no'],
|
|
'ext_info' => '',
|
|
'stock_id' => $param['stock_id'],
|
|
'create_time' => time(),
|
|
'mch_id' => $this->mch_id,
|
|
'valid_time_type' => $data['valid_time_type'],
|
|
'coupon_type' => $data['coupon_type'],
|
|
'appoint_type' => $data['appoint_type'] ? $data['appoint_type'] : 1,
|
|
'operator_id' => $param['operator_id'],
|
|
'type' => $data['type'],
|
|
'user_type' => $data['user_type'],
|
|
'operation_num' => $data['stock_use_rule']['max_coupons']
|
|
];
|
|
if (!empty($param['goods_tag'])) {
|
|
$content['goods_tag'] = implode(',', $param['goods_tag']);
|
|
}
|
|
if (!empty($data['coupon_use_rule']['goodsList'])) {
|
|
$content['goods_id_gather'] = implode(',', array_column($data['coupon_use_rule']['goodsList'], "goods_id"));
|
|
$content['store_id_gather'] = implode(',', array_unique(array_column($data['coupon_use_rule']['goodsList'], "store_id")));
|
|
if ($data['appoint_type'] == 2) {
|
|
$content['goods_code_str'] = implode(',', array_unique(array_column($data['coupon_use_rule']['goodsList'], 'goods_code')));
|
|
}
|
|
}
|
|
try {
|
|
$payCouponModel = new PayCouponModel();
|
|
$couponId = $payCouponModel->addOrder($content);
|
|
} catch (\Exception $e) {
|
|
return callBack(statusCode()['error'], '添加失败');
|
|
}
|
|
if ($couponId) {
|
|
return callBack(statusCode()['success'], '添加成功');
|
|
} else {
|
|
return callBack(statusCode()['error'], '添加失败');
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* 创建代金券参数替换
|
|
* @param $param
|
|
* [
|
|
* merchant_id
|
|
* store_id
|
|
* data=>[
|
|
* stock_name 批次名称必填
|
|
* available_begin_time 开始时间必选
|
|
* available_end_time 结束时间必选
|
|
* stock_type 批次类型 NORMAL 固定面额满减券使用规则 DISCOUNT 折扣券使用规则 EXCHANGE 换购券使用规则
|
|
* coupon_use_rule=>[
|
|
* available_items 可核销商品编码
|
|
* unavailable_items 不参与优惠商品编码
|
|
* ],
|
|
* stock_use_rule=>[
|
|
* max_coupons 请设置最大发券数
|
|
* max_amount 请设置总消耗金额
|
|
* max_coupons_per_user 单个用户可领个数,每个用户最多60张券
|
|
* ],
|
|
* fixed_normal_coupon=>[ NORMAL
|
|
* coupon_amount 请填写满减面额
|
|
* transaction_minimum 请填写满减门槛
|
|
* ],
|
|
* disscount_coupon=>[
|
|
* discount_amount_max 请填写最高折扣金额
|
|
* discount_percent 请填写折扣百分比
|
|
* ]
|
|
* ]
|
|
* ]
|
|
* @return mixed
|
|
*/
|
|
public function createCouponReplace($param)
|
|
{
|
|
$data = $param['data'];
|
|
if ($data['no_cash'] == 1) {
|
|
$data['no_cash'] = true;
|
|
} else {
|
|
$data['no_cash'] = false;
|
|
}
|
|
$data['available_begin_time'] = $data['available_time'][0] . '.120+08:00';
|
|
$data['available_end_time'] = $data['available_time'][1] . '.120+08:00';
|
|
switch ($data['stock_type']) {
|
|
case 1:
|
|
//NORMAL
|
|
$data['stock_type'] = 'NORMAL';
|
|
$data['coupon_use_rule']['fixed_normal_coupon']['coupon_amount'] = intval($data['coupon_use_rule']['fixed_normal_coupon']['coupon_amount'] * 100);
|
|
$data['coupon_use_rule']['fixed_normal_coupon']['transaction_minimum'] = intval($data['coupon_use_rule']['fixed_normal_coupon']['transaction_minimum'] * 100);
|
|
unset($data['coupon_use_rule']['disscount_coupon']);
|
|
break;
|
|
case 2:
|
|
//DISCOUNT
|
|
$data['stock_type'] = 'DISCOUNT';
|
|
$data['coupon_use_rule']['disscount_coupon']['discount_amount_max'] = floatval($data['coupon_use_rule']['disscount_coupon']['discount_amount_max'] * 100);
|
|
$data['coupon_use_rule']['disscount_coupon']['discount_percent'] = intval($data['coupon_use_rule']['disscount_coupon']['discount_percent']);
|
|
unset($data['coupon_use_rule']['fixed_normal_coupon']);
|
|
if (!empty($data['coupon_use_rule']['disscount_coupon']['transaction_minimum'])) {
|
|
$data['coupon_use_rule']['disscount_coupon']['transaction_minimum'] = intval($data['coupon_use_rule']['disscount_coupon']['transaction_minimum'] * 100);
|
|
} else {
|
|
unset($data['coupon_use_rule']['disscount_coupon']['transaction_minimum']);
|
|
}
|
|
break;
|
|
}
|
|
switch ($data['coupon_type']) {
|
|
case 2:
|
|
//单品
|
|
if (!empty($data['coupon_use_rule']['goodsList'])) {
|
|
$available_items = []; //商品编码集合
|
|
switch ($data['appoint_type']) {
|
|
case 1:
|
|
//指定商品id
|
|
foreach ($data['coupon_use_rule']['goodsList'] as $val) {
|
|
$available_items[] = 'S' . $val['store_id'] . 'G' . $val['goods_id'];
|
|
}
|
|
$data['coupon_use_rule']['goods_tag'] = [$param['merchant_id']]; //商户id作为优惠标识
|
|
break;
|
|
case 2:
|
|
//指定商品条码
|
|
foreach ($data['coupon_use_rule']['goodsList'] as $val) {
|
|
$available_items[] = $val['goods_code'];
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
$data['coupon_use_rule']['available_items'] = $available_items;
|
|
break;
|
|
}
|
|
if (in_array($data['valid_time_type'], [1, 2, 3])) {
|
|
//时间
|
|
switch ($data['valid_time_type']) {
|
|
case 1:
|
|
$time = strtotime(date('Y-m-d', time()));
|
|
$begin_time = strtotime($data['coupon_use_rule']['coupon_available_time']['fix_available_time']['begin_time']) - $time;
|
|
$end_time = strtotime($data['coupon_use_rule']['coupon_available_time']['fix_available_time']['end_time']) - $time;
|
|
$data['coupon_use_rule']['coupon_available_time']['fix_available_time']['begin_time'] = intval($begin_time);
|
|
$data['coupon_use_rule']['coupon_available_time']['fix_available_time']['end_time'] = intval($end_time);
|
|
if (!empty($data['coupon_use_rule']['coupon_available_time']['fix_available_time']['available_week_day'])) {
|
|
|
|
$week_day = array_map('int', $data['coupon_use_rule']['coupon_available_time']['fix_available_time']['available_week_day']);
|
|
unset($data['coupon_use_rule']['coupon_available_time']['fix_available_time']['available_week_day']);
|
|
$data['coupon_use_rule']['coupon_available_time']['fix_available_time']['available_week_day'] = $week_day;
|
|
} else {
|
|
unset($data['coupon_use_rule']['coupon_available_time']['fix_available_time']['available_week_day']);
|
|
}
|
|
unset($data['coupon_use_rule']['coupon_available_time']['second_day_available']);
|
|
unset($data['coupon_use_rule']['coupon_available_time']['available_time_after_receive']);
|
|
unset($data['coupon_use_rule']['coupon_available_time']['fix_available_time']['valid_time']);
|
|
break;
|
|
case 2:
|
|
unset($data['coupon_use_rule']['coupon_available_time']['fix_available_time']);
|
|
unset($data['coupon_use_rule']['coupon_available_time']['available_time_after_receive']);
|
|
break;
|
|
case 3:
|
|
$data['coupon_use_rule']['coupon_available_time']['available_time_after_receive'] = intval($data['coupon_use_rule']['coupon_available_time']['available_time_after_receive']);
|
|
unset($data['coupon_use_rule']['coupon_available_time']['fix_available_time']);
|
|
unset($data['coupon_use_rule']['coupon_available_time']['second_day_available']);
|
|
break;
|
|
}
|
|
} else {
|
|
unset($data['coupon_use_rule']['coupon_available_time']);
|
|
}
|
|
if (empty($data['pattern_info']['merchant_name'])) {
|
|
unset($data['pattern_info']);
|
|
}
|
|
$data['stock_use_rule']['max_coupons'] = intval($data['stock_use_rule']['max_coupons']);
|
|
$data['stock_use_rule']['max_amount'] = intval($data['stock_use_rule']['max_amount'] * 100);
|
|
$data['stock_use_rule']['max_coupons_per_user'] = intval($data['stock_use_rule']['max_coupons_per_user']);
|
|
$data['belong_merchant'] = $this->mch_id;//批次归属商户号
|
|
$data['out_request_no'] = createOrderTradeNo($param['merchant_id']);//随机字符串
|
|
unset($data['user_type']);
|
|
unset($data['appoint_type']);
|
|
unset($data['type']);
|
|
unset($data['coupon_type']);
|
|
unset($data['available_time']);
|
|
unset($data['valid_time_type']);
|
|
unset($data['coupon_use_rule']['goodsList']);
|
|
return $data;
|
|
}
|
|
|
|
|
|
/**
|
|
* 代金券参数验证
|
|
* @param $param
|
|
* [
|
|
* merchant_id
|
|
* type 1小程序 2公众号
|
|
* data=>[
|
|
* stock_name 批次名称必填
|
|
* available_begin_time 开始时间必选
|
|
* available_end_time 结束时间必选
|
|
* stock_type 批次类型 NORMAL 固定面额满减券使用规则 DISCOUNT 折扣券使用规则 EXCHANGE 换购券使用规则
|
|
* coupon_use_rule=>[
|
|
* available_items 可核销商品编码
|
|
* unavailable_items 不参与优惠商品编码
|
|
* ],
|
|
* stock_use_rule=>[
|
|
* max_coupons 请设置最大发券数
|
|
* max_amount 请设置总消耗金额
|
|
* max_coupons_per_user 单个用户可领个数,每个用户最多60张券
|
|
* ],
|
|
* fixed_normal_coupon=>[ NORMAL
|
|
* coupon_amount 请填写满减面额
|
|
* transaction_minimum 请填写满减门槛
|
|
* ],
|
|
* disscount_coupon=>[
|
|
* discount_amount_max 请填写最高折扣金额
|
|
* discount_percent 请填写折扣百分比
|
|
* ]
|
|
* ]
|
|
* ]
|
|
* @return array
|
|
* [
|
|
* status
|
|
* message
|
|
* ]
|
|
*/
|
|
public function createCouponVerify($param)
|
|
{
|
|
$reg = "/^\+?[0-9][0-9]*$/";
|
|
$reg1 = "/^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d{0,2})$/";
|
|
$time = strtotime(date("Y-m-d", time()));
|
|
$data = $param['data'];
|
|
if (empty($data['user_type'])) {
|
|
return callBack(statusCode()['error'], "请选择支付模式");
|
|
}
|
|
if ($data['user_type'] == 1 && empty($data['type'])) {
|
|
return callBack(statusCode()['error'], "请选择普通支付模式的场景");
|
|
}
|
|
if ($data['user_type'] == 2 && $data['no_cash'] == 2) {
|
|
return callBack(statusCode()['error'], "服务商模式下暂不支付创建预充值类型");
|
|
}
|
|
if (empty($this->mch_id) || empty($this->serial_no) || empty($this->apiclient_key)) {
|
|
return callBack(statusCode()['error'], "商户号,Api序列号,或私钥证书有误");
|
|
}
|
|
if (empty($param['merchant_id']) || empty($data)) {
|
|
return callBack(statusCode()['error'], "缺少必要参数");
|
|
}
|
|
if (empty($data['stock_name']) || strlen($data['stock_name']) > 20) {
|
|
return callBack(statusCode()['error'], "批次名称必填且不能大于20个字符");
|
|
}
|
|
if (empty($data['available_time'])) {
|
|
return callBack(statusCode()['error'], "有效时间必选");
|
|
}
|
|
if (empty($data['stock_use_rule']['max_coupons']) || $data['stock_use_rule']['max_coupons'] < 0 || !preg_match($reg, $data['stock_use_rule']['max_coupons'])) {
|
|
return callBack(statusCode()['error'], "请设置正确最大发券数");
|
|
}
|
|
if (empty($data['stock_use_rule']['max_amount']) || !preg_match($reg1, $data['stock_use_rule']['max_amount']) || $data['stock_use_rule']['max_amount'] < 0
|
|
|| ($data['stock_type'] == 1 && (($data['stock_use_rule']['max_amount'] * 100) != ($data['stock_use_rule']['max_coupons'] * ($data['coupon_use_rule']['fixed_normal_coupon']['coupon_amount'] * 100))))) {
|
|
return callBack(statusCode()['error'], "请设置正确总发放金额");
|
|
}
|
|
if (empty($data['stock_use_rule']['max_coupons_per_user']) || $data['stock_use_rule']['max_coupons_per_user'] > 60) {
|
|
return callBack(statusCode()['error'], "单个用户可领个数,每个用户最多60张券");
|
|
}
|
|
if (!in_array($data['stock_type'], [1, 2, 3])) return callBack(statusCode()['error'], "请选择代金券类型");
|
|
switch ($data['stock_type']) {
|
|
case 1:
|
|
if (empty($data['coupon_use_rule']['fixed_normal_coupon']['coupon_amount'])
|
|
|| $data['coupon_use_rule']['fixed_normal_coupon']['coupon_amount'] <= 0.01
|
|
|| !preg_match($reg1, $data['coupon_use_rule']['fixed_normal_coupon']['coupon_amount'])) {
|
|
return callBack(statusCode()['error'], "请填写满减面额,最低1元");
|
|
}
|
|
if (empty($data['coupon_use_rule']['fixed_normal_coupon']['transaction_minimum'])
|
|
|| $data['coupon_use_rule']['fixed_normal_coupon']['transaction_minimum'] < $data['coupon_use_rule']['fixed_normal_coupon']['coupon_amount']
|
|
|| !preg_match($reg1, $data['coupon_use_rule']['fixed_normal_coupon']['transaction_minimum'])) {
|
|
return callBack(statusCode()['error'], "请填写正确满减门槛,必须大于满减面额");
|
|
}
|
|
break;
|
|
case 2:
|
|
if (empty($data['coupon_use_rule']['disscount_coupon']['discount_amount_max'])
|
|
|| $data['coupon_use_rule']['disscount_coupon']['discount_amount_max'] < 0
|
|
|| !preg_match($reg1, $data['coupon_use_rule']['disscount_coupon']['discount_amount_max'])) {
|
|
return callBack(statusCode()['error'], "请填写正确折扣金额");
|
|
}
|
|
if (empty($data['coupon_use_rule']['disscount_coupon']['discount_percent'])
|
|
|| $data['coupon_use_rule']['disscount_coupon']['discount_percent'] < 0
|
|
|| !preg_match($reg, $data['coupon_use_rule']['disscount_coupon']['discount_percent'])) {
|
|
return callBack(statusCode()['error'], "请填写正确折扣");
|
|
}
|
|
break;
|
|
}
|
|
if ($data['valid_time_type'] == 1) {
|
|
if ((empty($data['coupon_use_rule']['coupon_available_time']['fix_available_time']['begin_time']) || empty($data['coupon_use_rule']['coupon_available_time']['fix_available_time']['end_time']))) {
|
|
return callBack(statusCode()['error'], "请选择生效时间的固定时间段");
|
|
}
|
|
if (strtotime($data['available_time'][0]) <= $time) {
|
|
return callBack(statusCode()['error'], "该类型的有效时间必须大于当前时间");
|
|
}
|
|
}
|
|
if ($data['valid_time_type'] == 2 && (strtotime(date('Y-m-d', strtotime($data['available_time'][0])))) <= $time) {
|
|
return callBack(statusCode()['error'], "该类型的有效时间必须大于当天");
|
|
}
|
|
if ($data['valid_time_type'] == 3 && (empty($data['coupon_use_rule']['coupon_available_time']['available_time_after_receive']) || $data['coupon_use_rule']['coupon_available_time']['available_time_after_receive'] < 30)) {
|
|
return callBack(statusCode()['error'], "请填写正确生效时间");
|
|
}
|
|
if (!empty($data['pattern_info']) && strlen($data['pattern_info']) > 128) {
|
|
return callBack(statusCode()['error'], "商户名称长度不能超过128字符");
|
|
}
|
|
switch ($data['coupon_type']) {
|
|
case 2:
|
|
//单品券
|
|
if (empty($data['coupon_use_rule']['goodsList'])) {
|
|
return callBack(statusCode()['error'], "请选择单品优惠的商品");
|
|
}
|
|
if (count($data['coupon_use_rule']['goodsList']) > 50) {
|
|
return callBack(statusCode()['error'], "单品优惠的商品最多50种");
|
|
}
|
|
if ($data['appoint_type'] == 2) {
|
|
$goodsList = $data['coupon_use_rule']['goodsList'];
|
|
foreach ($goodsList as $val) {
|
|
if (empty($val['goods_code'])) {
|
|
return callBack(statusCode()['error'], '商品条码有误');
|
|
}
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
return callBack(statusCode()['success'], "验证成功");
|
|
}
|
|
} |