G2A Many GEOs
SHARE
TWEET

WebhookControllerTest.php

a guest Jan 22nd, 2020 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. namespace App\Tests\Controller;
  3.  
  4. use DateTime;
  5. use App\Entity\User;
  6. use App\Entity\Subscription;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
  9. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  10.  
  11. class WebhookControllerTest extends WebTestCase
  12. {
  13.     /** @var EntityManagerInterface */
  14.     private $em;
  15.     private $container;
  16.  
  17.     public function setUp()
  18.     {
  19.         self::bootKernel();
  20.         $this->container = self::$kernel->getContainer();
  21.         $this->em = $this->container->get('doctrine')->getManager();
  22.     }
  23.  
  24.     public function testStripeCustomerSubscriptionDeleted()
  25.     {
  26.         $subscription = $this->createSubscription();
  27.  
  28.         $eventJson = $this->getCustomerSubscriptionDeletedEvent($subscription->getStripeSubscriptionId());
  29.  
  30.         $client = $this->createClient();
  31.         $client->request(
  32.             'POST',
  33.             'webhooks/stripe',
  34.             [],
  35.             [],
  36.             [],
  37.             $eventJson
  38.         );
  39.  
  40.         dump($client->getResponse()->getContent());
  41.         $this->assertEquals(200, $client->getResponse()->getStatusCode());
  42.  
  43.         $this->assertFalse($subscription->isActive());
  44.     }
  45.  
  46.     private function createSubscription()
  47.     {
  48.         $user = new User();
  49.         $user->setEmail('fluffy'.mt_rand().'@sheep.com');
  50.         $user->setUsername('fluffy'.mt_rand());
  51.         $user->setPassword("password");
  52.  
  53.         $subscription = new Subscription();
  54.         $subscription->setUser($user);
  55.         $subscription->activateSubscription(
  56.             'plan_STRIPE_TEST_ABC'.mt_rand(),
  57.             'sub_STRIPE_TEST_XYZ'.mt_rand(),
  58.             new \DateTime('+1 month')
  59.         );
  60.  
  61.         $this->em->persist($user);
  62.         $this->em->persist($subscription);
  63.         $this->em->flush();
  64.  
  65.         return $subscription;
  66.     }
  67.  
  68.     private function getCustomerSubscriptionDeletedEvent($subscriptionId)
  69.     {
  70.         $json = <<<EOF
  71. {
  72.     "created": 1326853478,
  73.     "livemode": false,
  74.     "id": "evt_00000000000000",
  75.     "type": "customer.subscription.deleted",
  76.     "object": "event",
  77.     "request": null,
  78.     "pending_webhooks": 1,
  79.     "api_version": "2019-12-03",
  80.     "data": {
  81.         "object": {
  82.         "id": "sub_00000000000000",
  83.         "object": "subscription",
  84.         "application_fee_percent": null,
  85.         "billing_cycle_anchor": 1579617600,
  86.         "billing_thresholds": null,
  87.         "cancel_at": null,
  88.         "cancel_at_period_end": false,
  89.         "canceled_at": null,
  90.         "collection_method": "charge_automatically",
  91.         "created": 1579617600,
  92.         "current_period_end": 1582296000,
  93.         "current_period_start": 1579617600,
  94.         "customer": "cus_00000000000000",
  95.         "days_until_due": null,
  96.         "default_payment_method": null,
  97.         "default_source": null,
  98.         "default_tax_rates": [
  99.    
  100.         ],
  101.         "discount": null,
  102.         "ended_at": 1579694523,
  103.         "items": {
  104.             "object": "list",
  105.             "data": [
  106.             {
  107.                 "id": "%s",
  108.                 "object": "subscription_item",
  109.                 "billing_thresholds": null,
  110.                 "created": 1579617601,
  111.                 "metadata": {
  112.                 },
  113.                 "plan": {
  114.                 "id": "farmer_00000000000000",
  115.                 "object": "plan",
  116.                 "active": true,
  117.                 "aggregate_usage": null,
  118.                 "amount": 9900,
  119.                 "amount_decimal": "9900",
  120.                 "billing_scheme": "per_unit",
  121.                 "created": 1579548278,
  122.                 "currency": "eur",
  123.                 "interval": "month",
  124.                 "interval_count": 1,
  125.                 "livemode": false,
  126.                 "metadata": {
  127.                 },
  128.                 "nickname": "Farmer Brent (monthly)",
  129.                 "product": "prod_00000000000000",
  130.                 "tiers": null,
  131.                 "tiers_mode": null,
  132.                 "transform_usage": null,
  133.                 "trial_period_days": null,
  134.                 "usage_type": "licensed"
  135.                 },
  136.                 "quantity": 1,
  137.                 "subscription": "sub_00000000000000",
  138.                 "tax_rates": [
  139.    
  140.                 ]
  141.             }
  142.             ],
  143.             "has_more": false,
  144.             "url": "/v1/subscription_items?subscription=sub_GaZ1c9WocA6CcW"
  145.         },
  146.         "latest_invoice": "in_1G3NtcGgCQgXBLKXe6AFO0Wf",
  147.         "livemode": false,
  148.         "metadata": {
  149.         },
  150.         "next_pending_invoice_item_invoice": null,
  151.         "pending_invoice_item_interval": null,
  152.         "pending_setup_intent": null,
  153.         "pending_update": null,
  154.         "plan": {
  155.             "id": "farmer_00000000000000",
  156.             "object": "plan",
  157.             "active": true,
  158.             "aggregate_usage": null,
  159.             "amount": 9900,
  160.             "amount_decimal": "9900",
  161.             "billing_scheme": "per_unit",
  162.             "created": 1579548278,
  163.             "currency": "eur",
  164.             "interval": "month",
  165.             "interval_count": 1,
  166.             "livemode": false,
  167.             "metadata": {
  168.             },
  169.             "nickname": "Farmer Brent (monthly)",
  170.             "product": "prod_00000000000000",
  171.             "tiers": null,
  172.             "tiers_mode": null,
  173.             "transform_usage": null,
  174.             "trial_period_days": null,
  175.             "usage_type": "licensed"
  176.         },
  177.         "quantity": 1,
  178.         "schedule": null,
  179.         "start_date": 1579617600,
  180.         "status": "canceled",
  181.         "tax_percent": null,
  182.         "trial_end": null,
  183.         "trial_start": null
  184.         }
  185.     }
  186.     }
  187. EOF;
  188.  
  189.         return sprintf($json, $subscriptionId);
  190.     }
  191. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top