eee
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace wchat\wx\V3\Notify;
|
||||
|
||||
class GoodsDetail
|
||||
{
|
||||
public string $goods_remark = "商品备注信息";
|
||||
public int $quantity = 1;
|
||||
public int $discount_amount = 1;
|
||||
public string $goods_id = "M1006";
|
||||
public int $unit_price = 100;
|
||||
|
||||
/**
|
||||
* @param array $value
|
||||
*/
|
||||
public function __construct(readonly public array $value)
|
||||
{
|
||||
$this->goods_remark = $this->value['goods_remark'];
|
||||
$this->quantity = $this->value['quantity'];
|
||||
$this->discount_amount = $this->value['discount_amount'];
|
||||
$this->goods_id = $this->value['goods_id'];
|
||||
$this->unit_price = $this->value['unit_price'];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
namespace wchat\wx\V3\Notify;
|
||||
|
||||
use JetBrains\PhpStorm\ArrayShape;
|
||||
|
||||
class NotifyModel
|
||||
{
|
||||
|
||||
//公众号支付
|
||||
const PAY_TYPE_JSAPI = 'JSAPI';
|
||||
|
||||
//扫码支付
|
||||
const PAY_TYPE_NATIVE = 'NATIVE';
|
||||
|
||||
//App支付
|
||||
const PAY_TYPE_App = 'App';
|
||||
|
||||
//付款码支付
|
||||
const PAY_TYPE_MICROPAY = 'MICROPAY';
|
||||
|
||||
//H5支付
|
||||
const PAY_TYPE_MWEB = 'MWEB';
|
||||
|
||||
//刷脸支付
|
||||
const PAY_TYPE_FACEPAY = 'FACEPAY';
|
||||
|
||||
|
||||
// 支付成功
|
||||
const PAY_RESULT_SUCCESS = 'SUCCESS';
|
||||
|
||||
// 转入退款
|
||||
const PAY_RESULT_REFUND = 'REFUND';
|
||||
|
||||
// 未支付
|
||||
const PAY_RESULT_NOTPAY = 'NOTPAY';
|
||||
|
||||
// 已关闭
|
||||
const PAY_RESULT_CLOSED = 'CLOSED';
|
||||
|
||||
// 已撤销(付款码支付)
|
||||
const PAY_RESULT_REVOKED = 'REVOKED';
|
||||
|
||||
// 用户支付中(付款码支付)
|
||||
const PAY_RESULT_USERPAYING = 'USERPAYING';
|
||||
|
||||
// 支付失败(其他原因,如银行返回失败)
|
||||
const PAY_RESULT_PAYERROR = 'PAYERROR';
|
||||
|
||||
|
||||
public string $appid;
|
||||
public string $mchid;
|
||||
public string $out_trade_no;
|
||||
public string $transaction_id;
|
||||
public string $trade_type;
|
||||
public string $trade_state;
|
||||
public string $trade_state_desc;
|
||||
public string $bank_type;
|
||||
public string $attach;
|
||||
public string $success_time;
|
||||
|
||||
/**
|
||||
* @var array|string[]
|
||||
*/
|
||||
#[ArrayShape(['openid' => 'string'])]
|
||||
public array $payer = ['openid' => ''];
|
||||
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
#[ArrayShape(['payer_total' => 'int', 'total' => 'int', 'currency' => 'string', 'payer_currency' => 'string'])]
|
||||
public array $amount = [
|
||||
"payer_total" => 100,
|
||||
"total" => 100,
|
||||
"currency" => "CNY",
|
||||
"payer_currency" => "CNY"
|
||||
];
|
||||
|
||||
|
||||
/**
|
||||
* @var array|string[]
|
||||
*/
|
||||
#[ArrayShape(['device_id' => 'string'])]
|
||||
public array $scene_info = [
|
||||
'device_id' => ''
|
||||
];
|
||||
|
||||
/**
|
||||
* @var array<PromotionDetail>
|
||||
*/
|
||||
public array $promotion_detail = [];
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace wchat\wx\V3\Notify;
|
||||
|
||||
class PromotionDetail
|
||||
{
|
||||
|
||||
public int $amount = 100;
|
||||
public int $wechatpay_contribute = 0;
|
||||
public string $coupon_id = "109519";
|
||||
public string $scope = "GLOBAL";
|
||||
public int $merchant_contribute = 0;
|
||||
public string $name = "单品惠-6";
|
||||
public int $other_contribute = 0;
|
||||
public string $currency = "CNY";
|
||||
public string $stock_id = "931386";
|
||||
|
||||
/**
|
||||
* @var array<GoodsDetail>
|
||||
*/
|
||||
public array $goods_detail = [];
|
||||
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace wchat\wx\V3;
|
||||
|
||||
use Exception;
|
||||
use JetBrains\PhpStorm\ArrayShape;
|
||||
use Kiri\Client;
|
||||
use Kiri\CurlClient;
|
||||
use wchat\wx\SmallProgram;
|
||||
|
||||
class TransferBatches extends SmallProgram
|
||||
{
|
||||
use WxV3PaymentTait;
|
||||
|
||||
|
||||
/**
|
||||
* @param TransferDetail $detail
|
||||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
#[ArrayShape([])]
|
||||
public function request(TransferDetail $detail): array
|
||||
{
|
||||
$payConfig = $this->getPayConfig();
|
||||
$body = [];
|
||||
if ($payConfig->typeIsApp()) {
|
||||
$body['appid'] = $payConfig->pay->wx->appId;
|
||||
} else {
|
||||
$body['appid'] = $payConfig->appId;
|
||||
}
|
||||
$body['out_batch_no'] = $detail->out_detail_no;
|
||||
$body["batch_name"] = $payConfig->getBody();
|
||||
$body["body"] = $payConfig->getBody();
|
||||
$body["batch_remark"] = $payConfig->getBody();
|
||||
$body["total_amount"] = $detail->transfer_amount;
|
||||
$body["total_num"] = 1;
|
||||
$body["transfer_detail_list"] = [$detail->toArray()];
|
||||
|
||||
$sign = $this->signature('POST', '/v3/transfer/batches', $json = json_encode($body, JSON_UNESCAPED_UNICODE));
|
||||
|
||||
$client = $this->createClient($sign, $json);
|
||||
$client->post('/v3/transfer/batches');
|
||||
$client->close();
|
||||
|
||||
$data = json_decode($client->getBody(), TRUE);
|
||||
if (json_last_error() != JSON_ERROR_NONE) {
|
||||
return ['code' => $client->getStatusCode(), 'message' => $client->getBody()];
|
||||
} else {
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace wchat\wx\V3;
|
||||
|
||||
use JetBrains\PhpStorm\ArrayShape;
|
||||
|
||||
class TransferDetail
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param string $out_detail_no
|
||||
* @param int|float $transfer_amount
|
||||
* @param string $transfer_remark
|
||||
* @param string $openid
|
||||
* @param string $user_name
|
||||
*/
|
||||
public function __construct(
|
||||
readonly public string $out_detail_no,
|
||||
readonly public int|float $transfer_amount,
|
||||
readonly public string $transfer_remark,
|
||||
readonly public string $openid,
|
||||
readonly public string $user_name = ''
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
#[ArrayShape(['out_detail_no' => "string", 'transfer_amount' => "float|int", 'transfer_remark' => "string", 'openid' => "string", 'user_name' => "string"])]
|
||||
public function toArray(): array
|
||||
{
|
||||
if (empty($this->user_name)) {
|
||||
return [
|
||||
'out_detail_no' => $this->out_detail_no,
|
||||
'transfer_amount' => $this->transfer_amount,
|
||||
'transfer_remark' => $this->transfer_remark,
|
||||
'openid' => $this->openid,
|
||||
];
|
||||
}
|
||||
return [
|
||||
'out_detail_no' => $this->out_detail_no,
|
||||
'transfer_amount' => $this->transfer_amount,
|
||||
'transfer_remark' => $this->transfer_remark,
|
||||
'openid' => $this->openid,
|
||||
'user_name' => $this->user_name,
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace wchat\wx\V3;
|
||||
|
||||
use Exception;
|
||||
use Kiri\Client;
|
||||
use wchat\wx\SmallProgram;
|
||||
|
||||
class WxV3AppPayment extends SmallProgram
|
||||
{
|
||||
|
||||
|
||||
use WxV3PaymentTait;
|
||||
|
||||
|
||||
/**
|
||||
* @param $orderNo
|
||||
* @param int $total
|
||||
* @param string|null $openId
|
||||
* @param string $payer_client_ip
|
||||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
public function payment($orderNo, int $total, string $openId = NULL, string $payer_client_ip = '127.0.0.1'): array
|
||||
{
|
||||
$body = $this->getInitCore($orderNo, $total);
|
||||
|
||||
$body['scene_info'] = ['payer_client_ip' => $payer_client_ip];
|
||||
|
||||
$sign = $this->signature('POST', '/v3/pay/transactions/components', $json = json_encode($body, JSON_UNESCAPED_UNICODE));
|
||||
|
||||
$client = $this->createClient($sign, $json);
|
||||
$client->post('/v3/pay/transactions/components');
|
||||
$client->close();
|
||||
|
||||
$json = json_decode($client->getBody(), TRUE);
|
||||
if (!isset($json['prepay_id'])) {
|
||||
throw new Exception('微信支付调用失败');
|
||||
}
|
||||
return $this->createResponse($json, $body);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace wchat\wx\V3;
|
||||
|
||||
use Exception;
|
||||
use Kiri\Client;
|
||||
use JetBrains\PhpStorm\ArrayShape;
|
||||
use wchat\wx\SmallProgram;
|
||||
|
||||
class WxV3NativePayment extends SmallProgram
|
||||
{
|
||||
|
||||
|
||||
use WxV3PaymentTait;
|
||||
|
||||
|
||||
/**
|
||||
* @param $orderNo
|
||||
* @param int $total
|
||||
* @param string|null $openId
|
||||
* @param string $payer_client_ip
|
||||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
#[ArrayShape(['code_url' => "string"])]
|
||||
public function payment($orderNo, int $total, string $openId = NULL, string $payer_client_ip = '127.0.0.1'): array
|
||||
{
|
||||
$body = $this->getInitCore($orderNo, $total);
|
||||
|
||||
$sign = $this->signature('POST', '/v3/pay/transactions/native', $json = json_encode($body, JSON_UNESCAPED_UNICODE));
|
||||
|
||||
$client = $this->createClient($sign, $json);
|
||||
$client->post('/v3/pay/transactions/native');
|
||||
$client->close();
|
||||
|
||||
$json = json_decode($client->getBody(), TRUE);
|
||||
if (!isset($json['code_url'])) {
|
||||
throw new Exception('微信支付调用失败');
|
||||
}
|
||||
return ['code_url' => $json['code_url']];
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace wchat\wx\V3;
|
||||
|
||||
use Exception;
|
||||
use Kiri\Client;
|
||||
use wchat\wx\SmallProgram;
|
||||
|
||||
class WxV3Payment extends SmallProgram
|
||||
{
|
||||
|
||||
|
||||
use WxV3PaymentTait;
|
||||
|
||||
|
||||
/**
|
||||
* @param $orderNo
|
||||
* @param int $total
|
||||
* @param string|null $openId
|
||||
* @param string $payer_client_ip
|
||||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
public function payment($orderNo, int $total, string $openId = NULL, string $payer_client_ip = '127.0.0.1'): array
|
||||
{
|
||||
$body = $this->getInitCore($orderNo, $total);
|
||||
$body['payer'] = ['openid' => $openId];
|
||||
$body['scene_info'] = ['payer_client_ip' => $payer_client_ip];
|
||||
|
||||
$sign = $this->signature('POST', '/v3/pay/transactions/jsapi', $json = json_encode($body, JSON_UNESCAPED_UNICODE));
|
||||
|
||||
$client = $this->createClient($sign, $json);
|
||||
$client->post('/v3/pay/transactions/jsapi');
|
||||
$client->close();
|
||||
|
||||
$json = json_decode($client->getBody(), TRUE);
|
||||
if (!isset($json['prepay_id'])) {
|
||||
throw new Exception('微信支付调用失败');
|
||||
}
|
||||
|
||||
return $this->createResponse($json, $body);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,167 @@
|
||||
<?php
|
||||
|
||||
namespace wchat\wx\V3;
|
||||
|
||||
use Exception;
|
||||
use OpenSSLAsymmetricKey;
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
use wchat\common\PayConfig;
|
||||
use wchat\wx\SmallProgram;
|
||||
use wchat\wx\V3\Notify\GoodsDetail;
|
||||
use wchat\wx\V3\Notify\NotifyModel;
|
||||
use wchat\wx\V3\Notify\PromotionDetail;
|
||||
|
||||
const KEY_TYPE_PUBLIC = 'public';
|
||||
const KEY_TYPE_PRIVATE = 'private';
|
||||
|
||||
class WxV3PaymentNotify extends SmallProgram
|
||||
{
|
||||
|
||||
|
||||
use WxV3PaymentTait;
|
||||
|
||||
|
||||
/**
|
||||
* @param string $id
|
||||
* @param string $create_time
|
||||
* @param string $resource_type
|
||||
* @param string $event_type
|
||||
* @param string $summary
|
||||
* @param array $resource
|
||||
*/
|
||||
public function __construct(
|
||||
public string $id = "EV-2018022511223320873",
|
||||
public string $create_time = "2015-05-20T13:29:35+08:00",
|
||||
public string $resource_type = "encrypt-resource",
|
||||
public string $event_type = "TRANSACTION.SUCCESS",
|
||||
public string $summary = "支付成功",
|
||||
public array $resource = []
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @var NotifyModel
|
||||
*/
|
||||
public NotifyModel $notifyModel;
|
||||
|
||||
|
||||
/**
|
||||
* @param RequestInterface $request
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function verify(RequestInterface $request): bool
|
||||
{
|
||||
$platformPublicKeyInstance = $this->rsaFrom($this->payConfig->pay->wx->mchKey, KEY_TYPE_PUBLIC);
|
||||
$inWechatpaySignature = $request->getHeaderLine('Wechatpay-Signature'); // 请根据实际情况获取
|
||||
$inWechatpayTimestamp = $request->getHeaderLine('Wechatpay-Timestamp'); // 请根据实际情况获取
|
||||
$inWechatpayNonce = $request->getHeaderLine('Wechatpay-Nonce'); // 请根据实际情况获取
|
||||
$inBody = $request->getBody()->getContents(); // 请根据实际情况获取,例如: file_get_contents('php://input');
|
||||
$timeOffsetStatus = 300 >= abs(time() - (int)$inWechatpayTimestamp);
|
||||
$verifiedStatus = $this->notifyVerify(
|
||||
$this->lineFeed([$inWechatpayTimestamp, $inWechatpayNonce, $inBody]),
|
||||
$inWechatpaySignature,
|
||||
$platformPublicKeyInstance);
|
||||
if (!$timeOffsetStatus || !$verifiedStatus) {
|
||||
return false;
|
||||
}
|
||||
return $this->decode($this->resource['ciphertext'], $this->resource['nonce'], $this->resource['associated_data']);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ...$pieces
|
||||
* @return string
|
||||
*/
|
||||
protected function lineFeed(...$pieces): string
|
||||
{
|
||||
return implode("\n", array_merge($pieces, ['']));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string|bool
|
||||
*/
|
||||
protected function body(): string|bool
|
||||
{
|
||||
return json_encode(['id' => $this->id, 'create_time' => $this->create_time, 'resource_type' => $this->resource_type, 'event_type' => $this->event_type, 'summary' => $this->summary, 'resource' => $this->resource]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
* @param string $signature
|
||||
* @param $publicKey
|
||||
* @return bool
|
||||
*/
|
||||
protected function notifyVerify(string $message, string $signature, $publicKey): bool
|
||||
{
|
||||
if (($result = openssl_verify($message, base64_decode($signature), $publicKey, OPENSSL_ALGO_SHA256)) === false) {
|
||||
throw new \UnexpectedValueException('Verified the input $message failed, please checking your $publicKey whether or nor correct.');
|
||||
}
|
||||
return $result === 1;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $thing
|
||||
* @param string $type
|
||||
* @return OpenSSLAsymmetricKey
|
||||
*/
|
||||
protected function rsaFrom($thing, string $type = KEY_TYPE_PRIVATE): OpenSSLAsymmetricKey
|
||||
{
|
||||
$pkey = (($isPublic = $type === KEY_TYPE_PUBLIC) ? openssl_pkey_get_public(file_get_contents($thing)) : openssl_pkey_get_private(file_get_contents($thing)));
|
||||
if (false === $pkey) {
|
||||
throw new \UnexpectedValueException(sprintf('Cannot load %s from(%s), please take care about the $thing input.', $isPublic ? 'publicKey' : 'privateKey', gettype($thing)));
|
||||
}
|
||||
return $pkey;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $ciphertext
|
||||
* @param $nonce
|
||||
* @param $associated_data
|
||||
* @return bool
|
||||
*/
|
||||
public function decode($ciphertext, $nonce, $associated_data): bool
|
||||
{
|
||||
$data = $this->decrypt($ciphertext, $this->payConfig->pay->wx->secret, $nonce, $associated_data);
|
||||
$this->notifyModel = new NotifyModel();
|
||||
$this->notifyModel->amount = $data['amount'];
|
||||
$this->notifyModel->payer = $data['payer'];
|
||||
$this->notifyModel->scene_info = $data['payer'];
|
||||
$this->notifyModel->appid = $data['appid'];
|
||||
$this->notifyModel->mchid = $data['mchid'];
|
||||
$this->notifyModel->out_trade_no = $data['out_trade_no'];
|
||||
$this->notifyModel->transaction_id = $data['transaction_id'];
|
||||
$this->notifyModel->trade_type = $data['trade_type'];
|
||||
$this->notifyModel->trade_state = $data['trade_state'];
|
||||
$this->notifyModel->trade_state_desc = $data['trade_state_desc'];
|
||||
$this->notifyModel->bank_type = $data['bank_type'];
|
||||
$this->notifyModel->attach = $data['attach'];
|
||||
$this->notifyModel->success_time = $data['success_time'];
|
||||
$this->notifyModel->promotion_detail = [];
|
||||
foreach ($data['promotion_detail'] as $datum) {
|
||||
$detail = new PromotionDetail();
|
||||
$detail->amount = $datum['amount'];
|
||||
$detail->wechatpay_contribute = $datum['wechatpay_contribute'];
|
||||
$detail->coupon_id = $datum['coupon_id'];
|
||||
$detail->scope = $datum['scope'];
|
||||
$detail->merchant_contribute = $datum['merchant_contribute'];
|
||||
$detail->name = $datum['name'];
|
||||
$detail->other_contribute = $datum['other_contribute'];
|
||||
$detail->currency = $datum['currency'];
|
||||
$detail->stock_id = $datum['stock_id'];
|
||||
$detail->goods_detail = [];
|
||||
foreach ($datum['goods_detail'] as $value) {
|
||||
$detail->goods_detail[] = new GoodsDetail($value);
|
||||
}
|
||||
$this->notifyModel->promotion_detail[] = $detail;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
<?php
|
||||
|
||||
namespace wchat\wx\V3;
|
||||
|
||||
use Exception;
|
||||
use Kiri\Client;
|
||||
use wchat\common\Help;
|
||||
|
||||
|
||||
/**
|
||||
* Bytes Length of the AES block
|
||||
*/
|
||||
const BLOCK_SIZE = 16;
|
||||
|
||||
/**
|
||||
* Bytes length of the AES secret key.
|
||||
*/
|
||||
const KEY_LENGTH_BYTE = 32;
|
||||
|
||||
/**
|
||||
* Bytes Length of the authentication tag in AEAD cipher mode
|
||||
* @deprecated 1.0 - As of the OpenSSL described, the `auth_tag` length may be one of 16, 15, 14, 13, 12, 8 or 4.
|
||||
* Keep it only compatible for the samples on the official documentation.
|
||||
*/
|
||||
const AUTH_TAG_LENGTH_BYTE = 16;
|
||||
|
||||
/**
|
||||
* The `aes-256-gcm` algorithm string
|
||||
*/
|
||||
const ALGO_AES_256_GCM = 'aes-256-gcm';
|
||||
|
||||
/**
|
||||
* The `aes-256-ecb` algorithm string
|
||||
*/
|
||||
const ALGO_AES_256_ECB = 'aes-256-ecb';
|
||||
|
||||
trait WxV3PaymentTait
|
||||
{
|
||||
|
||||
/**
|
||||
* @param $orderNo
|
||||
* @param $total
|
||||
* @return array
|
||||
*/
|
||||
public function getInitCore($orderNo, $total): array
|
||||
{
|
||||
$payConfig = $this->getPayConfig();
|
||||
if ($payConfig->typeIsApp()) {
|
||||
$body['appid'] = $payConfig->pay->wx->appId;
|
||||
} else {
|
||||
$body['appid'] = $payConfig->appId;
|
||||
}
|
||||
$body['mchid'] = $payConfig->pay->wx->mchId;
|
||||
$body['description'] = $payConfig->getBody();
|
||||
$body['out_trade_no'] = $orderNo;
|
||||
$body['notify_url'] = $payConfig->getNotifyUrl();
|
||||
$body['amount'] = ['total' => $total, 'currency' => $payConfig->getCurrency()];
|
||||
return $body;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $sign
|
||||
* @param string $json
|
||||
* @return Client
|
||||
*/
|
||||
public function createClient(string $sign, string $json): Client
|
||||
{
|
||||
$client = new Client('api.mch.weixin.qq.com', 443, TRUE);
|
||||
$client->withAddedHeader('Authorization', $sign)
|
||||
->withContentType('application/json')->withAddedHeader('User-Agent', 'application/json')
|
||||
->withBody($json)->withAddedHeader("Accept", "*/*");
|
||||
|
||||
$proxyHost = $this->getConfig()->getProxyHost();
|
||||
$proxyPort = $this->getConfig()->getProxyPort();
|
||||
if (!empty($proxyHost) && $proxyPort > 0) {
|
||||
$client->withProxyHost($proxyHost)->withProxyPort($proxyPort);
|
||||
}
|
||||
return $client;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $http_method
|
||||
* @param string $canonical_url
|
||||
* @param string $body
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
public function signature(string $http_method, string $canonical_url, string $body = ''): string
|
||||
{
|
||||
$payConfig = $this->getPayConfig();
|
||||
|
||||
$rand = md5(random_bytes(32));
|
||||
$time = time();
|
||||
|
||||
$message = sprintf("%s\n%s\n%d\n%s\n%s\n", $http_method, $canonical_url, $time, $rand, $body);
|
||||
|
||||
$sign = $this->openssl_signature($message);
|
||||
|
||||
return sprintf('%s mchid="%s",nonce_str="%s",timestamp="%d",serial_no="%s",signature="%s"', $payConfig->pay->wx->schema,
|
||||
$payConfig->pay->wx->mchId, $rand, $time, $payConfig->pay->wx->SerialNumber, $sign);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $body
|
||||
* @return string
|
||||
*/
|
||||
public function openssl_signature($body): string
|
||||
{
|
||||
$payConfig = $this->getPayConfig();
|
||||
|
||||
$mch_private_key = openssl_get_privatekey($payConfig->pay->wx->mchKey);
|
||||
|
||||
openssl_sign($body, $raw_sign, $mch_private_key, 'sha256WithRSAEncryption');
|
||||
return base64_encode($raw_sign);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $json
|
||||
* @param $body
|
||||
* @return array
|
||||
*/
|
||||
private function createResponse($json, $body): array
|
||||
{
|
||||
$responseArray['appId'] = $body['appid'];
|
||||
$responseArray['timeStamp'] = (string)time();
|
||||
$responseArray['nonceStr'] = Help::random(32);
|
||||
$responseArray['package'] = "prepay_id=" . $json['prepay_id'];
|
||||
|
||||
$responseBody = $responseArray['appId'] . PHP_EOL . $responseArray['timeStamp'] . PHP_EOL . $responseArray['nonceStr'] . PHP_EOL . $responseArray['package'] . PHP_EOL;
|
||||
|
||||
$responseArray['signType'] = 'RSA';
|
||||
$responseArray['signBody'] = $responseBody;
|
||||
$responseArray['paySign'] = $this->openssl_signature($responseBody);
|
||||
$responseArray['prepay_id'] = $json['prepay_id'];
|
||||
|
||||
return $responseArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $ciphertext
|
||||
* @param string $v3Key
|
||||
* @param string $iv
|
||||
* @param string $aad
|
||||
* @return array
|
||||
*/
|
||||
public function decrypt(string $ciphertext, string $v3Key, string $iv = '', string $aad = ''): array
|
||||
{
|
||||
$ciphertext = base64_decode($ciphertext);
|
||||
$authTag = substr($ciphertext, $tailLength = 0 - BLOCK_SIZE);
|
||||
$tagLength = strlen($authTag);
|
||||
|
||||
/* Manually checking the length of the tag, because the `openssl_decrypt` was mentioned there, it's the caller's responsibility. */
|
||||
if ($tagLength > BLOCK_SIZE || ($tagLength < 12 && $tagLength !== 8 && $tagLength !== 4)) {
|
||||
throw new \RuntimeException('The inputs `$ciphertext` incomplete, the bytes length must be one of 16, 15, 14, 13, 12, 8 or 4.');
|
||||
}
|
||||
$plaintext = openssl_decrypt(substr($ciphertext, 0, $tailLength), ALGO_AES_256_GCM, $v3Key, OPENSSL_RAW_DATA, $iv, $authTag, $aad);
|
||||
if (false === $plaintext) {
|
||||
throw new \UnexpectedValueException('Decrypting the input $ciphertext failed, please checking your $key and $iv whether or nor correct.');
|
||||
}
|
||||
return json_decode($plaintext, true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace wchat\wx\V3;
|
||||
|
||||
use Exception;
|
||||
use JetBrains\PhpStorm\ArrayShape;
|
||||
use Kiri\Client;
|
||||
use wchat\wx\SmallProgram;
|
||||
|
||||
class WxV3Withdrawal extends SmallProgram
|
||||
{
|
||||
|
||||
|
||||
use WxV3PaymentTait;
|
||||
|
||||
|
||||
/**
|
||||
* @param $orderNo
|
||||
* @param string $batch_name
|
||||
* @param string $batch_remark
|
||||
* @param TransferDetail[] $details
|
||||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
public function payment($orderNo, string $batch_name, string $batch_remark, array $details): array
|
||||
{
|
||||
$body = $this->create($orderNo, $batch_name, $batch_remark, $details);
|
||||
|
||||
$sign = $this->signature('POST', '/v3/pay/transactions/batches', $json = json_encode($body, JSON_UNESCAPED_UNICODE));
|
||||
|
||||
$client = $this->createClient($sign, $json);
|
||||
$client->post('/v3/pay/transactions/batches');
|
||||
$client->close();
|
||||
|
||||
$json = json_decode($client->getBody(), TRUE);
|
||||
if (!isset($json['prepay_id'])) {
|
||||
throw new Exception('微信支付调用失败');
|
||||
}
|
||||
|
||||
return $this->createResponse($json, $body);
|
||||
}
|
||||
|
||||
|
||||
#[ArrayShape(['transfer_detail_list' => "array", 'total_amount' => "int", 'total_num' => "int", 'batch_remark' => "string", 'batch_name' => "string", 'out_batch_no' => ""])]
|
||||
private function create($orderNo, string $batch_name, string $batch_remark, array $details): array
|
||||
{
|
||||
$total = 0;
|
||||
$body = ['transfer_detail_list' => []];
|
||||
$body['out_batch_no'] = $orderNo;
|
||||
$body['batch_name'] = $batch_name;
|
||||
$body['batch_remark'] = $batch_remark;
|
||||
$body['total_num'] = count($details);
|
||||
foreach ($details as $detail) {
|
||||
$total += $detail->transfer_amount;
|
||||
$body['transfer_detail_list'][] = $detail->toArray();
|
||||
}
|
||||
$body['total_amount'] = $total;
|
||||
return $body;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user