Skip to content

Commit

Permalink
update test
Browse files Browse the repository at this point in the history
  • Loading branch information
repl6669 committed Nov 20, 2023
1 parent 9369169 commit acad37a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/StripePaymentAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function handleWebhook(Request $request): JsonResponse

$paymentIntentStatus = match (true) {
in_array($event->type, array_keys($statusMap)) => $statusMap[$event->type],
default => 'intent',
default => 'unknown',
};

$paymentIntent = new PaymentIntent(
Expand Down
36 changes: 28 additions & 8 deletions tests/HandleStripeWebhookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@
Config::set('lunar-api.stripe.automatic_payment_methods', false);
$intentId = App::make(StripePaymentAdapter::class)->createIntent($cart)->id;

$this->app->bind(\Lunar\Stripe\Concerns\ConstructsWebhookEvent::class, function ($app) {
return new class implements \Lunar\Stripe\Concerns\ConstructsWebhookEvent
{
public function constructEvent(string $jsonPayload, string $signature, string $secret)
{
return \Stripe\Event::constructFrom([]);
}
};
});

$this->intent = PaymentIntent::retrieve($intentId);

$this->cart = $cart;
Expand All @@ -56,7 +66,7 @@
->post(
'/stripe/webhook',
$data,
['Stripe-Signature' => 'foobar'],
['Stripe-Signature' => $this->determineStripeSignature($data)],
);

$response->assertSuccessful();
Expand All @@ -74,7 +84,11 @@
$data['data']['object']['id'] = $paymentIntentId;

$response = $this
->post('/stripe/webhook', $data);
->post(
'/stripe/webhook',
$data,
['Stripe-Signature' => $this->determineStripeSignature($data)],
);

$response->assertSuccessful();

Expand All @@ -90,23 +104,29 @@
$data['data']['object']['id'] = $this->cart->meta['payment_intent'];

$response = $this
->post('/stripe/webhook', $data);
->post(
'/stripe/webhook',
$data,
['Stripe-Signature' => $this->determineStripeSignature($data)],
);

$response->assertSuccessful();

Event::assertDispatched(OrderPaymentFailed::class);
})->todo();

it('can handle any other event', function () {
$events = Event::fake();

/** @var TestCase $this */
$data = json_decode(file_get_contents(__DIR__.'/Stubs/Stripe/charge.succeeded.json'), true);

$data['data']['object']['id'] = $this->cart->meta['payment_intent'];

$response = $this
->post('/stripe/webhook', $data);
->post(
'/stripe/webhook',
$data,
['Stripe-Signature' => $this->determineStripeSignature($data)],
);

$response->assertSuccessful();

Event::assertNothingDispatched();
})->todo();
16 changes: 16 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,22 @@ public function getEnvironmentSetUp($app): void
]);
}

/**
* Determine the Stripe signature.
*/
protected function determineStripeSignature(array $payload, string $configKey = null): string
{
$secret = Config::get('services.stripe.webhooks.'.($configKey ?? 'payment_intent'));

$timestamp = time();

$timestampedPayload = $timestamp.'.'.json_encode($payload);

$signature = hash_hmac('sha256', $timestampedPayload, $secret);

return "t={$timestamp},v1={$signature}";
}

/**
* Define database migrations.
*/
Expand Down

0 comments on commit acad37a

Please sign in to comment.