Skip to content

Commit 431dfde

Browse files
Add calls for Coupons (#183)
1 parent d3c2592 commit 431dfde

21 files changed

+1035
-0
lines changed

src/Gateway.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,4 +338,54 @@ public function completePurchase(array $parameters = array())
338338
{
339339
return $this->createRequest('\Omnipay\Stripe\Message\CompletePurchaseRequest', $parameters);
340340
}
341+
342+
/**
343+
* @param array $parameters
344+
*
345+
* @return \Omnipay\Common\Message\AbstractRequest
346+
*/
347+
public function createCoupon(array $parameters = array())
348+
{
349+
return $this->createRequest('\Omnipay\Stripe\Message\CreateCouponRequest', $parameters);
350+
}
351+
352+
/**
353+
* @param array $parameters
354+
*
355+
* @return \Omnipay\Common\Message\AbstractRequest
356+
*/
357+
public function fetchCoupon(array $parameters = array())
358+
{
359+
return $this->createRequest('\Omnipay\Stripe\Message\FetchCouponRequest', $parameters);
360+
}
361+
362+
/**
363+
* @param array $parameters
364+
*
365+
* @return \Omnipay\Common\Message\AbstractRequest
366+
*/
367+
public function deleteCoupon(array $parameters = array())
368+
{
369+
return $this->createRequest('\Omnipay\Stripe\Message\DeleteCouponRequest', $parameters);
370+
}
371+
372+
/**
373+
* @param array $parameters
374+
*
375+
* @return \Omnipay\Common\Message\AbstractRequest
376+
*/
377+
public function updateCoupon(array $parameters = array())
378+
{
379+
return $this->createRequest('\Omnipay\Stripe\Message\UpdateCouponRequest', $parameters);
380+
}
381+
382+
/**
383+
* @param array $parameters
384+
*
385+
* @return \Omnipay\Common\Message\AbstractRequest
386+
*/
387+
public function listCoupons(array $parameters = array())
388+
{
389+
return $this->createRequest('\Omnipay\Stripe\Message\ListCouponsRequest', $parameters);
390+
}
341391
}
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
<?php
2+
3+
namespace Omnipay\Stripe\Message;
4+
5+
use Omnipay\Common\Exception\InvalidRequestException;
6+
7+
/**
8+
* Stripe Create Coupon Request
9+
*
10+
* @see \Omnipay\Stripe\Gateway
11+
* @link https://stripe.com/docs/api/coupons/create
12+
*/
13+
class CreateCouponRequest extends AbstractRequest
14+
{
15+
/**
16+
* @return int
17+
*/
18+
public function getAmountOff()
19+
{
20+
return $this->getParameter('amount_off');
21+
}
22+
23+
/**
24+
* @param int $value
25+
*
26+
* @return CreateCouponRequest
27+
*/
28+
public function setAmountOff($value)
29+
{
30+
return $this->setParameter('amount_off', $value);
31+
}
32+
33+
/**
34+
* @return int
35+
*/
36+
public function getPercentOff()
37+
{
38+
return $this->getParameter('percent_off');
39+
}
40+
41+
/**
42+
* @param int $value
43+
*
44+
* @return CreateCouponRequest
45+
*/
46+
public function setPercentOff($value)
47+
{
48+
return $this->setParameter('percent_off', $value);
49+
}
50+
51+
/**
52+
* @return int
53+
*/
54+
public function getDurationInMonths()
55+
{
56+
return $this->getParameter('duration_in_months');
57+
}
58+
59+
/**
60+
* @param int $value
61+
*
62+
* @return CreateCouponRequest
63+
*/
64+
public function setDurationInMonths($value)
65+
{
66+
return $this->setParameter('duration_in_months', $value);
67+
}
68+
69+
/**
70+
* @return string
71+
*/
72+
public function getName()
73+
{
74+
return $this->getParameter('name');
75+
}
76+
77+
/**
78+
* @param string $value
79+
*
80+
* @return CreateCouponRequest
81+
*/
82+
public function setName($value)
83+
{
84+
return $this->setParameter('name', $value);
85+
}
86+
87+
/**
88+
* @return int
89+
*/
90+
public function getMaxRedemptions()
91+
{
92+
return $this->getParameter('max_redemptions');
93+
}
94+
95+
/**
96+
* @param int $value
97+
*
98+
* @return CreateCouponRequest
99+
*/
100+
public function setMaxRedemptions($value)
101+
{
102+
return $this->setParameter('duration_in_months', $value);
103+
}
104+
105+
/**
106+
* @return int
107+
*/
108+
public function getRedeemBy()
109+
{
110+
return $this->getParameter('redeem_by');
111+
}
112+
113+
/**
114+
* @param int $value
115+
*
116+
* @return CreateCouponRequest
117+
*/
118+
public function setRedeemBy($value)
119+
{
120+
return $this->setParameter('redeem_by', $value);
121+
}
122+
123+
/**
124+
* @return string
125+
*/
126+
public function getId()
127+
{
128+
return $this->getParameter('id');
129+
}
130+
131+
/**
132+
* @param string $value
133+
*
134+
* @return CreateCouponRequest
135+
*/
136+
public function setId($value)
137+
{
138+
return $this->setParameter('id', $value);
139+
}
140+
141+
/**
142+
* @return string
143+
*/
144+
public function getDuration()
145+
{
146+
return $this->getParameter('duration');
147+
}
148+
149+
/**
150+
* @param string $value
151+
*
152+
* @return CreateCouponRequest
153+
*/
154+
public function setDuration($value)
155+
{
156+
return $this->setParameter('duration', $value);
157+
}
158+
159+
/**
160+
* {@inheritdoc}
161+
*/
162+
public function getData()
163+
{
164+
$this->validate('duration');
165+
166+
$amountOff = $this->getAmountOff();
167+
$percentOff = $this->getPercentOff();
168+
169+
if (!isset($amountOff) && !isset($percentOff)) {
170+
throw new InvalidRequestException("Either amount_off or percent_off is required");
171+
}
172+
173+
$data = [
174+
'id' => $this->getId(),
175+
'duration' => $this->getDuration(),
176+
'amount_off' => $this->getAmountOff(),
177+
'currency' => $this->getCurrency(),
178+
'duration_in_months' => $this->getDurationInMonths(),
179+
'name' => $this->getName(),
180+
'max_redemptions' => $this->getMaxRedemptions(),
181+
'percent_off' => $this->getPercentOff(),
182+
'redeem_by' => $this->getRedeemBy(),
183+
];
184+
185+
if ($this->getMetadata()) {
186+
$data['metadata'] = $this->getMetadata();
187+
}
188+
189+
return $data;
190+
}
191+
192+
public function getEndpoint()
193+
{
194+
return $this->endpoint.'/coupons';
195+
}
196+
197+
public function getHttpMethod()
198+
{
199+
return 'POST';
200+
}
201+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Omnipay\Stripe\Message;
4+
5+
/**
6+
* Stripe Delete Coupon Request
7+
*
8+
* @see \Omnipay\Stripe\Gateway
9+
* @link https://stripe.com/docs/api/coupons/delete
10+
*/
11+
class DeleteCouponRequest extends AbstractRequest
12+
{
13+
/**
14+
* Get the source id.
15+
*
16+
* @return string
17+
*/
18+
public function getCouponId()
19+
{
20+
return $this->getParameter('couponId');
21+
}
22+
23+
/**
24+
* Set the coupon id.
25+
*
26+
* @param string $value
27+
*
28+
* @return DeleteCouponRequest provides a fluent interface
29+
*/
30+
public function setCouponId($value)
31+
{
32+
return $this->setParameter('couponId', $value);
33+
}
34+
35+
public function getData()
36+
{
37+
$this->validate('couponId');
38+
}
39+
40+
public function getEndpoint()
41+
{
42+
return $this->endpoint.'/coupons/'.$this->getCouponId();
43+
}
44+
45+
public function getHttpMethod()
46+
{
47+
return 'DELETE';
48+
}
49+
}

src/Message/FetchCouponRequest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Omnipay\Stripe\Message;
4+
5+
/**
6+
* Stripe Fetch Coupon Request
7+
*
8+
* @see \Omnipay\Stripe\Gateway
9+
* @link https://stripe.com/docs/api/coupons/retrieve
10+
*/
11+
class FetchCouponRequest extends AbstractRequest
12+
{
13+
/**
14+
* Get the coupon id.
15+
*
16+
* @return string
17+
*/
18+
public function getCouponId()
19+
{
20+
return $this->getParameter('couponId');
21+
}
22+
23+
/**
24+
* Set the coupon id.
25+
*
26+
* @param string
27+
* @return FetchCouponRequest provides a fluent interface.
28+
*/
29+
public function setCouponId($value)
30+
{
31+
return $this->setParameter('couponId', $value);
32+
}
33+
34+
public function getData()
35+
{
36+
$this->validate('couponId');
37+
}
38+
39+
public function getEndpoint()
40+
{
41+
return $this->endpoint.'/coupons/'.$this->getCouponId();
42+
}
43+
44+
public function getHttpMethod()
45+
{
46+
return 'GET';
47+
}
48+
}

0 commit comments

Comments
 (0)