Skip to content

Commit

Permalink
create stripe customer or if exists then update/sync
Browse files Browse the repository at this point in the history
  • Loading branch information
arafatkn committed Feb 29, 2024
1 parent 06f01fa commit 675f451
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
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 createOrUpdateStripeCustomer(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 createOrSyncStripeCustomer(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->createOrUpdateStripeCustomer(['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->createOrUpdateStripeCustomer(['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->createOrSyncStripeCustomer(['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->createOrSyncStripeCustomer();

// 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

0 comments on commit 675f451

Please sign in to comment.