272 lines
8.2 KiB
PHP
272 lines
8.2 KiB
PHP
<?php
|
|
|
|
namespace App\Facades\PingAn;
|
|
|
|
use App\Models\Coupon;
|
|
use App\Models\User;
|
|
use Carbon\Carbon;
|
|
|
|
class Partner extends PingAn
|
|
{
|
|
|
|
public function action()
|
|
{
|
|
$this->getToken();
|
|
return $this->getAll();
|
|
}
|
|
|
|
/**
|
|
* 校验平安券编号
|
|
* @param [type] $thirdPartyGoodsId 平安券规则 YSD-full100-10
|
|
* @param [type] $total 订单金额
|
|
* @param [type] $user 登陆的用户
|
|
* @return [type] [description]
|
|
*/
|
|
public function checkCode($thirdPartyGoodsId, $total, $user)
|
|
{
|
|
$code = $user->code()->where('code', $thirdPartyGoodsId)->first();
|
|
|
|
if (!$code) {
|
|
return "未找到此项平安券规则";
|
|
}
|
|
|
|
$ticket = explode('-', $thirdPartyGoodsId);
|
|
if (!is_array($ticket) || count($ticket) != 3) {
|
|
return "平安券规则格式不正确";
|
|
}
|
|
|
|
$full = $ticket[1]; //full100
|
|
$price = $ticket[2];
|
|
// preg_match('/(\d{3}(\.\d+)?)/is', $full, $result);
|
|
preg_match('/\d+/', $full, $result);
|
|
|
|
if (empty($result)) {
|
|
return "平安券规则格式不正确.";
|
|
}
|
|
if (!is_array($result)) {
|
|
return "平安券编号不正确";
|
|
}
|
|
|
|
if (!is_numeric($total)) {
|
|
return "订单金额必须是数字";
|
|
}
|
|
|
|
if ($result[0] > $total) {
|
|
return '订单金额不足,平安券编号不可使用。';
|
|
}
|
|
|
|
$res = [
|
|
'total' => $result[0],
|
|
'price' => $price,
|
|
'profit' => $code->profit,
|
|
];
|
|
|
|
return $res;
|
|
}
|
|
|
|
/**
|
|
* 通过兑换码查询平安券编号详情V2
|
|
* @param [type] $redemptionCode 平安券编号
|
|
* @param [type] $total [订单金额]
|
|
* @param [type] $user [用户模型]
|
|
* @param [type] $outletId [提交的网点id]
|
|
* @return [type] [description]
|
|
*/
|
|
public function coupondetail($redemptionCode, $total, $user, $outletId)
|
|
{
|
|
|
|
//查询网点是否存在
|
|
$info = User::where('outlet_id', $outletId)->first();
|
|
|
|
if (!$info) {
|
|
return '网点编号错误';
|
|
}
|
|
|
|
$url = $this->baseUri . 'partner/v2/coupondetail';
|
|
$params = [
|
|
'redemptionCode' => $redemptionCode,
|
|
'outletNo' => $info->PaOutletId,
|
|
'thirdOutletNo' => $info->outlet_id,
|
|
];
|
|
$res = $this->getPingAnData($url, $params);
|
|
if (!is_array($res)) {
|
|
return $res;
|
|
}
|
|
|
|
if ($res['code'] != 200) {
|
|
return $res['message'];
|
|
}
|
|
|
|
$coupon = $res['data'];
|
|
|
|
if ($coupon['status'] > 0) {
|
|
if (isset(config('pingan.coupon_status')[$coupon['status']])) {
|
|
return '平安券' . config('pingan.coupon_status')[$coupon['status']];
|
|
}
|
|
return '平安券编号不可用';
|
|
}
|
|
|
|
$startTime = Carbon::parse($coupon['startTime']);
|
|
$endTime = Carbon::parse($coupon['endTime']);
|
|
$now = now();
|
|
if ($startTime->gt($now)) {
|
|
return '平安券编号未开始使用';
|
|
}
|
|
|
|
if ($now->gt($endTime)) {
|
|
return '平安券编号已过期';
|
|
}
|
|
|
|
$pinganData = $this->getPinganProduct($coupon, $info);
|
|
|
|
if (is_string($pinganData)) {
|
|
return $pinganData;
|
|
}
|
|
|
|
$thirdPartyGoodsId = $pinganData['thirdPartyGoodsId'];
|
|
$productId = $pinganData['productId'];
|
|
$PaOutletId = $pinganData['PaOutletId'];
|
|
|
|
$ticket = $this->checkCode($thirdPartyGoodsId, $total, $user);
|
|
if (!is_array($ticket)) {
|
|
return $ticket;
|
|
}
|
|
|
|
$couponData = [
|
|
'user_id' => $user->id,
|
|
'outletId' => $outletId,
|
|
'productId' => $productId,
|
|
'PaOutletId' => $PaOutletId,
|
|
'redemptionCode' => $redemptionCode,
|
|
'thirdPartyGoodsId' => $thirdPartyGoodsId,
|
|
'couponName' => $coupon['couponName'],
|
|
'price' => $ticket['price'],
|
|
'total' => $total,
|
|
'profit' => $ticket['profit'],
|
|
'status' => $coupon['status'],
|
|
'startTime' => $coupon['startTime'],
|
|
'endTime' => $coupon['endTime'],
|
|
];
|
|
|
|
$couponModel = Coupon::create($couponData);
|
|
return $couponModel;
|
|
}
|
|
|
|
/**
|
|
* 查找平安商品id
|
|
* @author 玄尘 2020-04-04
|
|
* @param [type] $coupon 平安返回的券信息
|
|
* @param [type] $user 网点用户数据
|
|
* @return [type] [description]
|
|
*/
|
|
public function getPinganProduct($coupon, $user)
|
|
{
|
|
$PaOutletId = $user->PaOutletId;
|
|
$outlet_id = $user->outlet_id;
|
|
$profitOfferItemVersion = $coupon['profitOfferItemVersion'];
|
|
if (!$PaOutletId && $profitOfferItemVersion != 1) {
|
|
return '参数错误,缺少平安网点信息';
|
|
}
|
|
|
|
$productItemList = $coupon['productItemList'];
|
|
if (!is_array($productItemList) || !is_array($productItemList[0])) {
|
|
return '平安券编号数据有误';
|
|
}
|
|
|
|
//循环查找
|
|
foreach ($productItemList as $key => $item) {
|
|
$productId = $item['productId'];
|
|
$thirdPartyGoodsId = $item['thirdPartyGoodsId'];
|
|
$outletList = $item['outletList'];
|
|
if (!is_array($outletList) || !is_array($outletList[0])) {
|
|
return '网点信息有误!';
|
|
break;
|
|
}
|
|
|
|
$outletList = collect($outletList);
|
|
//判断是新版还是旧版
|
|
if ($profitOfferItemVersion) {
|
|
//新版通过第三方查询
|
|
$first = $outletList->firstWhere('thirdOutletNo', $outlet_id);
|
|
|
|
if ($first) {
|
|
|
|
break;
|
|
}
|
|
} else {
|
|
//旧版通过平安网点查询
|
|
$first = $outletList->firstWhere('outletNo', $PaOutletId);
|
|
if ($first) {
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!$thirdPartyGoodsId) {
|
|
return '平安券编号规则有误';
|
|
}
|
|
|
|
if (!$productId) {
|
|
return '商品id有误';
|
|
}
|
|
|
|
return [
|
|
'thirdPartyGoodsId' => $thirdPartyGoodsId,
|
|
'productId' => $productId,
|
|
'PaOutletId' => $first['outletNo'],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 通过兑换码核销卡卷(直接核销)
|
|
* @param [type] $redemptionCode 平安券编号码
|
|
* @return [type] [description]
|
|
*/
|
|
public function freezecoupon($coupon)
|
|
{
|
|
$params = [
|
|
'couponNo' => $coupon->redemptionCode,
|
|
'partnerOrderId' => date('ymdHis') . sprintf("%0" . strlen(999999) . "d", mt_rand(0, 999999)),
|
|
'outletId' => $coupon->PaOutletId,
|
|
'productId' => $coupon->productId,
|
|
'timestamp' => $this->getMsecTime(),
|
|
];
|
|
|
|
$url = $this->baseUri . 'partner/redemption';
|
|
$str = $this->encrypt($params);
|
|
$res = $this->getPingAnData($url, [], ['data' => $str]);
|
|
if (!is_array($res)) {
|
|
$coupon->remark = $res;
|
|
$coupon->status = 3;
|
|
$coupon->save();
|
|
return $res;
|
|
}
|
|
|
|
if ($res['code'] != 200) {
|
|
$coupon->remark = $res['code'] . '--' . $res['message'];
|
|
$coupon->status = 3;
|
|
$coupon->save();
|
|
|
|
return $res['message'];
|
|
}
|
|
|
|
$data = $res['data'];
|
|
|
|
$coupon->remark = $data['message'];
|
|
$coupon->partnerOrderId = $params['partnerOrderId'];
|
|
$coupon->status = ($data['status'] == 1) ? 2 : 3;
|
|
$coupon->save();
|
|
|
|
//返回的数据
|
|
$resdata = [
|
|
'price' => $coupon->price,
|
|
'name' => $coupon->couponName,
|
|
'total' => $coupon->total,
|
|
];
|
|
//核销成功 执行分润
|
|
$coupon->profit();
|
|
return $resdata;
|
|
}
|
|
|
|
}
|