diff --git a/src/Concerns/ManagesCustomer.php b/src/Concerns/ManagesCustomer.php index c1122439..66894d35 100644 --- a/src/Concerns/ManagesCustomer.php +++ b/src/Concerns/ManagesCustomer.php @@ -127,6 +127,36 @@ public function createOrGetStripeCustomer(array $options = []) return $this->createAsStripeCustomer($options); } + /** + * Update the Stripe customer information for the current user or create one. + * + * @param array $options + * @return \Stripe\Customer + */ + public function updateOrCreateStripeCustomer(array $options = []) + { + if ($this->hasStripeId()) { + return $this->updateStripeCustomer($options); + } + + return $this->createAsStripeCustomer($options); + } + + /** + * Sync the customer's information to Stripe for the current user or create one. + * + * @param array $options + * @return \Stripe\Customer + */ + public function syncOrCreateStripeCustomer(array $options = []) + { + if ($this->hasStripeId()) { + return $this->syncStripeCustomerDetails(); + } + + return $this->createAsStripeCustomer($options); + } + /** * Get the Stripe customer for the model. * diff --git a/tests/Feature/CustomerTest.php b/tests/Feature/CustomerTest.php index 6ab64365..99759a85 100644 --- a/tests/Feature/CustomerTest.php +++ b/tests/Feature/CustomerTest.php @@ -24,6 +24,24 @@ public function test_customers_in_stripe_can_be_updated() $this->assertEquals('Mohamed Said', $customer->description); } + public function test_customers_in_stripe_can_be_created_or_updated() + { + $user = $this->createCustomer('customers_in_stripe_can_be_created_or_updated'); + + $customer = $user->updateOrCreateStripeCustomer(['description' => 'Hello World']); + + // Created + $this->assertEquals('Main Str. 1', $customer->address->line1); + $this->assertEquals('Little Rock', $customer->address->city); + $this->assertEquals('72201', $customer->address->postal_code); + $this->assertEquals('Hello World', $customer->description); + + $customer = $user->updateOrCreateStripeCustomer(['description' => 'Random details']); + + // Updated + $this->assertEquals('Random details', $customer->description); + } + public function test_customer_details_can_be_synced_with_stripe() { $user = $this->createCustomer('customer_details_can_be_synced_with_stripe'); @@ -43,6 +61,33 @@ public function test_customer_details_can_be_synced_with_stripe() $this->assertEquals('72201', $customer->address->postal_code); } + public function test_customer_details_can_be_synced_or_created_with_stripe() + { + $user = $this->createCustomer('customer_details_can_be_synced_or_created_with_stripe'); + + $customer = $user->syncOrCreateStripeCustomer(['description' => 'Hello World']); + + // Created + $this->assertEquals('Main Str. 1', $customer->address->line1); + $this->assertEquals('Little Rock', $customer->address->city); + $this->assertEquals('72201', $customer->address->postal_code); + $this->assertEquals('Hello World', $customer->description); + + $user->name = 'John Doe'; + $user->email = 'john@example.com'; + $user->phone = '+32 499 00 00 00'; + + $customer = $user->syncOrCreateStripeCustomer(); + + // Synced + $this->assertEquals('John Doe', $customer->name); + $this->assertEquals('john@example.com', $customer->email); + $this->assertEquals('+32 499 00 00 00', $customer->phone); + $this->assertEquals('Main Str. 1', $customer->address->line1); + $this->assertEquals('Little Rock', $customer->address->city); + $this->assertEquals('72201', $customer->address->postal_code); + } + public function test_customers_can_generate_a_billing_portal_url() { $user = $this->createCustomer('customers_can_generate_a_billing_portal_url');