|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Code4mk\LaraStripe\Lib; |
| 4 | + |
| 5 | +use Stripe\StripeClient; |
| 6 | + |
| 7 | +class StripePromotion |
| 8 | +{ |
| 9 | + /** |
| 10 | + * Secret key |
| 11 | + * |
| 12 | + * @var string |
| 13 | + */ |
| 14 | + private $secretKey; |
| 15 | + |
| 16 | + private $stripe; |
| 17 | + private $coupon; |
| 18 | + private $max_redemptions = ''; |
| 19 | + |
| 20 | + public function __construct() |
| 21 | + { |
| 22 | + $this->secretKey = config('lara-stripe.secret_key'); |
| 23 | + $this->stripe = new StripeClient($this->secretKey); |
| 24 | + } |
| 25 | + |
| 26 | + public function couponId($id) |
| 27 | + { |
| 28 | + $this->coupon = $id; |
| 29 | + return $this; |
| 30 | + } |
| 31 | + |
| 32 | + public function maxRedem($data) |
| 33 | + { |
| 34 | + $this->max_redemptions = $data; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Create promotion & retrieve data |
| 39 | + * |
| 40 | + * @return object |
| 41 | + */ |
| 42 | + public function create() |
| 43 | + { |
| 44 | + try { |
| 45 | + $promotionData = [ |
| 46 | + 'coupon' => $this->coupon |
| 47 | + ]; |
| 48 | + |
| 49 | + if ($this->max_redemptions !== '') { |
| 50 | + $promotionData['max_redemptions'] = $this->max_redemptions; |
| 51 | + } |
| 52 | + |
| 53 | + $promotion = $this->stripe->promotionCodes->create($promotionData); |
| 54 | + return $promotion; |
| 55 | + } catch (\Exception $e) { |
| 56 | + return (object) ['isError' => 'true', 'message' => $e->getMessage(), 'stripe' => $e->getJsonBody()['error']]; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + public function retrieve($id) |
| 61 | + { |
| 62 | + try { |
| 63 | + $promotion = $this->stripe->promotionCodes->retrieve($id); |
| 64 | + return $promotion; |
| 65 | + } catch (\Exception $e) { |
| 66 | + return (object) ['isError' => 'true', 'message' => $e->getMessage(), 'stripe' => $e->getJsonBody()['error']]; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + public function deactive($id) |
| 71 | + { |
| 72 | + try { |
| 73 | + $promotion = $this->stripe->promotionCodes->update($id,[ |
| 74 | + 'active' => false |
| 75 | + ]); |
| 76 | + return $promotion; |
| 77 | + } catch (\Exception $e) { |
| 78 | + return (object) ['isError' => 'true', 'message' => $e->getMessage(), 'stripe' => $e->getJsonBody()['error']]; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + public function active($id) |
| 83 | + { |
| 84 | + try { |
| 85 | + $promotion = $this->stripe->promotionCodes->update($id,[ |
| 86 | + 'active' => true |
| 87 | + ]); |
| 88 | + return $promotion; |
| 89 | + } catch (\Exception $e) { |
| 90 | + return (object) ['isError' => 'true', 'message' => $e->getMessage(), 'stripe' => $e->getJsonBody()['error']]; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + public function lists() |
| 95 | + { |
| 96 | + try { |
| 97 | + $coupon = $this->stripe->promotionCodes->all(); |
| 98 | + return $coupon; |
| 99 | + } catch (\Exception $e) { |
| 100 | + return (object) ['isError' => 'true', 'message' => $e->getMessage(), 'stripe' => $e->getJsonBody()['error']]; |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments