Skip to content

Commit

Permalink
[2.x] Fix existing customers in Paddle issue (#220)
Browse files Browse the repository at this point in the history
* Fix existing customers in Paddle issue

* Apply fixes from StyleCI

---------

Co-authored-by: StyleCI Bot <[email protected]>
  • Loading branch information
driesvints and StyleCIBot authored Dec 12, 2023
1 parent 163ef33 commit 5afbbfa
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
12 changes: 10 additions & 2 deletions src/Concerns/ManagesCustomer.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,18 @@ public function createAsCustomer(array $options = [])

unset($options['trial_ends_at']);

$response = Cashier::api('POST', 'customers', $options)['data'];
// Attempt to find the customer by email address first...
$response = Cashier::api('GET', 'customers', [
'status' => 'active,archived',
'search' => $options['email'],
])['data'][0] ?? null;

// If we can't find the customer by email, we'll create them on Paddle...
if (is_null($response)) {
$response = Cashier::api('POST', 'customers', $options)['data'];
}

$customer = $this->customer()->make();

$customer->paddle_id = $response['id'];
$customer->name = $response['name'];
$customer->email = $response['email'];
Expand Down
12 changes: 6 additions & 6 deletions tests/Feature/CustomerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ public function test_billable_models_can_create_a_customer_record()
$user = $this->createUser();

Cashier::fake([
'customers' => [
'data' => [
'customers*' => [
'data' => [[
'id' => 'cus_123456789',
'name' => $user->name,
'email' => $user->email,
],
]],
],
]);

Expand Down Expand Up @@ -46,12 +46,12 @@ public function test_trial_ends_at_works_if_generic_trial_is_expired()
$user = $this->createUser();

Cashier::fake([
'customers' => [
'data' => [
'customers*' => [
'data' => [[
'id' => 'cus_123456789',
'name' => $user->name,
'email' => $user->email,
],
]],
],
]);

Expand Down
6 changes: 3 additions & 3 deletions tests/Feature/FeatureTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ protected function createBillable($description = 'taylor', array $options = []):
$user = $this->createUser($description);

Cashier::fake([
'customers' => [
'data' => [
'customers*' => [
'data' => [[
'id' => 'cus_123456789',
'name' => $user->name,
'email' => $user->email,
],
]],
],
]);

Expand Down

0 comments on commit 5afbbfa

Please sign in to comment.