Skip to content

Commit 70b581c

Browse files
[15.x] Create stripe customer if not exists or update/sync (#1661)
* create stripe customer or if exists then update/sync * formatting --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent 4235d5e commit 70b581c

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

src/Concerns/ManagesCustomer.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,36 @@ public function createOrGetStripeCustomer(array $options = [])
127127
return $this->createAsStripeCustomer($options);
128128
}
129129

130+
/**
131+
* Update the Stripe customer information for the current user or create one.
132+
*
133+
* @param array $options
134+
* @return \Stripe\Customer
135+
*/
136+
public function updateOrCreateStripeCustomer(array $options = [])
137+
{
138+
if ($this->hasStripeId()) {
139+
return $this->updateStripeCustomer($options);
140+
}
141+
142+
return $this->createAsStripeCustomer($options);
143+
}
144+
145+
/**
146+
* Sync the customer's information to Stripe for the current user or create one.
147+
*
148+
* @param array $options
149+
* @return \Stripe\Customer
150+
*/
151+
public function syncOrCreateStripeCustomer(array $options = [])
152+
{
153+
if ($this->hasStripeId()) {
154+
return $this->syncStripeCustomerDetails();
155+
}
156+
157+
return $this->createAsStripeCustomer($options);
158+
}
159+
130160
/**
131161
* Get the Stripe customer for the model.
132162
*

tests/Feature/CustomerTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,24 @@ public function test_customers_in_stripe_can_be_updated()
2424
$this->assertEquals('Mohamed Said', $customer->description);
2525
}
2626

27+
public function test_customers_in_stripe_can_be_created_or_updated()
28+
{
29+
$user = $this->createCustomer('customers_in_stripe_can_be_created_or_updated');
30+
31+
$customer = $user->updateOrCreateStripeCustomer(['description' => 'Hello World']);
32+
33+
// Created
34+
$this->assertEquals('Main Str. 1', $customer->address->line1);
35+
$this->assertEquals('Little Rock', $customer->address->city);
36+
$this->assertEquals('72201', $customer->address->postal_code);
37+
$this->assertEquals('Hello World', $customer->description);
38+
39+
$customer = $user->updateOrCreateStripeCustomer(['description' => 'Random details']);
40+
41+
// Updated
42+
$this->assertEquals('Random details', $customer->description);
43+
}
44+
2745
public function test_customer_details_can_be_synced_with_stripe()
2846
{
2947
$user = $this->createCustomer('customer_details_can_be_synced_with_stripe');
@@ -43,6 +61,33 @@ public function test_customer_details_can_be_synced_with_stripe()
4361
$this->assertEquals('72201', $customer->address->postal_code);
4462
}
4563

64+
public function test_customer_details_can_be_synced_or_created_with_stripe()
65+
{
66+
$user = $this->createCustomer('customer_details_can_be_synced_or_created_with_stripe');
67+
68+
$customer = $user->syncOrCreateStripeCustomer(['description' => 'Hello World']);
69+
70+
// Created
71+
$this->assertEquals('Main Str. 1', $customer->address->line1);
72+
$this->assertEquals('Little Rock', $customer->address->city);
73+
$this->assertEquals('72201', $customer->address->postal_code);
74+
$this->assertEquals('Hello World', $customer->description);
75+
76+
$user->name = 'John Doe';
77+
$user->email = 'john@example.com';
78+
$user->phone = '+32 499 00 00 00';
79+
80+
$customer = $user->syncOrCreateStripeCustomer();
81+
82+
// Synced
83+
$this->assertEquals('John Doe', $customer->name);
84+
$this->assertEquals('john@example.com', $customer->email);
85+
$this->assertEquals('+32 499 00 00 00', $customer->phone);
86+
$this->assertEquals('Main Str. 1', $customer->address->line1);
87+
$this->assertEquals('Little Rock', $customer->address->city);
88+
$this->assertEquals('72201', $customer->address->postal_code);
89+
}
90+
4691
public function test_customers_can_generate_a_billing_portal_url()
4792
{
4893
$user = $this->createCustomer('customers_can_generate_a_billing_portal_url');

0 commit comments

Comments
 (0)