This commit is contained in:
2023-10-31 14:11:47 +08:00
commit c4b2e4957b
14 changed files with 1016 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
.idea
/vendor/

19
composer.json Normal file
View File

@@ -0,0 +1,19 @@
{
"name": "xuanchen/card-coupon",
"description": "优惠券总模块",
"autoload": {
"psr-4": {
"XuanChen\\CardCoupon\\": "src/"
}
},
"require": {
"php": ">=7.4",
"guzzlehttp/guzzle": "^6.2 || ^7.0"
},
"authors": [
{
"name": "xuanchen",
"email": "122383162@qq.com"
}
]
}

485
composer.lock generated Normal file
View File

@@ -0,0 +1,485 @@
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "95db59cb7efec488d581da2cafacd03f",
"packages": [
{
"name": "guzzlehttp/guzzle",
"version": "7.8.0",
"dist": {
"type": "zip",
"url": "https://mirrors.cloud.tencent.com/repository/composer/guzzlehttp/guzzle/7.8.0/guzzlehttp-guzzle-7.8.0.zip",
"reference": "1110f66a6530a40fe7aea0378fe608ee2b2248f9",
"shasum": ""
},
"require": {
"ext-json": "*",
"guzzlehttp/promises": "^1.5.3 || ^2.0.1",
"guzzlehttp/psr7": "^1.9.1 || ^2.5.1",
"php": "^7.2.5 || ^8.0",
"psr/http-client": "^1.0",
"symfony/deprecation-contracts": "^2.2 || ^3.0"
},
"provide": {
"psr/http-client-implementation": "1.0"
},
"require-dev": {
"bamarni/composer-bin-plugin": "^1.8.1",
"ext-curl": "*",
"php-http/client-integration-tests": "dev-master#2c025848417c1135031fdf9c728ee53d0a7ceaee as 3.0.999",
"php-http/message-factory": "^1.1",
"phpunit/phpunit": "^8.5.29 || ^9.5.23",
"psr/log": "^1.1 || ^2.0 || ^3.0"
},
"suggest": {
"ext-curl": "Required for CURL handler support",
"ext-intl": "Required for Internationalized Domain Name (IDN) support",
"psr/log": "Required for using the Log middleware"
},
"type": "library",
"extra": {
"bamarni-bin": {
"bin-links": true,
"forward-command": false
}
},
"autoload": {
"files": [
"src/functions_include.php"
],
"psr-4": {
"GuzzleHttp\\": "src/"
}
},
"license": [
"MIT"
],
"authors": [
{
"name": "Graham Campbell",
"email": "hello@gjcampbell.co.uk",
"homepage": "https://github.com/GrahamCampbell"
},
{
"name": "Michael Dowling",
"email": "mtdowling@gmail.com",
"homepage": "https://github.com/mtdowling"
},
{
"name": "Jeremy Lindblom",
"email": "jeremeamia@gmail.com",
"homepage": "https://github.com/jeremeamia"
},
{
"name": "George Mponos",
"email": "gmponos@gmail.com",
"homepage": "https://github.com/gmponos"
},
{
"name": "Tobias Nyholm",
"email": "tobias.nyholm@gmail.com",
"homepage": "https://github.com/Nyholm"
},
{
"name": "Márk Sági-Kazár",
"email": "mark.sagikazar@gmail.com",
"homepage": "https://github.com/sagikazarmark"
},
{
"name": "Tobias Schultze",
"email": "webmaster@tubo-world.de",
"homepage": "https://github.com/Tobion"
}
],
"description": "Guzzle is a PHP HTTP client library",
"keywords": [
"client",
"curl",
"framework",
"http",
"http client",
"psr-18",
"psr-7",
"rest",
"web service"
],
"time": "2023-08-27T10:20:53+00:00"
},
{
"name": "guzzlehttp/promises",
"version": "2.0.1",
"dist": {
"type": "zip",
"url": "https://mirrors.cloud.tencent.com/repository/composer/guzzlehttp/promises/2.0.1/guzzlehttp-promises-2.0.1.zip",
"reference": "111166291a0f8130081195ac4556a5587d7f1b5d",
"shasum": ""
},
"require": {
"php": "^7.2.5 || ^8.0"
},
"require-dev": {
"bamarni/composer-bin-plugin": "^1.8.1",
"phpunit/phpunit": "^8.5.29 || ^9.5.23"
},
"type": "library",
"extra": {
"bamarni-bin": {
"bin-links": true,
"forward-command": false
}
},
"autoload": {
"psr-4": {
"GuzzleHttp\\Promise\\": "src/"
}
},
"license": [
"MIT"
],
"authors": [
{
"name": "Graham Campbell",
"email": "hello@gjcampbell.co.uk",
"homepage": "https://github.com/GrahamCampbell"
},
{
"name": "Michael Dowling",
"email": "mtdowling@gmail.com",
"homepage": "https://github.com/mtdowling"
},
{
"name": "Tobias Nyholm",
"email": "tobias.nyholm@gmail.com",
"homepage": "https://github.com/Nyholm"
},
{
"name": "Tobias Schultze",
"email": "webmaster@tubo-world.de",
"homepage": "https://github.com/Tobion"
}
],
"description": "Guzzle promises library",
"keywords": [
"promise"
],
"time": "2023-08-03T15:11:55+00:00"
},
{
"name": "guzzlehttp/psr7",
"version": "2.6.1",
"dist": {
"type": "zip",
"url": "https://mirrors.cloud.tencent.com/repository/composer/guzzlehttp/psr7/2.6.1/guzzlehttp-psr7-2.6.1.zip",
"reference": "be45764272e8873c72dbe3d2edcfdfcc3bc9f727",
"shasum": ""
},
"require": {
"php": "^7.2.5 || ^8.0",
"psr/http-factory": "^1.0",
"psr/http-message": "^1.1 || ^2.0",
"ralouphie/getallheaders": "^3.0"
},
"provide": {
"psr/http-factory-implementation": "1.0",
"psr/http-message-implementation": "1.0"
},
"require-dev": {
"bamarni/composer-bin-plugin": "^1.8.1",
"http-interop/http-factory-tests": "^0.9",
"phpunit/phpunit": "^8.5.29 || ^9.5.23"
},
"suggest": {
"laminas/laminas-httphandlerrunner": "Emit PSR-7 responses"
},
"type": "library",
"extra": {
"bamarni-bin": {
"bin-links": true,
"forward-command": false
}
},
"autoload": {
"psr-4": {
"GuzzleHttp\\Psr7\\": "src/"
}
},
"license": [
"MIT"
],
"authors": [
{
"name": "Graham Campbell",
"email": "hello@gjcampbell.co.uk",
"homepage": "https://github.com/GrahamCampbell"
},
{
"name": "Michael Dowling",
"email": "mtdowling@gmail.com",
"homepage": "https://github.com/mtdowling"
},
{
"name": "George Mponos",
"email": "gmponos@gmail.com",
"homepage": "https://github.com/gmponos"
},
{
"name": "Tobias Nyholm",
"email": "tobias.nyholm@gmail.com",
"homepage": "https://github.com/Nyholm"
},
{
"name": "Márk Sági-Kazár",
"email": "mark.sagikazar@gmail.com",
"homepage": "https://github.com/sagikazarmark"
},
{
"name": "Tobias Schultze",
"email": "webmaster@tubo-world.de",
"homepage": "https://github.com/Tobion"
},
{
"name": "Márk Sági-Kazár",
"email": "mark.sagikazar@gmail.com",
"homepage": "https://sagikazarmark.hu"
}
],
"description": "PSR-7 message implementation that also provides common utility methods",
"keywords": [
"http",
"message",
"psr-7",
"request",
"response",
"stream",
"uri",
"url"
],
"time": "2023-08-27T10:13:57+00:00"
},
{
"name": "psr/http-client",
"version": "1.0.3",
"dist": {
"type": "zip",
"url": "https://mirrors.cloud.tencent.com/repository/composer/psr/http-client/1.0.3/psr-http-client-1.0.3.zip",
"reference": "bb5906edc1c324c9a05aa0873d40117941e5fa90",
"shasum": ""
},
"require": {
"php": "^7.0 || ^8.0",
"psr/http-message": "^1.0 || ^2.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
},
"autoload": {
"psr-4": {
"Psr\\Http\\Client\\": "src/"
}
},
"license": [
"MIT"
],
"authors": [
{
"name": "PHP-FIG",
"homepage": "https://www.php-fig.org/"
}
],
"description": "Common interface for HTTP clients",
"homepage": "https://github.com/php-fig/http-client",
"keywords": [
"http",
"http-client",
"psr",
"psr-18"
],
"time": "2023-09-23T14:17:50+00:00"
},
{
"name": "psr/http-factory",
"version": "1.0.2",
"dist": {
"type": "zip",
"url": "https://mirrors.cloud.tencent.com/repository/composer/psr/http-factory/1.0.2/psr-http-factory-1.0.2.zip",
"reference": "e616d01114759c4c489f93b099585439f795fe35",
"shasum": ""
},
"require": {
"php": ">=7.0.0",
"psr/http-message": "^1.0 || ^2.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
},
"autoload": {
"psr-4": {
"Psr\\Http\\Message\\": "src/"
}
},
"license": [
"MIT"
],
"authors": [
{
"name": "PHP-FIG",
"homepage": "https://www.php-fig.org/"
}
],
"description": "Common interfaces for PSR-7 HTTP message factories",
"keywords": [
"factory",
"http",
"message",
"psr",
"psr-17",
"psr-7",
"request",
"response"
],
"time": "2023-04-10T20:10:41+00:00"
},
{
"name": "psr/http-message",
"version": "2.0",
"dist": {
"type": "zip",
"url": "https://mirrors.tencent.com/repository/composer/psr/http-message/2.0/psr-http-message-2.0.zip",
"reference": "402d35bcb92c70c026d1a6a9883f06b2ead23d71",
"shasum": ""
},
"require": {
"php": "^7.2 || ^8.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "2.0.x-dev"
}
},
"autoload": {
"psr-4": {
"Psr\\Http\\Message\\": "src/"
}
},
"license": [
"MIT"
],
"authors": [
{
"name": "PHP-FIG",
"homepage": "https://www.php-fig.org/"
}
],
"description": "Common interface for HTTP messages",
"homepage": "https://github.com/php-fig/http-message",
"keywords": [
"http",
"http-message",
"psr",
"psr-7",
"request",
"response"
],
"time": "2023-04-04T09:54:51+00:00"
},
{
"name": "ralouphie/getallheaders",
"version": "3.0.3",
"dist": {
"type": "zip",
"url": "https://mirrors.cloud.tencent.com/repository/composer/ralouphie/getallheaders/3.0.3/ralouphie-getallheaders-3.0.3.zip",
"reference": "120b605dfeb996808c31b6477290a714d356e822",
"shasum": ""
},
"require": {
"php": ">=5.6"
},
"require-dev": {
"php-coveralls/php-coveralls": "^2.1",
"phpunit/phpunit": "^5 || ^6.5"
},
"type": "library",
"autoload": {
"files": [
"src/getallheaders.php"
]
},
"license": [
"MIT"
],
"authors": [
{
"name": "Ralph Khattar",
"email": "ralph.khattar@gmail.com"
}
],
"description": "A polyfill for getallheaders.",
"time": "2019-03-08T08:55:37+00:00"
},
{
"name": "symfony/deprecation-contracts",
"version": "v2.5.2",
"dist": {
"type": "zip",
"url": "https://mirrors.tencent.com/repository/composer/symfony/deprecation-contracts/v2.5.2/symfony-deprecation-contracts-v2.5.2.zip",
"reference": "e8b495ea28c1d97b5e0c121748d6f9b53d075c66",
"shasum": ""
},
"require": {
"php": ">=7.1"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-main": "2.5-dev"
},
"thanks": {
"name": "symfony/contracts",
"url": "https://github.com/symfony/contracts"
}
},
"autoload": {
"files": [
"function.php"
]
},
"license": [
"MIT"
],
"authors": [
{
"name": "Nicolas Grekas",
"email": "p@tchwork.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "A generic function and convention to trigger deprecation notices",
"homepage": "https://symfony.com",
"time": "2022-01-02T09:53:40+00:00"
}
],
"packages-dev": [],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,
"platform": {
"php": ">=7.4"
},
"platform-dev": [],
"plugin-api-version": "2.6.0"
}

