|
@@ -14,122 +14,110 @@ use WeChatPay\BuilderChainable;
|
|
|
|
|
|
|
|
trait ApiTrait
|
|
trait ApiTrait
|
|
|
{
|
|
{
|
|
|
- public function __construct(
|
|
|
|
|
- private readonly Config $config,
|
|
|
|
|
- private readonly ExtensionPaymentEntity $extensionPaymentEntity,
|
|
|
|
|
- private ?BuilderChainable $builderChainable = null,
|
|
|
|
|
- )
|
|
|
|
|
- {
|
|
|
|
|
- }
|
|
|
|
|
- private function getBuilderChainable(): BuilderChainable
|
|
|
|
|
- {
|
|
|
|
|
-
|
|
|
|
|
- if ($this->builderChainable === null) {
|
|
|
|
|
- $this->builderChainable = Builder::factory([
|
|
|
|
|
- 'mchid' => $this->config->mchid,
|
|
|
|
|
- 'serial' => $this->config->serial_no,
|
|
|
|
|
- 'privateKey' => $this->config->apiclient_key,
|
|
|
|
|
- 'certs' => [
|
|
|
|
|
- $this->config->platform_no => $this->config->platform_cert,
|
|
|
|
|
- $this->config->public_key_id => $this->config->public_key,
|
|
|
|
|
- ],
|
|
|
|
|
- ]);
|
|
|
|
|
- }
|
|
|
|
|
- return $this->builderChainable;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- private function handleAsyncRequest(PromiseInterface $promise)
|
|
|
|
|
- {
|
|
|
|
|
- return $promise
|
|
|
|
|
- ->then(function ($response) {
|
|
|
|
|
- Log::info('微信支付异步回调返回数据:' . $response->getBody());
|
|
|
|
|
- return json_decode((string)$response->getBody());
|
|
|
|
|
- })
|
|
|
|
|
- ->otherwise(function ($e) {
|
|
|
|
|
- if ($e instanceof \GuzzleHttp\Exception\RequestException && $e->hasResponse()) {
|
|
|
|
|
-
|
|
|
|
|
- $errorBody = json_decode((string)$e->getResponse()->getBody());
|
|
|
|
|
- throw new ErrorException(message: trim($errorBody->message));
|
|
|
|
|
- }
|
|
|
|
|
- throw $e;
|
|
|
|
|
- })
|
|
|
|
|
- ->wait();
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ use HandleAsyncRequestTrait;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 微信支付
|
|
* 微信支付
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param string $openid 用户openid
|
|
|
|
|
+ * @param string $outTradeNo 商户订单号
|
|
|
|
|
+ * @param int $total 订单总金额,单位为分
|
|
|
|
|
+ * @param string $description 商品描述
|
|
|
|
|
+ * @param string $notifyUrl 通知地址
|
|
|
|
|
+ * @param int $expireTime 订单过期时间戳
|
|
|
|
|
+ * @return mixed
|
|
|
*/
|
|
*/
|
|
|
- private function wechatPay(string $openid, string $outTradeNo, int $total, string $description, string $notifyUrl, int $expireTime)
|
|
|
|
|
|
|
+ private function wechatPay(string $openid, string $outTradeNo, int $total, string $description, int $expireTime)
|
|
|
{
|
|
{
|
|
|
|
|
|
|
|
// https://pay.weixin.qq.com/doc/v3/merchant/4012791856
|
|
// https://pay.weixin.qq.com/doc/v3/merchant/4012791856
|
|
|
// 【POST】/v3/pay/transactions/jsapi
|
|
// 【POST】/v3/pay/transactions/jsapi
|
|
|
- return $this->handleAsyncRequest($this->getBuilderChainable()->v3->pay->transactions->jsapi->postAsync([
|
|
|
|
|
- 'json' => [
|
|
|
|
|
- 'mchid' => $this->config->mchid,
|
|
|
|
|
- 'out_trade_no' => $outTradeNo,
|
|
|
|
|
- 'appid' => $this->config->appid,
|
|
|
|
|
- 'description' => $description,
|
|
|
|
|
- 'notify_url' => $notifyUrl,
|
|
|
|
|
- // yyyy-MM-DDTHH:mm:ss+TIMEZONE
|
|
|
|
|
- 'time_expire' => date('Y-m-d\TH:i:s+08:00', $expireTime),
|
|
|
|
|
- 'attach' => 'actor',
|
|
|
|
|
- 'amount' => [
|
|
|
|
|
- 'total' => $total,
|
|
|
|
|
- 'currency' => 'CNY'
|
|
|
|
|
|
|
+ return $this->handleAsyncRequest(function (
|
|
|
|
|
+ BuilderChainable $builder, Config $config, string $openid, string $outTradeNo, int $total, string $description, int $expireTime
|
|
|
|
|
+ ): PromiseInterface {
|
|
|
|
|
+ return $builder->v3->pay->transactions->jsapi->postAsync([
|
|
|
|
|
+ 'json' => [
|
|
|
|
|
+ 'mchid' => $config->mchid,
|
|
|
|
|
+ 'out_trade_no' => $outTradeNo,
|
|
|
|
|
+ 'appid' => $config->appid,
|
|
|
|
|
+ 'description' => $description,
|
|
|
|
|
+ 'notify_url' => $config->notify_url,
|
|
|
|
|
+ // yyyy-MM-DDTHH:mm:ss+TIMEZONE
|
|
|
|
|
+ 'time_expire' => date('Y-m-d\TH:i:s+08:00', $expireTime),
|
|
|
|
|
+ 'attach' => 'actor',
|
|
|
|
|
+ 'amount' => [
|
|
|
|
|
+ 'total' => $total,
|
|
|
|
|
+ 'currency' => 'CNY'
|
|
|
|
|
+ ],
|
|
|
|
|
+ 'payer' => [
|
|
|
|
|
+ 'openid' => $openid
|
|
|
|
|
+ ]
|
|
|
],
|
|
],
|
|
|
- 'payer' => [
|
|
|
|
|
- 'openid' => $openid
|
|
|
|
|
- ]
|
|
|
|
|
- ],]));
|
|
|
|
|
|
|
+ ]);
|
|
|
|
|
+ }, $openid, $outTradeNo, $total, $description, $expireTime);
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 退款申请
|
|
* 退款申请
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param string $outRefundNo 商户退款单号
|
|
|
|
|
+ * @param string $outTradeNo 商户订单号
|
|
|
|
|
+ * @param array $amount 退款金额信息
|
|
|
|
|
+ * @param string $reason 退款原因
|
|
|
|
|
+ * @return mixed
|
|
|
*/
|
|
*/
|
|
|
private function domesticRefunds(string $outRefundNo, string $outTradeNo, array $amount, string $reason)
|
|
private function domesticRefunds(string $outRefundNo, string $outTradeNo, array $amount, string $reason)
|
|
|
{
|
|
{
|
|
|
// https://pay.weixin.qq.com/doc/v3/merchant/4012791862
|
|
// https://pay.weixin.qq.com/doc/v3/merchant/4012791862
|
|
|
// 【POST】/v3/refund/domestic/refunds
|
|
// 【POST】/v3/refund/domestic/refunds
|
|
|
- return $this->handleAsyncRequest($this->getBuilderChainable()->v3->refund->domestic->refunds->postAsync([
|
|
|
|
|
- 'json' => [
|
|
|
|
|
- 'out_refund_no' => $outRefundNo,
|
|
|
|
|
- 'out_trade_no' => $outTradeNo,
|
|
|
|
|
- 'reason' => $reason,
|
|
|
|
|
- 'amount' => $amount,
|
|
|
|
|
- 'notify_url' => $this->notifyUrl
|
|
|
|
|
- ]
|
|
|
|
|
- ]));
|
|
|
|
|
|
|
+ return $this->handleAsyncRequest(function (BuilderChainable $builder, Config $config) use ($outRefundNo, $outTradeNo, $amount, $reason): PromiseInterface {
|
|
|
|
|
+ return $builder->v3->refund->domestic->refunds->postAsync([
|
|
|
|
|
+ 'json' => [
|
|
|
|
|
+ 'out_refund_no' => $outRefundNo,
|
|
|
|
|
+ 'out_trade_no' => $outTradeNo,
|
|
|
|
|
+ 'reason' => $reason,
|
|
|
|
|
+ 'amount' => $amount,
|
|
|
|
|
+ 'notify_url' => $config->notifyUrl
|
|
|
|
|
+ ]
|
|
|
|
|
+ ]);
|
|
|
|
|
+ });
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 发起转账
|
|
* 发起转账
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param int $openid 用户openid
|
|
|
|
|
+ * @param string $orderSn 订单号
|
|
|
|
|
+ * @param string $amount 转账金额
|
|
|
|
|
+ * @return mixed
|
|
|
*/
|
|
*/
|
|
|
- private function transferBills(int $uid, string $orderSn, string $amount)
|
|
|
|
|
|
|
+ private function transferBills(int $openid, string $orderSn, string $amount)
|
|
|
{
|
|
{
|
|
|
// https://pay.weixin.qq.com/doc/v3/merchant/4012716434
|
|
// https://pay.weixin.qq.com/doc/v3/merchant/4012716434
|
|
|
//【POST】/v3/fund-app/mch-transfer/transfer-bills
|
|
//【POST】/v3/fund-app/mch-transfer/transfer-bills
|
|
|
- return $this->handleAsyncRequest($this->getBuilderChainable()->v3->fundApp->mchTransfer->transferBills->postAsync([
|
|
|
|
|
- 'json' => [
|
|
|
|
|
- 'appid' => $this->appId,
|
|
|
|
|
- 'openid' => $this->getOpenId($uid, 'routine'),
|
|
|
|
|
- 'out_bill_no' => $orderSn,
|
|
|
|
|
- 'transfer_amount' => (int)($amount * 100),
|
|
|
|
|
- 'notify_url' => $this->notifyUrl,
|
|
|
|
|
- 'transfer_scene_id' => '1005', // 佣金报酬
|
|
|
|
|
- 'transfer_scene_report_infos' => [
|
|
|
|
|
- [
|
|
|
|
|
- 'info_type' => '岗位类型',
|
|
|
|
|
- 'info_content' => '合作运营'
|
|
|
|
|
- ],
|
|
|
|
|
- [
|
|
|
|
|
- 'info_type' => '报酬说明',
|
|
|
|
|
- 'info_content' => sprintf('提现到账%.2f元', $amount)
|
|
|
|
|
|
|
+ return $this->handleAsyncRequest(function (BuilderChainable $builder, Config $config) use ($openid, $orderSn, $amount): PromiseInterface {
|
|
|
|
|
+ return $builder->v3->fundApp->mchTransfer->transferBills->postAsync([
|
|
|
|
|
+ 'json' => [
|
|
|
|
|
+ 'appid' => $config->appId,
|
|
|
|
|
+ 'openid' => $openid,
|
|
|
|
|
+ 'out_bill_no' => $orderSn,
|
|
|
|
|
+ 'transfer_amount' => (int)($amount * 100),
|
|
|
|
|
+ 'notify_url' => $config->notifyUrl,
|
|
|
|
|
+ 'transfer_scene_id' => '1005', // 佣金报酬
|
|
|
|
|
+ 'transfer_scene_report_infos' => [
|
|
|
|
|
+ [
|
|
|
|
|
+ 'info_type' => '岗位类型',
|
|
|
|
|
+ 'info_content' => '合作运营'
|
|
|
|
|
+ ],
|
|
|
|
|
+ [
|
|
|
|
|
+ 'info_type' => '报酬说明',
|
|
|
|
|
+ 'info_content' => sprintf('提现到账%.2f元', $amount)
|
|
|
|
|
+ ],
|
|
|
],
|
|
],
|
|
|
- ],
|
|
|
|
|
- 'transfer_remark' => sprintf('提现%s元到零钱', $amount),
|
|
|
|
|
- ]
|
|
|
|
|
- ]));
|
|
|
|
|
|
|
+ 'transfer_remark' => sprintf('提现%s元到零钱', $amount),
|
|
|
|
|
+ ]
|
|
|
|
|
+ ]);
|
|
|
|
|
+ });
|
|
|
}
|
|
}
|
|
|
-}
|
|
|
|
|
|
|
+}
|