Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[15.x] Create stripe customer if not exists or update/sync #1661

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/Concerns/ManagesCustomer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
45 changes: 45 additions & 0 deletions tests/Feature/CustomerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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');
Expand Down