5
config/config.php Normal file
View File

@@ -0,0 +1,5 @@
<?php
return [
'name' => '优惠券模块'
];

View File

@@ -0,0 +1,49 @@
<?php
namespace XuanChen\CardCoupon\Adapter;
use XuanChen\CardCoupon\Contracts\CouponGateway;
use XuanChen\CardCoupon\Gateways\WechatGateway;
class WechatAdapter implements CouponGateway
{
protected $gateway;
public function __construct(WechatGateway $gateway)
{
$this->gateway = $gateway;
}
public function setRight($right): self
{
$this->gateway->setRight($right);
return $this;
}
public function setOrder($order): self
{
$this->gateway->setOrder($order);
return $this;
}
public function setUser($user): self
{
$this->gateway->setUser($user);
return $this;
}
public function issue()
{
return $this->gateway->issue();
}
public function query($couponCode)
{
return $this->gateway->query($couponCode);
}
public function validate($couponCode)
{
return $this->gateway->validate($couponCode);
}
}

View File

@@ -0,0 +1,49 @@
<?php
namespace XuanChen\CardCoupon\Adapter;
use XuanChen\CardCoupon\Contracts\CouponGateway;
use XuanChen\CardCoupon\Gateways\YsdGateway;
class YsdAdapter implements CouponGateway
{
protected $gateway;
public function __construct(YsdGateway $gateway)
{
$this->gateway = $gateway;
}
public function setRight($right): self
{
$this->gateway->setRight($right);
return $this;
}
public function setOrder($order): self
{
$this->gateway->setOrder($order);
return $this;
}
public function setUser($user): self
{
$this->gateway->setUser($user);
return $this;
}
public function issue()
{
return $this->gateway->issue();
}
public function query($couponCode)
{
return $this->gateway->query($couponCode);
}
public function validate($couponCode)
{
return $this->gateway->validate($couponCode);
}
}

