Skip to content

Commit

Permalink
feat: encrypt as array
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Dec 13, 2024
1 parent 123758e commit 8955713
Show file tree
Hide file tree
Showing 29 changed files with 110 additions and 36 deletions.
37 changes: 30 additions & 7 deletions src/Kernel/Encryptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,31 @@ public function getToken(): string
* @throws Exception
*/
public function encrypt(string $plaintext, ?string $nonce = null, int|string|null $timestamp = null): string
{
return $this->encryptAsXml($plaintext, $nonce, $timestamp);
}

/**
* @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
*/
public function encryptAsXml(string $plaintext, ?string $nonce = null, int|string|null $timestamp = null): string
{
$encrypted = $this->encryptAsArray($plaintext, $nonce, $timestamp);

$response = [
'Encrypt' => $encrypted['ciphertext'],
'MsgSignature' => $encrypted['signature'],
'TimeStamp' => $encrypted['timestamp'],
'Nonce' => $encrypted['nonce'],
];

return Xml::build($response);
}

/**
* @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
*/
public function encryptAsArray(string $plaintext, ?string $nonce = null, int|string|null $timestamp = null): array
{
try {
$plaintext = Pkcs7::padding(random_bytes(16).pack('N', strlen($plaintext)).$plaintext.$this->appId, 32);
Expand All @@ -102,14 +127,12 @@ public function encrypt(string $plaintext, ?string $nonce = null, int|string|nul
$nonce ??= Str::random();
$timestamp ??= time();

$response = [
'Encrypt' => $ciphertext,
'MsgSignature' => $this->createSignature($this->token, $timestamp, $nonce, $ciphertext),
'TimeStamp' => $timestamp,
'Nonce' => $nonce,
return [
'ciphertext' => $ciphertext,
'signature' => $this->createSignature($this->token, $timestamp, $nonce, $ciphertext),
'timestamp' => $timestamp,
'nonce' => $nonce,
];

return Xml::build($response);
}

public function createSignature(mixed ...$attributes): string
Expand Down
4 changes: 3 additions & 1 deletion src/Kernel/Exceptions/BadMethodCallException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

namespace EasyWeChat\Kernel\Exceptions;

class BadMethodCallException extends Exception {}
class BadMethodCallException extends Exception
{
}
4 changes: 3 additions & 1 deletion src/Kernel/Exceptions/BadRequestException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@

namespace EasyWeChat\Kernel\Exceptions;

class BadRequestException extends Exception {}
class BadRequestException extends Exception
{
}
4 changes: 3 additions & 1 deletion src/Kernel/Exceptions/BadResponseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@

namespace EasyWeChat\Kernel\Exceptions;

class BadResponseException extends Exception {}
class BadResponseException extends Exception
{
}
4 changes: 3 additions & 1 deletion src/Kernel/Exceptions/DecryptException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@

namespace EasyWeChat\Kernel\Exceptions;

class DecryptException extends Exception {}
class DecryptException extends Exception
{
}
4 changes: 3 additions & 1 deletion src/Kernel/Exceptions/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@

use Exception as BaseException;

class Exception extends BaseException {}
class Exception extends BaseException
{
}
4 changes: 3 additions & 1 deletion src/Kernel/Exceptions/InvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@

namespace EasyWeChat\Kernel\Exceptions;

class InvalidArgumentException extends Exception {}
class InvalidArgumentException extends Exception
{
}
4 changes: 3 additions & 1 deletion src/Kernel/Exceptions/InvalidConfigException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@

namespace EasyWeChat\Kernel\Exceptions;

class InvalidConfigException extends Exception {}
class InvalidConfigException extends Exception
{
}
4 changes: 3 additions & 1 deletion src/Kernel/Exceptions/RuntimeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@

namespace EasyWeChat\Kernel\Exceptions;

class RuntimeException extends Exception {}
class RuntimeException extends Exception
{
}
4 changes: 3 additions & 1 deletion src/Kernel/Exceptions/ServiceNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@

namespace EasyWeChat\Kernel\Exceptions;

class ServiceNotFoundException extends Exception {}
class ServiceNotFoundException extends Exception
{
}
4 changes: 3 additions & 1 deletion src/Kernel/Form/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ class Form
/**
* @param array<string|array|DataPart> $fields
*/
public function __construct(protected array $fields) {}
public function __construct(protected array $fields)
{
}

/**
* @param array<string|array|DataPart> $fields
Expand Down
3 changes: 2 additions & 1 deletion src/Kernel/HttpClient/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public function __construct(
protected ResponseInterface $response,
protected ?Closure $failureJudge = null,
protected bool $throw = true
) {}
) {
}

public function throw(bool $throw = true): static
{
Expand Down
4 changes: 3 additions & 1 deletion src/MiniApp/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@

namespace EasyWeChat\MiniApp;

class Server extends \EasyWeChat\OfficialAccount\Server {}
class Server extends \EasyWeChat\OfficialAccount\Server
{
}
4 changes: 3 additions & 1 deletion src/MiniApp/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@

class Utils
{
public function __construct(protected Application $app) {}
public function __construct(protected Application $app)
{
}

/**
* @throws HttpException
Expand Down
3 changes: 2 additions & 1 deletion src/OfficialAccount/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public function __construct(
protected ?string $secret,
protected ?string $token = null,
protected ?string $aesKey = null
) {}
) {
}

public function getAppId(): string
{
Expand Down
4 changes: 3 additions & 1 deletion src/OfficialAccount/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

class Utils
{
public function __construct(protected Application $app) {}
public function __construct(protected Application $app)
{
}

/**
* @param array<string> $jsApiList
Expand Down
3 changes: 2 additions & 1 deletion src/OpenPlatform/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public function __construct(
protected string $secret,
protected string $token,
protected string $aesKey
) {}
) {
}

public function getAppId(): string
{
Expand Down
4 changes: 3 additions & 1 deletion src/OpenPlatform/AuthorizerAccessToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

class AuthorizerAccessToken implements AccessToken, Stringable
{
public function __construct(protected string $appId, protected string $accessToken) {}
public function __construct(protected string $appId, protected string $accessToken)
{
}

public function getAppId(): string
{
Expand Down
3 changes: 2 additions & 1 deletion src/OpenWork/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public function __construct(
protected string $suiteSecret,
protected string $token,
protected string $aesKey
) {}
) {
}

public function getCorpId(): string
{
Expand Down
4 changes: 3 additions & 1 deletion src/Pay/Exceptions/InvalidSignatureException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@

use EasyWeChat\Kernel\Exceptions\RuntimeException;

class InvalidSignatureException extends RuntimeException {}
class InvalidSignatureException extends RuntimeException
{
}
6 changes: 4 additions & 2 deletions src/Pay/LegacySignature.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

class LegacySignature
{
public function __construct(protected MerchantInterface $merchant) {}
public function __construct(protected MerchantInterface $merchant)
{
}

/**
* @param array<string, mixed> $params
Expand All @@ -39,7 +41,7 @@ public function sign(array $params): array
],
$params
),
static fn ($value, $key) => !($key === 'sign' || $value === '' || is_null($value)),
static fn ($value, $key) => ! ($key === 'sign' || $value === '' || is_null($value)),
ARRAY_FILTER_USE_BOTH
);

Expand Down
4 changes: 3 additions & 1 deletion src/Pay/ResponseValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

class ResponseValidator implements \EasyWeChat\Pay\Contracts\ResponseValidator
{
public function __construct(protected MerchantInterface $merchant) {}
public function __construct(protected MerchantInterface $merchant)
{
}

/**
* @throws \EasyWeChat\Kernel\Exceptions\BadResponseException
Expand Down
4 changes: 3 additions & 1 deletion src/Pay/Signature.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@

class Signature
{
public function __construct(protected MerchantInterface $merchant) {}
public function __construct(protected MerchantInterface $merchant)
{
}

/**
* @param array<string,mixed> $options
Expand Down
4 changes: 3 additions & 1 deletion src/Pay/URLSchemeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@

class URLSchemeBuilder
{
public function __construct(protected MerchantInterface $merchant) {}
public function __construct(protected MerchantInterface $merchant)
{
}

/**
* @throws Exception
Expand Down
4 changes: 3 additions & 1 deletion src/Pay/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

class Utils
{
public function __construct(protected MerchantInterface $merchant) {}
public function __construct(protected MerchantInterface $merchant)
{
}

/**
* @see https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter4_1_4.shtml
Expand Down
4 changes: 3 additions & 1 deletion src/Pay/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ class Validator implements \EasyWeChat\Pay\Contracts\Validator

public const HEADER_SIGNATURE = 'Wechatpay-Signature';

public function __construct(protected MerchantInterface $merchant) {}
public function __construct(protected MerchantInterface $merchant)
{
}

/**
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
Expand Down
3 changes: 2 additions & 1 deletion src/Work/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public function __construct(
protected string $secret,
protected string $token,
protected string $aesKey,
) {}
) {
}

public function getCorpId(): string
{
Expand Down
4 changes: 3 additions & 1 deletion src/Work/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

class Utils
{
public function __construct(protected Application $app) {}
public function __construct(protected Application $app)
{
}

/**
* @param array<string> $jsApiList
Expand Down
4 changes: 3 additions & 1 deletion tests/Kernel/Traits/DecryptXmlMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ public function test_it_can_decrypt_message()
}
}

class DummyMessage extends Message {}
class DummyMessage extends Message
{
}

0 comments on commit 8955713

Please sign in to comment.