2026-03-17 16:48:52 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace wchat\wx\V3;
|
|
|
|
|
|
|
|
|
|
use Kiri\Client;
|
|
|
|
|
use wchat\wx\SmallProgram;
|
|
|
|
|
use wchat\wx\V3\Libs\XPayGoodsDeliverNotify;
|
|
|
|
|
|
|
|
|
|
class WxVirtualPayment extends SmallProgram
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $session_key
|
|
|
|
|
* @param string $projectId
|
|
|
|
|
* @param float $price
|
|
|
|
|
* @param string $orderNo
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function payment(string $session_key, string $projectId, float $price, string $orderNo, array $attach = []): array
|
|
|
|
|
{
|
|
|
|
|
$signData = [
|
|
|
|
|
'offerId' => $this->getPayConfig()->pay->wx->offerId,
|
|
|
|
|
'buyQuantity' => 1,
|
|
|
|
|
'env' => 0,
|
|
|
|
|
'currencyType' => 'CNY',
|
|
|
|
|
'productId' => $projectId,
|
|
|
|
|
'goodsPrice' => $price,
|
|
|
|
|
'outTradeNo' => $orderNo,
|
|
|
|
|
'attach' => json_encode($attach, JSON_UNESCAPED_UNICODE),
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$signJson = json_encode($signData);
|
|
|
|
|
return [
|
|
|
|
|
'signData' => $signData,
|
|
|
|
|
'mode' => 'short_series_goods',
|
|
|
|
|
'paySig' => $this->paySign('requestVirtualPayment', $signJson),
|
|
|
|
|
'signature' => $this->sign($session_key, $signJson),
|
|
|
|
|
'session_key' => $session_key,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/***
|
|
|
|
|
* @param string $prefix
|
|
|
|
|
* @param string $data
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
private function paySign(string $prefix, string $data): string
|
|
|
|
|
{
|
|
|
|
|
return hash_hmac('sha256', $prefix . '&' . $data, $this->getPayConfig()->pay->wx->appKey);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $session_key
|
|
|
|
|
* @param string $data
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
private function sign(string $session_key, string $data): string
|
|
|
|
|
{
|
|
|
|
|
return hash_hmac('sha256', $data, $session_key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $openId
|
|
|
|
|
* @param string $orderNo
|
|
|
|
|
* @return bool|array
|
|
|
|
|
*/
|
|
|
|
|
public function queryOrder(string $openId, string $orderNo): bool|array
|
|
|
|
|
{
|
|
|
|
|
$data = json_encode(['openid' => $openId, 'order_id' => $orderNo, 'env' => 0], JSON_UNESCAPED_UNICODE);
|
|
|
|
|
|
|
|
|
|
$client = new Client('api.weixin.qq.com', 443, true);
|
|
|
|
|
$client->post('/xpay/query_order?access_token=' . $this->payConfig->getAccessToken() . '&pay_sig=' . $this->paySign('/xpay/query_order', $data), $data);
|
2026-06-12 23:57:22 +08:00
|
|
|
$resp = $client->body;
|
2026-03-17 16:48:52 +08:00
|
|
|
|
|
|
|
|
$client->close();
|
|
|
|
|
|
|
|
|
|
return json_decode($resp, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $orderNo
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function notify_provide_goods(string $orderNo): array
|
|
|
|
|
{
|
|
|
|
|
$data = json_encode(['order_id' => $orderNo, 'env' => 0], JSON_UNESCAPED_UNICODE);
|
|
|
|
|
|
|
|
|
|
$client = new Client('api.weixin.qq.com', 443, true);
|
|
|
|
|
$client->post('/xpay/notify_provide_goods?access_token=' . $this->payConfig->getAccessToken() . '&pay_sig=' . $this->paySign('/xpay/notify_provide_goods', $data), $data);
|
2026-06-12 23:57:22 +08:00
|
|
|
$resp = $client->body;
|
2026-03-17 16:48:52 +08:00
|
|
|
$client->close();
|
|
|
|
|
|
|
|
|
|
return json_decode($resp, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|