44
src/Clients/ApiClient.php Normal file
View File

@@ -0,0 +1,44 @@
<?php
namespace XuanChen\CardCoupon\Clients;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
class ApiClient
{
private $http;
public function __construct(string $baseUri)
{
$this->http = new Client(['base_uri' => $baseUri]);
}
public function get(string $endpoint, array $query = [])
{
return $this->send('GET', $endpoint, [
'query' => $query,
]);
}
public function post(string $endpoint, array $data = [])
{
return $this->send('POST', $endpoint, [
'json' => $data,
]);
}
private function send(string $method, string $endpoint, array $options = [])
{
try {
$response = $this->http->request($method, $endpoint, $options);
$statusCode = $response->getStatusCode();
$body = $response->getBody();
// Handle response
} catch (RequestException $e) {
// Handle request error
}
}
}

View File

@@ -0,0 +1,39 @@
<?php
namespace XuanChen\CardCoupon\Contracts;
use App\Models\Coupon;
interface CouponGateway
{
/**
* Notes: 发券
*
* @Author: 玄尘
* @Date: 2023/10/30 13:53
* @param $couponData
* @return mixed
*/
public function issue();
/**
* Notes: 查券
*
* @Author: 玄尘
* @Date: 2023/10/30 13:53
* @param \App\Models\Coupon $coupon
* @return mixed
*/
public function query(Coupon $coupon);
/**
* Notes: 核销
*
* @Author: 玄尘
* @Date: 2023/10/30 13:54
* @param \App\Models\Coupon $coupon
* @return mixed
*/
public function validate(Coupon $coupon);
}

