101 lines
2.8 KiB
PHP
101 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace wchat\wx\V2;
|
|
|
|
use JetBrains\PhpStorm\ArrayShape;
|
|
use wchat\common\Help;
|
|
|
|
trait WxV2PayTait
|
|
{
|
|
|
|
/**
|
|
* 'appId' => $result['appid'],
|
|
* 'nonceStr' => $result['nonce_str'],
|
|
* 'package' => 'prepay_id=' . $result['prepay_id'],
|
|
* 'signType' => 'MD5',
|
|
* 'timeStamp' => (string)time(),
|
|
* @param $prepay_id
|
|
* @return array
|
|
*/
|
|
#[ArrayShape(['appId' => "string", 'nonceStr' => "string", 'package' => "string", 'signType' => "string", 'timeStamp' => "string", 'paySign' => "string"])]
|
|
public function reception($prepay_id): array
|
|
{
|
|
$array = [
|
|
'appId' => $this->config->getAppid(),
|
|
'nonceStr' => Help::random(32),
|
|
'package' => 'prepay_id=' . $prepay_id,
|
|
'signType' => 'MD5',
|
|
'timeStamp' => (string)time(),
|
|
];
|
|
$key = $this->config->getKey();
|
|
$sign_type = $this->config->getSignType();
|
|
$array['paySign'] = Help::sign($array, $key, $sign_type);
|
|
return $array;
|
|
}
|
|
|
|
|
|
/**
|
|
* @param string $orderNo
|
|
* @param float|int $total
|
|
* @return array
|
|
*/
|
|
#[ArrayShape(['appid' => "string", 'mch_id' => "string", 'nonce_str' => "string", 'body' => "string", 'out_trade_no' => "string", 'total_fee' => "float|int", 'spbill_create_ip' => "mixed", 'notify_url' => "string", 'trade_type' => "string"])]
|
|
protected function getInitCore(string $orderNo, float|int $total): array
|
|
{
|
|
return [
|
|
'appid' => $this->config->getAppid(),
|
|
'mch_id' => $this->config->getMchId(),
|
|
'nonce_str' => Help::random(32),
|
|
'body' => $this->config->getBody(),
|
|
'out_trade_no' => $orderNo,
|
|
'total_fee' => $total,
|
|
'notify_url' => $this->config->getNotifyUrl(),
|
|
'trade_type' => $this->config->getTradeType(),
|
|
];
|
|
}
|
|
|
|
|
|
/**
|
|
* @param array $data
|
|
* @return string
|
|
*/
|
|
protected function sign(array $data): string
|
|
{
|
|
$key = $this->config->getKey();
|
|
$sign_type = $this->config->getSignType();
|
|
|
|
$data['sign'] = Help::sign($data, $key, $sign_type);
|
|
|
|
return Help::toXml($data);
|
|
}
|
|
|
|
/**
|
|
* @param string $orderNo
|
|
* @param int|float $money
|
|
* @param string $openid
|
|
* @return string
|
|
*/
|
|
protected function builder(string $orderNo, int|float $money, string $openid): string
|
|
{
|
|
$data = [
|
|
'appid' => $this->config->getAppid(),
|
|
'mch_id' => $this->config->getMchId(),
|
|
'nonce_str' => Help::random(32),
|
|
'body' => $this->config->getBody(),
|
|
'out_trade_no' => $orderNo,
|
|
'total_fee' => $money,
|
|
'spbill_create_ip' => $_SERVER['REMOTE_ADDR'],
|
|
'notify_url' => $this->config->getNotifyUrl(),
|
|
'trade_type' => $this->config->getTradeType(),
|
|
'openid' => $openid
|
|
];
|
|
|
|
$key = $this->config->getKey();
|
|
$sign_type = $this->config->getSignType();
|
|
|
|
$data['sign'] = Help::sign($data, $key, $sign_type);
|
|
|
|
return Help::toXml($data);
|
|
}
|
|
}
|