Skip to content

Commit 1bb39f7

Browse files
committed
发送代金券
1 parent 9d7b151 commit 1bb39f7

File tree

3 files changed

+195
-0
lines changed

3 files changed

+195
-0
lines changed

src/BaseAbstractGateway.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,4 +274,14 @@ public function queryBank($parameters = array())
274274
{
275275
return $this->createRequest('\Omnipay\WechatPay\Message\QueryBankRequest', $parameters);
276276
}
277+
278+
/**
279+
* @param array $parameters
280+
*
281+
* @return \Omnipay\WechatPay\Message\CouponTransfersResponse
282+
*/
283+
public function sendCoupon($parameters = array())
284+
{
285+
return $this->createRequest('\Omnipay\WechatPay\Message\CouponTransfersRequest', $parameters);
286+
}
277287
}
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
<?php
2+
3+
namespace Omnipay\WechatPay\Message;
4+
5+
use GuzzleHttp\Client;
6+
use Omnipay\Common\Exception\InvalidRequestException;
7+
use Omnipay\Common\Message\ResponseInterface;
8+
use Omnipay\WechatPay\Helper;
9+
10+
/**
11+
* Class CouponTransfersRequest
12+
*
13+
* @package Omnipay\WechatPay\Message
14+
* @link https://pay.weixin.qq.com/wiki/doc/api/tools/sp_coupon.php?chapter=12_3
15+
* @method CouponTransfersResponse send()
16+
*/
17+
class CouponTransfersRequest extends BaseAbstractRequest
18+
{
19+
protected $endpoint = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/send_coupon';
20+
21+
22+
/**
23+
* Get the raw data array for this message. The format of this varies from gateway to
24+
* gateway, but will usually be either an associative array, or a SimpleXMLElement.
25+
* @return mixed
26+
* @throws InvalidRequestException
27+
*/
28+
public function getData()
29+
{
30+
$this->validate('coupon_stock_id', 'openid_count', 'partner_trade_no', 'openid', 'app_id', 'mch_id', 'cert_path', 'key_path');
31+
32+
$data = array(
33+
'coupon_stock_id' => $this->getCouponStockId(),
34+
'openid_count' => $this->getOpenIdCount(),
35+
'partner_trade_no' => $this->getPartnerTradeNo(),
36+
'openid' => $this->getOpenId(),
37+
'appid' => $this->getAppId(),
38+
'mch_id' => $this->getMchId(),
39+
'nonce_str' => md5(uniqid()),
40+
);
41+
$data = array_filter($data);
42+
43+
$data['sign'] = Helper::sign($data, $this->getApiKey());
44+
45+
return $data;
46+
}
47+
48+
/**
49+
* @return mixed
50+
*/
51+
public function getCouponStockId()
52+
{
53+
return $this->getParameter('coupon_stock_id');
54+
}
55+
56+
/**
57+
* @param mixed $couponStockId
58+
*/
59+
public function setCouponStockId($couponStockId)
60+
{
61+
$this->setParameter('coupon_stock_id', $couponStockId);
62+
}
63+
64+
/**
65+
* @return mixed
66+
*/
67+
public function getOpenIdCount()
68+
{
69+
return $this->getParameter('openid_count');
70+
}
71+
72+
/**
73+
* @param mixed $openidCount
74+
*/
75+
public function setOpenIdCount($openIdCount)
76+
{
77+
$this->setParameter('openid_count', $openIdCount);
78+
}
79+
80+
/**
81+
* @return mixed
82+
*/
83+
public function getPartnerTradeNo()
84+
{
85+
return $this->getParameter('partner_trade_no');
86+
}
87+
88+
/**
89+
* @param mixed $partnerTradeNo
90+
*/
91+
public function setPartnerTradeNo($partnerTradeNo)
92+
{
93+
$this->setParameter('partner_trade_no', $partnerTradeNo);
94+
}
95+
96+
/**
97+
* @return mixed
98+
*/
99+
public function getOpenId()
100+
{
101+
return $this->getParameter('openid');
102+
}
103+
104+
/**
105+
* @param mixed $openId
106+
*/
107+
public function setOpenId($openid)
108+
{
109+
$this->setParameter('openid', $openid);
110+
}
111+
112+
113+
/**
114+
* @return mixed
115+
*/
116+
public function getCertPath()
117+
{
118+
return $this->getParameter('cert_path');
119+
}
120+
121+
122+
/**
123+
* @param mixed $certPath
124+
*/
125+
public function setCertPath($certPath)
126+
{
127+
$this->setParameter('cert_path', $certPath);
128+
}
129+
130+
131+
/**
132+
* @return mixed
133+
*/
134+
public function getKeyPath()
135+
{
136+
return $this->getParameter('key_path');
137+
}
138+
139+
140+
/**
141+
* @param mixed $keyPath
142+
*/
143+
public function setKeyPath($keyPath)
144+
{
145+
$this->setParameter('key_path', $keyPath);
146+
}
147+
148+
149+
/**
150+
* Send the request with specified data
151+
*
152+
* @param mixed $data The data to send
153+
*
154+
* @return ResponseInterface
155+
*/
156+
public function sendData($data)
157+
{
158+
$body = Helper::array2xml($data);
159+
$client = new Client();
160+
161+
$options = [
162+
'body' => $body,
163+
'verify' => true,
164+
'cert' => $this->getCertPath(),
165+
'ssl_key' => $this->getKeyPath(),
166+
];
167+
$response = $client->request('POST', $this->endpoint, $options)->getBody();
168+
$responseData = Helper::xml2array($response);
169+
170+
return $this->response = new CouponTransfersResponse($this, $responseData);
171+
}
172+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Omnipay\WechatPay\Message;
4+
5+
/**
6+
* Class CouponTransfersResponse
7+
*
8+
* @package Omnipay\WechatPay\Message
9+
* @link hhttps://pay.weixin.qq.com/wiki/doc/api/tools/sp_coupon.php?chapter=12_3
10+
*/
11+
class CouponTransfersResponse extends BaseAbstractResponse
12+
{
13+
}

0 commit comments

Comments
 (0)