33
src/CouponService.php Normal file
View File

@@ -0,0 +1,33 @@
<?php
namespace XuanChen\CardCoupon;
use XuanChen\CardCoupon\Contracts\CouponGateway;
use XuanChen\CardCoupon\Logging\CouponLogger;
class CouponService
{
protected $gateway;
protected $channel;
public function __construct(CouponGateway $gateway)
{
$this->gateway = $gateway;
}
public function issue(array $data)
{
try {
return $this->gateway->issue($data);
} catch (\Exception $e) {
CouponLogger::getLogger(__CLASS__)->error("Error issuing coupon: {$e->getMessage()}");
throw $e;
}
}
public function setChannel($channel)
{
$this->channel = $channel;
return $this;
}
}

View File

@@ -0,0 +1,66 @@
<?php
namespace XuanChen\CardCoupon\Gateways;
use XuanChen\CardCoupon\Contracts\CouponGateway;
use XuanChen\CardCoupon\Logging\CouponLogger;
abstract class BaseGateway implements CouponGateway
{
protected $right;
protected $order;
protected $user;
protected $logger;
public function __construct()
{
$this->logger = CouponLogger::getLogger(get_class($this));
}
protected function log($message, $context = [])
{
$this->logger->info($message, $context);
}
/**
* Notes: 设置权益
*
* @Author: 玄尘
* @Date: 2023/10/31 10:21
* @param $right
* @return \XuanChen\CardCoupon\Contracts\BaseGateway
*/
public function setRight($right): self
{
$this->right = $right;
return $this;
}
/**
* Notes: 设置订单
*
* @Author: 玄尘
* @Date: 2023/10/31 10:22
* @param $order
*/
public function setOrder($order)
{
$this->order = $order;
return $this;
}
/**
* Notes: 设置购买人
*
* @Author: 玄尘
* @Date: 2023/10/31 10:22
* @param $user
*/
public function setUser($user)
{
$this->user = $user;
return $this;
}
}

View File

