| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace App\Containers\Stripe\Tasks; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use App\Containers\Stripe\Exceptions\StripeAccountNotFoundException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use App\Containers\Stripe\Exceptions\StripeApiErrorException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use App\Containers\User\Models\User; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use App\Ship\Parents\Tasks\Task; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Cartalyst\Stripe\Stripe; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Illuminate\Support\Facades\Config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * Class ChargeWithStripeTask. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @author Mahmoud Zalt <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | class ChargeWithStripeTask extends Task | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     public $stripe; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * StripeApi constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @param \Cartalyst\Stripe\Stripe $stripe | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     public function __construct(Stripe $stripe) | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |         $this->stripe = $stripe->make(Config::get('services.stripe.secret'), Config::get('services.stripe.version')); | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @param \App\Containers\User\Models\User $user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @param                                  $amount | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @param string                           $currency | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @return  array|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     public function run(User $user, $amount, $currency = 'USD') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $stripeAccount = $user->stripeAccount; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         if(!$stripeAccount){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             throw new StripeAccountNotFoundException('We could not find your credit card information.  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             For security reasons, we do not store your credit card information on our server.  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             So please login to our Web App and enter your credit card information directly into Stripe,  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             then try to purchase the credits again.  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             Thanks.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             $response = $this->stripe->charges()->create([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                 'customer' => $user->stripeAccount->customer_id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                 'currency' => $currency, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                 'amount'   => $amount, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         } catch (Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             throw (new StripeApiErrorException('Stripe API error (chargeCustomer)'))->debug($e->getMessage(), true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         if ($response['status'] != 'succeeded') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             throw new StripeApiErrorException('Stripe response status not succeeded (chargeCustomer)'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         if ($response['paid'] !== true) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         // this data will be stored on the pivot table (user credits) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             'payment_method' => 'stripe', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             'description'    => $response['id'] // the charge id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 80 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 81 |  |  |  |