add clear
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace wchat\wx\V2;
|
||||
|
||||
use Kiri\Client;
|
||||
use wchat\common\Help;
|
||||
use wchat\common\Result;
|
||||
use wchat\wx\SmallProgram;
|
||||
|
||||
class WxV2AppPayment extends SmallProgram
|
||||
{
|
||||
|
||||
|
||||
private string $uniformed = '/pay/unifiedorder';
|
||||
|
||||
use WxV2PayTait;
|
||||
|
||||
|
||||
/**
|
||||
* @param int $money
|
||||
* @param string $orderNo
|
||||
* @param string $spbill_create_ip
|
||||
* @return Result
|
||||
*/
|
||||
public function payment(int $money, string $orderNo, string $spbill_create_ip = '127.0.0.1'): Result
|
||||
{
|
||||
if ($money < 0) {
|
||||
return new Result(code: 400, message: '充值金额不能小于0.');
|
||||
}
|
||||
|
||||
$body = $this->getInitCore($orderNo, $money);
|
||||
$body['trade_type'] = 'APP';
|
||||
$body['spbill_create_ip'] = $spbill_create_ip;
|
||||
|
||||
$client = new Client('api.mch.weixin.qq.com', 443, true);
|
||||
$client->withHeader(['Content-Type' => 'application/json']);
|
||||
$client->withBody($this->sign($body));
|
||||
$client->post($this->uniformed);
|
||||
$client->close();
|
||||
|
||||
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||
return new Result(code: 505, message: 'network error.');
|
||||
}
|
||||
$data = Help::toArray($client->getBody());
|
||||
if (isset($data['return_code']) && $data['return_code'] != 'SUCCESS') {
|
||||
return new Result(code: 503, message: $data['return_msg']);
|
||||
}
|
||||
if ($data['result_code'] != 'SUCCESS') {
|
||||
return new Result(code: 504, message: $data['err_code_des']);
|
||||
}
|
||||
|
||||
return new Result(code: 0, data: $this->reception($data['prepayid']));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $prepay_id
|
||||
* @return string
|
||||
*/
|
||||
public function reception(string $prepay_id): string
|
||||
{
|
||||
return $this->sign([
|
||||
'appId' => $this->config->getAppid(),
|
||||
'partnerid' => $this->config->getMchId(),
|
||||
'prepayid' => $prepay_id,
|
||||
'package' => 'Sign=WXPay',
|
||||
'noncestr' => Help::random(32),
|
||||
'timestamp' => time()
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,210 @@
|
||||
<?php
|
||||
|
||||
namespace wchat\wx\V2;
|
||||
|
||||
use Kiri\Client;
|
||||
use Phalcon\Cache\Frontend\Json;
|
||||
use wchat\common\Help;
|
||||
use wchat\common\Result;
|
||||
use wchat\wx\SmallProgram;
|
||||
|
||||
class WxV2PayJsApi extends SmallProgram
|
||||
{
|
||||
|
||||
private string $uniformed = '/pay/unifiedorder';
|
||||
|
||||
use WxV2PayTait;
|
||||
|
||||
|
||||
/**
|
||||
* @param int $money
|
||||
* @param string $orderNo
|
||||
* @param string $openId
|
||||
* @param string $spbill_create_ip
|
||||
* @return Result
|
||||
*/
|
||||
public function applet(int $money, string $orderNo, string $openId, string $spbill_create_ip = '127.0.0.1'): Result
|
||||
{
|
||||
if ($money < 0) {
|
||||
return new Result(code: 400, message: '充值金额不能小于0.');
|
||||
}
|
||||
|
||||
$body = $this->getInitCore($orderNo, $money);
|
||||
$body['trade_type'] = 'JSAPI';
|
||||
$body['spbill_create_ip'] = $spbill_create_ip;
|
||||
$body['openid'] = $openId;
|
||||
|
||||
$client = new Client('api.mch.weixin.qq.com', 443, true);
|
||||
$client->withHeader(['Content-Type' => 'application/json']);
|
||||
$client->withBody($this->sign($body));
|
||||
$client->post($this->uniformed);
|
||||
$client->close();
|
||||
|
||||
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||
return new Result(code: 505, message: 'network error.');
|
||||
}
|
||||
$data = Help::toArray($client->getBody());
|
||||
if (isset($data['return_code']) && $data['return_code'] != 'SUCCESS') {
|
||||
return new Result(code: 503, message: $data['return_msg']);
|
||||
}
|
||||
if ($data['result_code'] != 'SUCCESS') {
|
||||
return new Result(code: 504, message: $data['err_code_des']);
|
||||
}
|
||||
|
||||
return new Result(code: 0, data: $this->reception($data['prepayid']));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param int $money
|
||||
* @param string $orderNo
|
||||
* @param string $app_name
|
||||
* @param string $package_name
|
||||
* @param string $spbill_create_ip
|
||||
* @return Result
|
||||
*/
|
||||
public function h5Android(int $money, string $orderNo, string $app_name, string $package_name, string $spbill_create_ip = '127.0.0.1'): Result
|
||||
{
|
||||
if ($money < 0) {
|
||||
return new Result(code: 400, message: '充值金额不能小于0.');
|
||||
}
|
||||
|
||||
$body = $this->getInitCore($orderNo, $money);
|
||||
$body['trade_type'] = 'MWEB';
|
||||
$body['spbill_create_ip'] = $spbill_create_ip;
|
||||
$body['scene_info'] = json_encode([
|
||||
'h5_info' => [
|
||||
'type' => 'Android',
|
||||
'app_name' => $app_name,
|
||||
'package_name' => $package_name
|
||||
]
|
||||
], JSON_UNESCAPED_UNICODE);
|
||||
|
||||
$client = new Client('api.mch.weixin.qq.com', 443, true);
|
||||
$client->withHeader(['Content-Type' => 'application/json']);
|
||||
$client->withBody($this->sign($body));
|
||||
$client->post($this->uniformed);
|
||||
$client->close();
|
||||
|
||||
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||
return new Result(code: 505, message: 'network error.');
|
||||
}
|
||||
$data = Help::toArray($client->getBody());
|
||||
if (isset($data['return_code']) && $data['return_code'] != 'SUCCESS') {
|
||||
return new Result(code: 503, message: $data['return_msg']);
|
||||
}
|
||||
if ($data['result_code'] != 'SUCCESS') {
|
||||
return new Result(code: 504, message: $data['err_code_des']);
|
||||
}
|
||||
|
||||
return new Result(code: 0, data: $this->reception($data['prepayid']));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param int $money
|
||||
* @param string $orderNo
|
||||
* @param string $app_name
|
||||
* @param string $bundle_id
|
||||
* @param string $spbill_create_ip
|
||||
* @return Result
|
||||
*/
|
||||
public function h5Ios(int $money, string $orderNo, string $app_name, string $bundle_id, string $spbill_create_ip = '127.0.0.1'): Result
|
||||
{
|
||||
if ($money < 0) {
|
||||
return new Result(code: 400, message: '充值金额不能小于0.');
|
||||
}
|
||||
|
||||
$body = $this->getInitCore($orderNo, $money);
|
||||
$body['trade_type'] = 'MWEB';
|
||||
$body['spbill_create_ip'] = $spbill_create_ip;
|
||||
$body['scene_info'] = json_encode([
|
||||
'h5_info' => [
|
||||
'type' => 'IOS',
|
||||
'app_name' => $app_name,
|
||||
'bundle_id' => $bundle_id
|
||||
]
|
||||
], JSON_UNESCAPED_UNICODE);
|
||||
|
||||
$client = new Client('api.mch.weixin.qq.com', 443, true);
|
||||
$client->withHeader(['Content-Type' => 'application/json']);
|
||||
$client->withBody($this->sign($body));
|
||||
$client->post($this->uniformed);
|
||||
$client->close();
|
||||
|
||||
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||
return new Result(code: 505, message: 'network error.');
|
||||
}
|
||||
$data = Help::toArray($client->getBody());
|
||||
if (isset($data['return_code']) && $data['return_code'] != 'SUCCESS') {
|
||||
return new Result(code: 503, message: $data['return_msg']);
|
||||
}
|
||||
if ($data['result_code'] != 'SUCCESS') {
|
||||
return new Result(code: 504, message: $data['err_code_des']);
|
||||
}
|
||||
|
||||
return new Result(code: 0, data: $this->reception($data['prepayid']));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param int $money
|
||||
* @param string $orderNo
|
||||
* @param string $wap_url
|
||||
* @param string $wap_name
|
||||
* @param string $spbill_create_ip
|
||||
* @return Result
|
||||
*/
|
||||
public function h5(int $money, string $orderNo, string $wap_url, string $wap_name, string $spbill_create_ip = '127.0.0.1'): Result
|
||||
{
|
||||
if ($money < 0) {
|
||||
return new Result(code: 400, message: '充值金额不能小于0.');
|
||||
}
|
||||
|
||||
$body = $this->getInitCore($orderNo, $money);
|
||||
$body['trade_type'] = 'MWEB';
|
||||
$body['spbill_create_ip'] = $spbill_create_ip;
|
||||
$body['scene_info'] = json_encode([
|
||||
'h5_info' => [
|
||||
'type' => 'IOS',
|
||||
'wap_url' => $wap_url,
|
||||
'wap_name' => $wap_name
|
||||
]
|
||||
], JSON_UNESCAPED_UNICODE);
|
||||
|
||||
$client = new Client('api.mch.weixin.qq.com', 443, true);
|
||||
$client->withHeader(['Content-Type' => 'application/json']);
|
||||
$client->withBody($this->sign($body));
|
||||
$client->post($this->uniformed);
|
||||
$client->close();
|
||||
|
||||
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||
return new Result(code: 505, message: 'network error.');
|
||||
}
|
||||
$data = Help::toArray($client->getBody());
|
||||
if (isset($data['return_code']) && $data['return_code'] != 'SUCCESS') {
|
||||
return new Result(code: 503, message: $data['return_msg']);
|
||||
}
|
||||
if ($data['result_code'] != 'SUCCESS') {
|
||||
return new Result(code: 504, message: $data['err_code_des']);
|
||||
}
|
||||
|
||||
return new Result(code: 0, data: $this->reception($data['prepayid']));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $prepay_id
|
||||
* @return string
|
||||
*/
|
||||
public function reception(string $prepay_id): string
|
||||
{
|
||||
return $this->sign([
|
||||
'signType' => $this->config->getSignType(),
|
||||
'package' => 'prepay_id=' . $prepay_id,
|
||||
'nonceStr' => Help::random(32),
|
||||
'timestamp' => time()
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace wchat\wx\V2;
|
||||
|
||||
use Kiri\Client;
|
||||
use wchat\common\Help;
|
||||
use wchat\common\Result;
|
||||
use wchat\wx\SmallProgram;
|
||||
|
||||
class WxV2Withdrawal extends SmallProgram
|
||||
{
|
||||
|
||||
private string $transfers = '/mmpaymkttransfers/promotion/transfers';
|
||||
|
||||
/**
|
||||
* @param int|float $money
|
||||
* @param string $openid
|
||||
* @param string $order
|
||||
* @param string $desc
|
||||
* @return Result
|
||||
*/
|
||||
public function payment(int|float $money, string $openid, string $order, string $desc = '零钱提现'): Result
|
||||
{
|
||||
$array = [
|
||||
'nonce_str' => Help::random(32),
|
||||
'partner_trade_no' => $order,
|
||||
'mchid' => $this->config->getMchId(),
|
||||
'mch_appid' => $this->config->getAppid(),
|
||||
'openid' => $openid,
|
||||
'check_name' => 'NO_CHECK',
|
||||
'amount' => $money * 100,
|
||||
'spbill_create_ip' => $this->config->getRemoteAddr(),
|
||||
'desc' => $desc,
|
||||
];
|
||||
|
||||
$key = $this->config->getKey();
|
||||
$sign_type = $this->config->getSignType();
|
||||
$array['sign'] = Help::sign($array, $key, $sign_type);
|
||||
|
||||
$client = new Client('api.mch.weixin.qq.com', 443, true);
|
||||
$client->withHeader(['Content-Type' => 'application/json']);
|
||||
$client->withBody($body = Help::toXml($array));
|
||||
$client->post($this->transfers);
|
||||
$client->close();
|
||||
|
||||
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
|
||||
return new Result(code: 505, message: 'network error.');
|
||||
}
|
||||
|
||||
$data = Help::toArray($client->getBody());
|
||||
|
||||
$data['body'] = $body;
|
||||
if ($data['return_code'] == 'FAIL') {
|
||||
return new Result(code: $array['return_code'], message: $data['return_msg'], data: $data);
|
||||
} else if ($array['result_code'] != 'SUCCESS') {
|
||||
return new Result(code: $array['err_code'], message: $data['err_code_des'], data: $data);
|
||||
} else {
|
||||
return new Result(code: 0, message: '提现成功', data: $data);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user