@@ -0,0 +1,35 @@
<?php
namespace XuanChen\CardCoupon\Gateways;
use App\Models\Coupon;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use XuanChen\CardCoupon\Clients\ApiClient;
use XuanChen\CardCoupon\Contracts\BaseGateway;
use XuanChen\CardCoupon\Contracts\CouponGateway;
use XuanChen\CardCoupon\Logging\CouponLogger;
class WechatGateway extends BaseGateway
{
protected ApiClient $apiClient;
protected $logger;
protected $server_id;
protected $des;
protected $key;
protected $outlet_id;
protected $baseUri;
public function __construct()
{
parent::__construct();
$this->server_id = config('pingan.server_id');
$this->des = config('pingan.des');
$this->key = config('pingan.key');
$this->outlet_id = config('pingan.outlet_id');
$this->baseUri = config('pingan.BaseUrl');
$this->apiClient = new ApiClient($this->baseUri);
}
}

146
src/Gateways/YsdGateway.php Normal file
View File

@@ -0,0 +1,146 @@
<?php
namespace XuanChen\CardCoupon\Gateways;
use App\Models\Coupon;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use XuanChen\CardCoupon\Clients\ApiClient;
class YsdGateway extends BaseGateway
{
protected ApiClient $apiClient;
protected $server_id;
protected $des;
protected $key;
protected $outlet_id;
protected $baseUri;
public function __construct()
{
parent::__construct();
$this->server_id = config('pingan.server_id');
$this->des = config('pingan.des');
$this->key = config('pingan.key');
$this->outlet_id = config('pingan.outlet_id');
$this->baseUri = config('pingan.BaseUrl');
$this->apiClient = new ApiClient($this->baseUri);
}
/**
* Notes: 发券
*
* @Author: 玄尘
* @Date: 2023/10/30 13:56
* @return mixed
*/
public function issue()
{
$data = [
'activityId' => $this->right->type_no,
'mobile' => $this->user->username,
'outletId' => $this->outlet_id,
];
$params = $this->getData($data);
dd($params);
return $params;
}
/**
* Notes: description
*
* @Author: 玄尘
* @Date: 2023/10/30 13:56
* @param \App\Models\Coupon $coupon
* @return mixed
*/
public function query(Coupon $coupon)
{
// TODO: Implement queryCoupon() method.
}
/**
* Notes: description
*
* @Author: 玄尘
* @Date: 2023/10/30 13:56
* @param \App\Models\Coupon $coupon
* @return mixed
*/
public function validate(Coupon $coupon)
{
// TODO: Implement verifyCoupon() method.
}
public function getData($paramArray, $channel = 'pingan')
{
$paramStr = self::keyasc(json_encode($paramArray));
$addcode = Str::random(16);
$sign = self::keysign($paramStr, $addcode);
$data = [
'server_id' => $this->server_id,
'key' => $this->key,
'data' => $paramStr,
'addcode' => $addcode,
'sign' => $sign,
];
if ($channel == 'bsshop') {
$data['des'] = $this->des;
}
return $data;
}
public function checkData(Request $request)
{
$server_id = $request->server_id ?? false;
$key = $request->key ?? false;
$data = $request->data ?? false;
$addcode = $request->addcode ?? false;
$sign = $request->sign ?? false;
if (! $server_id) {
return '参数server_id不能为空';
}
if (! $key) {
return '参数key不能为空';
}
if (! $sign) {
return '参数sign不能为空';
}
if (! $data) {
return '参数data不能为空';
}
if (! $addcode) {
return '参数addcode不能为空';
}
if ($this->key) {
$this->key = $key;
}
$data = str_replace('\\', '', $data);
$data = str_replace(' ', '+', $data);
$checksign = $this->keysign($data, $addcode);
if ($checksign != $sign) {
return '参数sign不正确';
}
$keydesc = $this->keydesc($data);
$keydescArr = json_decode($this->keydesc($data), true);
if (empty($keydescArr) && ! empty($keydesc)) {
return '传递的json数据不对';
}
return $keydescArr;
}
}

View File

@@ -0,0 +1,17 @@
<?php
namespace XuanChen\CardCoupon\Logging;
use Monolog\Logger as MonologLogger;
use Monolog\Handler\StreamHandler;
class CouponLogger
{
public static function getLogger($name)
{
$logger = new MonologLogger($name);
$logger->pushHandler(new StreamHandler(storage_path('logs/coupon.log')));
return $logger;
}
}

View File

@@ -0,0 +1,27 @@
<?php
namespace XuanChen\CardCoupon\Providers;
use Illuminate\Support\ServiceProvider;
use XuanChen\CardCoupon\Contracts\CouponGateway;
use XuanChen\CardCoupon\Services\CouponService;
class CouponServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->singleton('card_coupon.', function ($app) {
return new CouponService();
});
$this->app->bind(CouponService::class, function ($app) {
$couponGateway = $app->make(CouponGateway::class);
return new CouponService($couponGateway);
});
}
public function boot()
{
$this->mergeConfigFrom(__DIR__.'/../config/config.php', 'card-coupon');
}
}