Skip to content

Commit

Permalink
merge from v3
Browse files Browse the repository at this point in the history
  • Loading branch information
yansongda committed Jun 6, 2021
2 parents 96d02d7 + a976e08 commit d08d153
Show file tree
Hide file tree
Showing 35 changed files with 241 additions and 131 deletions.
Expand Up @@ -6,10 +6,10 @@

use Psr\Http\Message\ResponseInterface;

interface PackerInterface
interface ParserInterface
{
/**
* @return mixed
*/
public function unpack(ResponseInterface $response);
public function parse(?ResponseInterface $response);
}
2 changes: 1 addition & 1 deletion src/Contract/ShortcutInterface.php
Expand Up @@ -9,7 +9,7 @@ interface ShortcutInterface
/**
* @author yansongda <me@yansongda.cn>
*
* @return \Yansongda\Pay\Contract\PluginInterface[]
* @return \Yansongda\Pay\Contract\PluginInterface[]|string[]
*/
public function getPlugins(array $params): array;
}
2 changes: 2 additions & 0 deletions src/Exception/Exception.php
Expand Up @@ -61,6 +61,8 @@ class Exception extends \Exception

public const INVALID_RESPONSE_CODE = 5004;

public const RESPONSE_NONE = 5005;

/**
* raw.
*
Expand Down
40 changes: 38 additions & 2 deletions src/Functions.php
Expand Up @@ -3,10 +3,21 @@
declare(strict_types=1);

use Yansongda\Pay\Contract\ConfigInterface;
use Yansongda\Pay\Exception\InvalidConfigException;
use Yansongda\Pay\Parser\NoHttpRequestParser;
use Yansongda\Pay\Pay;
use Yansongda\Pay\Rocket;
use Yansongda\Supports\Config;
use Yansongda\Supports\Str;

if (!function_exists('should_http_request')) {
function should_http_request(Rocket $rocket): bool
{
return NoHttpRequestParser::class !== $rocket->getDirection() &&
!in_array(NoHttpRequestParser::class, class_parents($rocket->getDirection()));
}
}

if (!function_exists('get_alipay_config')) {
/**
* @throws \Yansongda\Pay\Exception\ContainerDependencyException
Expand All @@ -23,11 +34,11 @@ function get_alipay_config(array $params): Config
}
}

if (!function_exists('get_alipay_cert')) {
if (!function_exists('get_public_crt_or_private_cert')) {
/**
* @return false|resource|string
*/
function get_alipay_cert(string $key)
function get_public_crt_or_private_cert(string $key)
{
if (Str::endsWith($key, '.crt')) {
$key = file_get_contents($key);
Expand All @@ -45,6 +56,31 @@ function get_alipay_cert(string $key)
}
}

if (!function_exists('verify_alipay_response')) {
/**
* @param string $sign base64decode 之后的
*
* @throws \Yansongda\Pay\Exception\ContainerDependencyException
* @throws \Yansongda\Pay\Exception\ContainerException
* @throws \Yansongda\Pay\Exception\InvalidConfigException
* @throws \Yansongda\Pay\Exception\ServiceNotFoundException
*/
function verify_alipay_response(array $params, string $contents, string $sign): bool
{
$public = get_alipay_config($params)->get('alipay_public_cert_path');

if (is_null($public)) {
throw new InvalidConfigException(InvalidConfigException::ALIPAY_CONFIG_ERROR, 'Missing Alipay Config -- [alipay_public_cert_path]');
}

return 1 === openssl_verify(
$contents,
$sign,
get_public_crt_or_private_cert($public),
OPENSSL_ALGO_SHA256);
}
}

