Advertisement
Guest User

Untitled

a guest
May 4th, 2017
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.15 KB | None | 0 0
  1. <?php namespace Laravel\Cashier;
  2.  
  3. use Exception;
  4. use Stripe_Event;
  5. use Stripe_Customer;
  6. use Illuminate\Routing\Controller;
  7. use Illuminate\Support\Facades\App;
  8. use Illuminate\Support\Facades\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  11. use Person;
  12. use Order;
  13. use OrderItem;
  14. use Item;
  15.  
  16.  
  17. class WebhookController extends Controller
  18. {
  19.  
  20.     /**
  21.      * Handle a Stripe webhook call.
  22.      *
  23.      * @return \Symfony\Component\HttpFoundation\Response
  24.      */
  25.  
  26.  
  27.     public function handleWebhook()
  28.     {
  29.  
  30.         $payload = $this->getJsonPayload();
  31.  
  32.         try {
  33.             $var = Stripe_Event::retrieve($payload['id']);
  34.         } catch (Exception $e) {
  35.             return $e->getMessage();
  36.         }
  37.  
  38.         if (!$this->eventExistsOnStripe($payload['id'])) {
  39.             return "doesn't exist onstripe";
  40.         }
  41.  
  42.         $method = 'handle' . studly_case(str_replace('.', '_', $payload['type']));
  43.  
  44.         if (method_exists($this, $method)) {
  45.             return $this->{$method}($payload);
  46.         } else {
  47.             return $this->missingMethod();
  48.         }
  49.     }
  50.  
  51.     /**
  52.      * Verify with Stripe that the event is genuine.
  53.      *
  54.      * @param  string $id
  55.      * @return bool
  56.      */
  57.     protected function eventExistsOnStripe($id)
  58.     {
  59.  
  60.         try {
  61.             return !is_null(Stripe_Event::retrieve($id));
  62.         } catch (Exception $e) {
  63.  
  64.             return false;
  65.         }
  66.     }
  67.  
  68.     protected function handleChargeSucceeded(array $payload)
  69.     {
  70.         return true
  71.     }
  72.  
  73.     /**
  74.      * Handle a failed payment from a Stripe subscription.
  75.      *
  76.      * @param  array $payload
  77.      * @return \Symfony\Component\HttpFoundation\Response
  78.      */
  79.     protected function handleInvoicePaymentFailed(array $payload)
  80.     {
  81.         if ($this->tooManyFailedPayments($payload)) {
  82.             $billable = $this->getBillable($payload['data']['object']['customer']);
  83.  
  84.             if ($billable) $billable->subscription()->cancel();
  85.         }
  86.  
  87.         return new Response('Webhook Handled', 200);
  88.     }
  89.  
  90.     /**
  91.      * Determine if the invoice has too many failed attempts.
  92.      *
  93.      * @param  array $payload
  94.      * @return bool
  95.      */
  96.     protected function tooManyFailedPayments(array $payload)
  97.     {
  98.         return $payload['data']['object']['attempt_count'] > 3;
  99.     }
  100.  
  101.     /**
  102.      * Get the billable entity instance by Stripe ID.
  103.      *
  104.      * @param  string $stripeId
  105.      * @return \Laravel\Cashier\BillableInterface
  106.      */
  107.     protected function getBillable($stripeId)
  108.     {
  109.         return App::make('Laravel\Cashier\BillableRepositoryInterface')->find($stripeId);
  110.     }
  111.  
  112.     /**
  113.      * Get the JSON payload for the request.
  114.      *
  115.      * @return array
  116.      */
  117.     protected function getJsonPayload()
  118.     {
  119.  
  120.  
  121.         return (array)json_decode(Request::getContent(), true);
  122.     }
  123.  
  124.     /**
  125.      * Handle calls to missing methods on the controller.
  126.      *
  127.      * @param  array $parameters
  128.      * @return mixed
  129.      */
  130.     public function missingMethod($parameters = array())
  131.     {
  132.         return new Response;
  133.     }
  134.  
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement