This commit is contained in:
xl
2023-11-14 00:06:57 +08:00
parent d64ffe4b07
commit c97302ae76
6 changed files with 147 additions and 34 deletions
+109 -6
View File
@@ -11,32 +11,40 @@ class AppConfig
/** /**
* @var string * @var string
*/ */
protected string $appId = ''; public string $appId = '';
/** /**
* @var string * @var string
*/ */
protected string $appSecret = ''; public string $appSecret = '';
/** /**
* @var PayConfig * @var PayConfig
*/ */
protected PayConfig $payConfig; public PayConfig $pay;
private string $proxyHost = '';
private int $proxyPort = 0;
private string $notifyUrl = '';
private string $body = '';
private string $currency = 'CNY';
private string $remoteIp = '';
/** /**
* @var int * @var int
*/ */
protected int $type; public int $type;
/** /**
* @var array|string[] * @var array|string[]
*/ */
#[ArrayShape(['token' => 'string', 'secret' => 'string', 'unionId' => 'string'])] #[ArrayShape(['token' => 'string', 'secret' => 'string', 'unionId' => 'string'])]
protected array $notice = ['token' => '', 'secret' => '', 'unionId' => '']; public array $notice = ['token' => '', 'secret' => '', 'unionId' => ''];
/** /**
@@ -49,7 +57,7 @@ class AppConfig
$model->appId = $app->appId; $model->appId = $app->appId;
$model->appSecret = $app->appSecret; $model->appSecret = $app->appSecret;
$model->type = $app->type; $model->type = $app->type;
$model->payConfig = PayConfig::parse($app->pay); $model->pay = PayConfig::parse($app->pay);
return $model; return $model;
} }
@@ -62,4 +70,99 @@ class AppConfig
return $this->type === 3; return $this->type === 3;
} }
/**
* @return string
*/
public function getProxyHost(): string
{
return $this->proxyHost;
}
/**
* @param string $proxyHost
*/
public function setProxyHost(string $proxyHost): void
{
$this->proxyHost = $proxyHost;
}
/**
* @return int
*/
public function getProxyPort(): int
{
return $this->proxyPort;
}
/**
* @param int $proxyPort
*/
public function setProxyPort(int $proxyPort): void
{
$this->proxyPort = $proxyPort;
}
/**
* @return string
*/
public function getNotifyUrl(): string
{
return $this->notifyUrl;
}
/**
* @param string $notifyUrl
*/
public function setNotifyUrl(string $notifyUrl): void
{
$this->notifyUrl = $notifyUrl;
}
/**
* @return string
*/
public function getBody(): string
{
return $this->body;
}
/**
* @param string $body
*/
public function setBody(string $body): void
{
$this->body = $body;
}
/**
* @return string
*/
public function getCurrency(): string
{
return $this->currency;
}
/**
* @param string $currency
*/
public function setCurrency(string $currency): void
{
$this->currency = $currency;
}
/**
* @return string
*/
public function getRemoteIp(): string
{
return $this->remoteIp;
}
/**
* @param string $remoteIp
*/
public function setRemoteIp(string $remoteIp): void
{
$this->remoteIp = $remoteIp;
}
} }
+9 -11
View File
@@ -8,8 +8,6 @@
namespace wchat\common; namespace wchat\common;
use wchat\common\Config;
abstract class Multiprogramming implements Progaram abstract class Multiprogramming implements Progaram
{ {
@@ -23,9 +21,9 @@ abstract class Multiprogramming implements Progaram
/** /**
* @var PayConfig * @var AppConfig
*/ */
protected PayConfig $payConfig; protected AppConfig $payConfig;
/** /**
@@ -72,7 +70,7 @@ abstract class Multiprogramming implements Progaram
/** /**
* @param \wchat\common\Config $config * @param Config $config
* @return void * @return void
*/ */
public function setConfig(Config $config): void public function setConfig(Config $config): void
@@ -81,26 +79,26 @@ abstract class Multiprogramming implements Progaram
} }
/** /**
* @return PayConfig * @return AppConfig
*/ */
public function getPayConfig(): PayConfig public function getPayConfig(): AppConfig
{ {
return $this->payConfig; return $this->payConfig;
} }
/** /**
* @param PayConfig $payConfig * @param AppConfig $payConfig
*/ */
public function setPayConfig(PayConfig $payConfig): void public function setPayConfig(AppConfig $payConfig): void
{ {
$this->payConfig = $payConfig; $this->payConfig = $payConfig;
} }
/** /**
* @return \wchat\common\Config * @return Config
*/ */
public function getConfig(): \wchat\common\Config public function getConfig(): Config
{ {
return $this->config; return $this->config;
} }
+1 -1
View File
@@ -51,6 +51,6 @@ class Wx
/** /**
* @var string * @var string
*/ */
public string $serialNumber; public string $SerialNumber;
} }
+10 -5
View File
@@ -21,12 +21,17 @@ class TransferBatches extends SmallProgram
#[ArrayShape([])] #[ArrayShape([])]
public function request(TransferDetail $detail): array public function request(TransferDetail $detail): array
{ {
$body = []; $payConfig = $this->getPayConfig();
$body['appid'] = $this->getConfig()->getAppid(); $body = [];
if ($payConfig->typeIsApp()) {
$body['appid'] = $payConfig->pay->wx->appId;
} else {
$body['appid'] = $payConfig->appId;
}
$body['out_batch_no'] = $detail->out_detail_no; $body['out_batch_no'] = $detail->out_detail_no;
$body["batch_name"] = $this->getConfig()->getBody(); $body["batch_name"] = $payConfig->getBody();
$body["body"] = $this->getConfig()->getBody(); $body["body"] = $payConfig->getBody();
$body["batch_remark"] = $this->getConfig()->getBody(); $body["batch_remark"] = $payConfig->getBody();
$body["total_amount"] = $detail->transfer_amount; $body["total_amount"] = $detail->transfer_amount;
$body["total_num"] = 1; $body["total_num"] = 1;
$body["transfer_detail_list"] = [$detail->toArray()]; $body["transfer_detail_list"] = [$detail->toArray()];
+2 -2
View File
@@ -54,7 +54,7 @@ class WxV3PaymentNotify extends SmallProgram
*/ */
public function verify(RequestInterface $request): bool public function verify(RequestInterface $request): bool
{ {
$platformPublicKeyInstance = $this->rsaFrom($this->payConfig->wx->mchKey, KEY_TYPE_PUBLIC); $platformPublicKeyInstance = $this->rsaFrom($this->payConfig->pay->wx->mchKey, KEY_TYPE_PUBLIC);
$inWechatpaySignature = $request->getHeaderLine('Wechatpay-Signature'); // 请根据实际情况获取 $inWechatpaySignature = $request->getHeaderLine('Wechatpay-Signature'); // 请根据实际情况获取
$inWechatpayTimestamp = $request->getHeaderLine('Wechatpay-Timestamp'); // 请根据实际情况获取 $inWechatpayTimestamp = $request->getHeaderLine('Wechatpay-Timestamp'); // 请根据实际情况获取
$inWechatpayNonce = $request->getHeaderLine('Wechatpay-Nonce'); // 请根据实际情况获取 $inWechatpayNonce = $request->getHeaderLine('Wechatpay-Nonce'); // 请根据实际情况获取
@@ -127,7 +127,7 @@ class WxV3PaymentNotify extends SmallProgram
*/ */
public function decode($ciphertext, $nonce, $associated_data): bool public function decode($ciphertext, $nonce, $associated_data): bool
{ {
$data = $this->decrypt($ciphertext, $this->payConfig->wx->secret, $nonce, $associated_data); $data = $this->decrypt($ciphertext, $this->payConfig->pay->wx->secret, $nonce, $associated_data);
$this->notifyModel = new NotifyModel(); $this->notifyModel = new NotifyModel();
$this->notifyModel->amount = $data['amount']; $this->notifyModel->amount = $data['amount'];
$this->notifyModel->payer = $data['payer']; $this->notifyModel->payer = $data['payer'];
+16 -9
View File
@@ -44,12 +44,17 @@ trait WxV3PaymentTait
*/ */
public function getInitCore($orderNo, $total): array public function getInitCore($orderNo, $total): array
{ {
$body['appid'] = $this->getConfig()->getAppid(); $payConfig = $this->getPayConfig();
$body['mchid'] = $this->getConfig()->getMchId(); if ($payConfig->typeIsApp()) {
$body['description'] = $this->getConfig()->getBody(); $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['out_trade_no'] = $orderNo;
$body['notify_url'] = $this->getConfig()->getNotifyUrl(); $body['notify_url'] = $payConfig->getNotifyUrl();
$body['amount'] = ['total' => $total, 'currency' => 'CNY']; $body['amount'] = ['total' => $total, 'currency' => $payConfig->getCurrency()];
return $body; return $body;
} }
@@ -84,6 +89,8 @@ trait WxV3PaymentTait
*/ */
public function signature(string $http_method, string $canonical_url, string $body = ''): string public function signature(string $http_method, string $canonical_url, string $body = ''): string
{ {
$payConfig = $this->getPayConfig();
$rand = md5(random_bytes(32)); $rand = md5(random_bytes(32));
$time = time(); $time = time();
@@ -91,8 +98,8 @@ trait WxV3PaymentTait
$sign = $this->openssl_signature($message); $sign = $this->openssl_signature($message);
return sprintf('%s mchid="%s",nonce_str="%s",timestamp="%d",serial_no="%s",signature="%s"', $this->getConfig()->getSchema(), return sprintf('%s mchid="%s",nonce_str="%s",timestamp="%d",serial_no="%s",signature="%s"', $payConfig->pay->wx->schema,
$this->getConfig()->getMchId(), $rand, $time, $this->getConfig()->getSerialNo(), $sign); $payConfig->pay->wx->mchId, $rand, $time, $payConfig->pay->wx->SerialNumber, $sign);
} }
@@ -102,9 +109,9 @@ trait WxV3PaymentTait
*/ */
public function openssl_signature($body): string public function openssl_signature($body): string
{ {
$pem = file_get_contents($this->getConfig()->getMchKey()); $payConfig = $this->getPayConfig();
$mch_private_key = openssl_get_privatekey($pem); $mch_private_key = openssl_get_privatekey($payConfig->pay->wx->mchKey);
openssl_sign($body, $raw_sign, $mch_private_key, 'sha256WithRSAEncryption'); openssl_sign($body, $raw_sign, $mch_private_key, 'sha256WithRSAEncryption');
return base64_encode($raw_sign); return base64_encode($raw_sign);