if (!function_exists('get_wechat_config')) {
/**
* @throws \Yansongda\Pay\Exception\ContainerDependencyException
Expand Down
12 changes: 8 additions & 4 deletions src/Parser/ArrayParser.php
Expand Up @@ -2,19 +2,23 @@

declare(strict_types=1);

namespace Yansongda\Pay\Packer;
namespace Yansongda\Pay\Parser;

use Psr\Http\Message\ResponseInterface;
use Yansongda\Pay\Contract\PackerInterface;
use Yansongda\Pay\Contract\ParserInterface;
use Yansongda\Pay\Exception\InvalidResponseException;

class ArrayParser implements PackerInterface
class ArrayParser implements ParserInterface
{
/**
* @throws \Yansongda\Pay\Exception\InvalidResponseException
*/
public function unpack(ResponseInterface $response): array
public function parse(?ResponseInterface $response): array
{
if (is_null($response)) {
throw new InvalidResponseException(InvalidResponseException::RESPONSE_NONE);
}

$contents = $response->getBody()->getContents();

$result = json_decode($contents, true);
Expand Down
10 changes: 5 additions & 5 deletions src/Parser/CollectionParser.php
Expand Up @@ -2,24 +2,24 @@

declare(strict_types=1);

namespace Yansongda\Pay\Packer;
namespace Yansongda\Pay\Parser;

use Psr\Http\Message\ResponseInterface;
use Yansongda\Pay\Contract\PackerInterface;
use Yansongda\Pay\Contract\ParserInterface;
use Yansongda\Pay\Pay;
use Yansongda\Supports\Collection;

class CollectionParser implements PackerInterface
class CollectionParser implements ParserInterface
{
/**
* @throws \Yansongda\Pay\Exception\ContainerDependencyException
* @throws \Yansongda\Pay\Exception\ContainerException
* @throws \Yansongda\Pay\Exception\ServiceNotFoundException
*/
public function unpack(ResponseInterface $response): Collection
public function parse(?ResponseInterface $response): Collection
{
return new Collection(
Pay::get(JsonPacker::class)->unpack($response)
Pay::get(ArrayParser::class)->parse($response)
);
}
}
2 changes: 1 addition & 1 deletion src/Parser/JsonParser.php
Expand Up @@ -4,6 +4,6 @@

namespace Yansongda\Pay\Parser;

class JsonParser extends ArrayPacker
class JsonParser extends ArrayParser
{
}
16 changes: 16 additions & 0 deletions src/Parser/NoHttpRequestParser.php
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Yansongda\Pay\Parser;

use Psr\Http\Message\ResponseInterface;
use Yansongda\Pay\Contract\ParserInterface;

class NoHttpRequestParser implements ParserInterface
{
public function parse(?ResponseInterface $response): ?ResponseInterface
{
return $response;
}
}
9 changes: 1 addition & 8 deletions src/Parser/ResponseParser.php
Expand Up @@ -4,13 +4,6 @@

namespace Yansongda\Pay\Parser;

use Psr\Http\Message\ResponseInterface;
use Yansongda\Pay\Contract\PackerInterface;

class ResponseParser implements PackerInterface
class ResponseParser extends NoHttpRequestParser
{
public function unpack(ResponseInterface $response): ResponseInterface
{
return $response;
}
}
58 changes: 58 additions & 0 deletions src/Plugin/Alipay/CallbackPlugin.php
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace Yansongda\Pay\Plugin\Alipay;

use Closure;
use Yansongda\Pay\Contract\PluginInterface;
use Yansongda\Pay\Exception\InvalidResponseException;
use Yansongda\Pay\Parser\NoHttpRequestParser;
use Yansongda\Pay\Rocket;
use Yansongda\Supports\Collection;

class CallbackPlugin implements PluginInterface
{
/**
* @throws \Yansongda\Pay\Exception\ContainerDependencyException
* @throws \Yansongda\Pay\Exception\ContainerException
* @throws \Yansongda\Pay\Exception\InvalidConfigException
* @throws \Yansongda\Pay\Exception\ServiceNotFoundException
* @throws \Yansongda\Pay\Exception\InvalidResponseException
*/
public function assembly(Rocket $rocket, Closure $next): Rocket
{
$this->filterPayload($rocket);

if (!($rocket->getParams()['sign'] ?? false)) {
throw new InvalidResponseException(InvalidResponseException::INVALID_RESPONSE_SIGN, '', $rocket->getParams());
}

if (!verify_alipay_response(
$rocket->getParams(),
$this->getSignContent($rocket->getPayload()),
base64_decode($rocket->getParams()['sign']))
) {
throw new InvalidResponseException(InvalidResponseException::INVALID_RESPONSE_SIGN, '', $rocket->getParams());
}

$rocket->setDirection(NoHttpRequestParser::class)
->setDestination($rocket->getPayload());

return $next($rocket);
}

protected function filterPayload(Rocket $rocket): void
{
$payload = (new Collection($rocket->getParams()))->filter(function ($v, $k) {
return '' !== $v && !is_null($v) && 'sign' != $k && 'sign_type' != $k;
});

$rocket->setPayload($payload);
}

protected function getSignContent(Collection $payload): string
{
return urldecode($payload->sortKeys()->toString());
}
}
4 changes: 2 additions & 2 deletions src/Plugin/Alipay/Data/BillDownloadUrlQueryPlugin.php
Expand Up @@ -4,9 +4,9 @@

namespace Yansongda\Pay\Plugin\Alipay\Data;

use Yansongda\Pay\Plugin\Alipay\GeneralPlugin;
use Yansongda\Pay\Plugin\Alipay\GeneralPayPlugin;

class BillDownloadUrlQueryPlugin extends GeneralPlugin
class BillDownloadUrlQueryPlugin extends GeneralPayPlugin
{
protected function getMethod(): string
{
Expand Down
4 changes: 2 additions & 2 deletions src/Plugin/Alipay/Data/BillEreceiptQueryPlugin.php
Expand Up @@ -4,9 +4,9 @@

namespace Yansongda\Pay\Plugin\Alipay\Data;

use Yansongda\Pay\Plugin\Alipay\GeneralPlugin;
use Yansongda\Pay\Plugin\Alipay\GeneralPayPlugin;

class BillEreceiptQueryPlugin extends GeneralPlugin
class BillEreceiptQueryPlugin extends GeneralPayPlugin
{
protected function getMethod(): string
{
Expand Down
4 changes: 2 additions & 2 deletions src/Plugin/Alipay/Ebpp/PdeductBillPayStatusPlugin.php
Expand Up @@ -4,9 +4,9 @@

namespace Yansongda\Pay\Plugin\Alipay\Ebpp;

use Yansongda\Pay\Plugin\Alipay\GeneralPlugin;
use Yansongda\Pay\Plugin\Alipay\GeneralPayPlugin;

class PdeductBillPayStatusPlugin extends GeneralPlugin
class PdeductBillPayStatusPlugin extends GeneralPayPlugin
{
protected function getMethod(): string
{
Expand Down
33 changes: 0 additions & 33 deletions src/Plugin/Alipay/FilterPlugin.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/Plugin/Alipay/Fund/AuthOrderUnfreezePlugin.php
Expand Up @@ -4,9 +4,9 @@

namespace Yansongda\Pay\Plugin\Alipay\Fund;

use Yansongda\Pay\Plugin\Alipay\GeneralPlugin;
use Yansongda\Pay\Plugin\Alipay\GeneralPayPlugin;

class AuthOrderUnfreezePlugin extends GeneralPlugin
class AuthOrderUnfreezePlugin extends GeneralPayPlugin
{
protected function getMethod(): string
{
Expand Down
4 changes: 2 additions & 2 deletions src/Plugin/Alipay/Fund/TransOrderQueryPlugin.php
Expand Up @@ -4,9 +4,9 @@

namespace Yansongda\Pay\Plugin\Alipay\Fund;

use Yansongda\Pay\Plugin\Alipay\GeneralPlugin;
use Yansongda\Pay\Plugin\Alipay\GeneralPayPlugin;

class TransOrderQueryPlugin extends GeneralPlugin
class TransOrderQueryPlugin extends GeneralPayPlugin
{
protected function getMethod(): string
{
Expand Down
Expand Up @@ -8,7 +8,7 @@
use Yansongda\Pay\Contract\PluginInterface;
use Yansongda\Pay\Rocket;

abstract class GeneralPlugin implements PluginInterface
abstract class GeneralPayPlugin implements PluginInterface
{
public function assembly(Rocket $rocket, Closure $next): Rocket
{
Expand Down
2 changes: 0 additions & 2 deletions src/Plugin/Alipay/HtmlResponsePlugin.php
Expand Up @@ -15,8 +15,6 @@ class HtmlResponsePlugin implements PluginInterface
{
public function assembly(Rocket $rocket, Closure $next): Rocket
{
$rocket->setDestination(new Response());

/* @var Rocket $rocket */
$rocket = $next($rocket);

Expand Down

0 comments on commit d08d153

Please sign in to comment.