Skip to content

Commit 05a6e9f

Browse files
committed
add helpers file
1 parent f48ddab commit 05a6e9f

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ Stripe Concepts:
6464
- Create a new stripe account
6565
- Use the stripe CLI to login to newly created account
6666
- Create a [stripe test clock](https://dashboard.stripe.com/test/billing/subscriptions/test-clocks) to simulate time
67-
- This can be done through either the API or dashboard. You should get a test clock id that you'll add to your .env file (needed to run the fixture)
67+
- This can be done through API. You should get a test clock id that you'll add to your .env file (needed to run the fixture)
6868
- Example: `STRIPE_TEST_CLOCK=clock_1PesQ4CAFqbekblQs2oyiGaq`
6969
- Once you've added your test clock you can use stripe cli to run your fixture. Fixture is located at fixtures/seed.json
7070
- The fixture will populate customer and subscription data into your stripe account

app/helpers.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
if (!function_exists('writeNewEnvironmentFileWithClock')) {
4+
function writeNewEnvironmentFileWithClock($clockId): bool
5+
{
6+
$replaced = preg_replace(
7+
clockReplacementPattern(),
8+
'STRIPE_TEST_CLOCK='.$clockId,
9+
$input = file_get_contents(app()->environmentFilePath())
10+
);
11+
12+
if ($replaced === $input || $replaced === null) {
13+
throw new Exception('Unable to set application key. No STRIPE_TEST_CLOCK variable was found in the .env file.');
14+
}
15+
16+
file_put_contents(app()->environmentFilePath(), $replaced);
17+
18+
return true;
19+
}
20+
}
21+
22+
if (!function_exists('clockReplacementPattern')) {
23+
function clockReplacementPattern(): string
24+
{
25+
$escaped = preg_quote('='.env('STRIPE_TEST_CLOCK'), '/');
26+
27+
return "/^STRIPE_TEST_CLOCK{$escaped}/m";
28+
}
29+
}

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@
2323
"App\\": "app/",
2424
"Database\\Factories\\": "database/factories/",
2525
"Database\\Seeders\\": "database/seeders/"
26-
}
26+
},
27+
"files": [
28+
"app/helpers.php"
29+
]
2730
},
2831
"autoload-dev": {
2932
"psr-4": {

0 commit comments

Comments
 (0)