| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace App\Http\Controllers\TicketShop; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use App\Exceptions\PaymentProviderException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use App\Http\Controllers\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use App\PaymentProvider\Mollie; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use App\Purchase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use App\User; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Illuminate\Contracts\Routing\ResponseFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Illuminate\Database\Eloquent\ModelNotFoundException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Illuminate\Http\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Illuminate\Http\Response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * Handles all mollie.com related requests | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @package App\Http\Controllers\Ticketshop | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 19 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  | class MollieController extends Controller | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |      * Instance of the type-hinted Mollie-PaymentProvider | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |      * @var Mollie | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     protected $paymentProvider; | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      * Create a new Controller instance with the Mollie-PaymentProvider via DPI | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      *  | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      * @param Mollie $paymentProvider  | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      * @return void  | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     public function __construct(Mollie $paymentProvider) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 36 |  |  |         $this->paymentProvider = $paymentProvider; | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |      * Process POST-Requests from Mollie to published Webhooks. | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |      * They only contain an id-field referencing a previously created payment. | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |      *  | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |      * @param Request $request  | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |      * @return Response|ResponseFactory  | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     public function processWebhook(Request $request) | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         $id = $request->input('id'); | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         // Check if a purchase with the given payment id exists | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |         // Someone could try to cause high load on webservice by | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         // requesting the webhook endpoint with fake ids | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         $purchase = Purchase::firstWhere('payment_id', $id); | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         if( !$purchase ) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |             return response('PaymentId invalid', 403); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         $this->paymentProvider->updatePayment($id); | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         return response('Payment processed', 200); | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |      * Update the state of the given purchase with the payment-state @mollie.com | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |      *  | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |      * @param Purchase $purchase Any purchase that has been submitted for payment to mollie.com | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |      * @return Response|ResponseFactory State of the given Purchase | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |      * @throws PaymentProviderException  | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |      * @throws ModelNotFoundException  | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     public function getPaymentUpdate(Purchase $purchase) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         // First check, if the purchase is linked to mollie. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         // Else we would trigger unneccessary api calls | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $mollie = User::firstWhere('email', '[email protected]'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         if( $purchase->vendor->id !== $mollie->id ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             return response('Not a mollie purchase!', 403); // 403 = Forbidden | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         // Trigger the update by calling the mollie-api | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $this->paymentProvider->updatePayment($purchase->payment_id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         // Refresh the model in order to have its current state | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         $purchase->refresh(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         // Just send back the purchase's state. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 87 |  |  |         return response($purchase->state); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 88 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 89 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |  |