|
| 1 | +<?php |
| 2 | +namespace Code4mk\LaraStripe; |
| 3 | + |
| 4 | +/** |
| 5 | + * @author @code4mk <[email protected]> |
| 6 | + * @author @kawsarsoft <[email protected]> |
| 7 | + * @copyright Kawsar Soft. (http://kawsarsoft.com) |
| 8 | + */ |
| 9 | + |
| 10 | +use Stripe\Checkout\Session; |
| 11 | +use Stripe\PaymentIntent; |
| 12 | +use Stripe\Product; |
| 13 | +use Stripe\Stripe; |
| 14 | +use Stripe\SKU; |
| 15 | +use Config; |
| 16 | + |
| 17 | +class StripeRequestPayment |
| 18 | +{ |
| 19 | + /** |
| 20 | + * secret key |
| 21 | + * @var string |
| 22 | + */ |
| 23 | + |
| 24 | + private $secretKey; |
| 25 | + private $title; |
| 26 | + private $description; |
| 27 | + private $amount; |
| 28 | + private $currency; |
| 29 | + |
| 30 | + public function __construct() |
| 31 | + { |
| 32 | + if(config::get('lara-stripe.driver') === 'config') { |
| 33 | + $this->secretKey = config::get('lara-stripe.secret_key'); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Set secret key |
| 39 | + * @param array $data |
| 40 | + * @return $this |
| 41 | + */ |
| 42 | + public function setup($data) |
| 43 | + { |
| 44 | + if ($data['secret_key']) { |
| 45 | + $this->secretKey = $data['secret_key']; |
| 46 | + } |
| 47 | + return $this; |
| 48 | + } |
| 49 | + |
| 50 | + public function title($title) |
| 51 | + { |
| 52 | + $this->title = $title; |
| 53 | + return $this; |
| 54 | + } |
| 55 | + |
| 56 | + public function description($des) |
| 57 | + { |
| 58 | + $this->description = $des; |
| 59 | + return $this; |
| 60 | + } |
| 61 | + |
| 62 | + public function amount($amount,$currency='usd') |
| 63 | + { |
| 64 | + $this->amount = round($amount,2) * 100; |
| 65 | + $this->currency = $currency; |
| 66 | + return $this; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Get balance |
| 71 | + * @return object |
| 72 | + */ |
| 73 | + public function get() |
| 74 | + { |
| 75 | + try { |
| 76 | + Stripe::setApiKey($this->secretKey); |
| 77 | + $product = Product::create([ |
| 78 | + 'name' => $this->title, |
| 79 | + 'type' => 'good', |
| 80 | + 'description' => $this->description, |
| 81 | + 'attributes' => ['name'], |
| 82 | + ]); |
| 83 | + |
| 84 | + $skus = SKU::create([ |
| 85 | + 'attributes' => [ |
| 86 | + 'name' => $this->title |
| 87 | + ], |
| 88 | + 'price' => $this->amount, |
| 89 | + 'currency' => $this->currency, |
| 90 | + 'inventory' => [ |
| 91 | + 'type' => 'infinite', |
| 92 | + ], |
| 93 | + 'product' => $product->id, |
| 94 | + ]); |
| 95 | + |
| 96 | + $output = (object) [ |
| 97 | + 'skus' => $skus->id, |
| 98 | + 'req_id' => $product->id |
| 99 | + ]; |
| 100 | + return $output; |
| 101 | + |
| 102 | + } catch (\Exception $e) { |
| 103 | + return (object)['isError' => 'true','message'=> $e->getMessage()]; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + public function status($sessionToken) |
| 108 | + { |
| 109 | + try { |
| 110 | + Stripe::setApiKey($this->secretKey); |
| 111 | + $session = Session::retrieve($sessionToken); |
| 112 | + $skus = $session->display_items[0]->sku->id; |
| 113 | + $req_id = $session->display_items[0]->sku->product; |
| 114 | + $pi = PaymentIntent::retrieve( |
| 115 | + $session->payment_intent |
| 116 | + ); |
| 117 | + if ($pi->status === 'succeeded') { |
| 118 | + return (object) [ |
| 119 | + 'status' => $pi->status, |
| 120 | + 'skus' => $skus, |
| 121 | + 'req_id' => $req_id |
| 122 | + ]; |
| 123 | + } |
| 124 | + return (object) [ |
| 125 | + 'status' => $pi->status, |
| 126 | + 'skus' => $skus, |
| 127 | + 'req_id' => $req_id |
| 128 | + ]; |
| 129 | + |
| 130 | + } catch (\Exception $e) { |
| 131 | + return (object)['isError' => 'true','message'=> $e->getMessage()]; |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments