Skip to content

Commit

Permalink
Merge pull request #189 from sander3/main
Browse files Browse the repository at this point in the history
Custom mollie_customer_id and mollie_mandate_id column names
  • Loading branch information
Naoray authored Aug 3, 2023
2 parents b4182c7 + 6967dad commit 236e4c1
Showing 1 changed file with 33 additions and 13 deletions.
46 changes: 33 additions & 13 deletions src/Billable.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function subscription($subscription = 'default')
*/
public function newSubscription($subscription, $plan, $firstPaymentOptions = [])
{
if (! empty($this->mollie_mandate_id)) {
if (! empty($this->mollieMandateId())) {
$mandate = $this->mollieMandate();
$planModel = app(PlanRepository::class)::findOrFail($plan);
$allowedPlanMethods = collect($planModel->firstPaymentMethod())->map(function ($allowedPlanMethod) {
Expand All @@ -84,7 +84,7 @@ public function newSubscription($subscription, $plan, $firstPaymentOptions = [])
&& $mandate->isValid()
&& $allowedPlanMethods->contains($mandate->method)
) {
return $this->newSubscriptionForMandateId($this->mollie_mandate_id, $subscription, $plan);
return $this->newSubscriptionForMandateId($this->mollieMandateId(), $subscription, $plan);
}
}

Expand Down Expand Up @@ -121,8 +121,8 @@ public function newSubscriptionViaMollieCheckout($subscription, $plan, $firstPay
public function newSubscriptionForMandateId($mandateId, $subscription, $plan)
{
// The mandateId has changed
if ($this->mollie_mandate_id !== $mandateId) {
$this->mollie_mandate_id = $mandateId;
if ($this->mollieMandateId() !== $mandateId) {
$this->{$this->getMollieMandateIdColumn()} = $mandateId;
$this->guardMollieMandate();
$this->save();
}
Expand All @@ -131,17 +131,27 @@ public function newSubscriptionForMandateId($mandateId, $subscription, $plan)
}

/**
* Retrieve the Mollie customer ID for this model
* Get the name of the "mollie_customer_id" column.
*
* @return string
*/
public function getMollieCustomerIdColumn()
{
return 'mollie_customer_id';
}

/**
* Retrieve the Mollie customer ID for the billable model.
*
* @return string
*/
public function mollieCustomerId()
{
if (empty($this->mollie_customer_id)) {
if (empty($this->{$this->getMollieCustomerIdColumn()})) {
return $this->createAsMollieCustomer()->id;
}

return $this->mollie_customer_id;
return $this->{$this->getMollieCustomerIdColumn()};
}

/**
Expand All @@ -158,7 +168,7 @@ public function createAsMollieCustomer(array $override_options = [])
$createMollieCustomer = app()->make(CreateMollieCustomer::class);
$customer = $createMollieCustomer->execute($options);

$this->mollie_customer_id = $customer->id;
$this->{$this->getMollieCustomerIdColumn()} = $customer->id;
$this->save();

return $customer;
Expand All @@ -171,14 +181,14 @@ public function createAsMollieCustomer(array $override_options = [])
*/
public function asMollieCustomer()
{
if (empty($this->mollie_customer_id)) {
if (empty($this->{$this->getMollieCustomerIdColumn()})) {
return $this->createAsMollieCustomer();
}

/** @var GetMollieCustomer $getMollieCustomer */
$getMollieCustomer = app()->make(GetMollieCustomer::class);

return $getMollieCustomer->execute($this->mollie_customer_id);
return $getMollieCustomer->execute($this->{$this->getMollieCustomerIdColumn()});
}

/**
Expand Down Expand Up @@ -409,12 +419,22 @@ public function downloadInvoice($orderId, $data = [], $view = Invoice::DEFAULT_V
return $order->invoice()->download($data, $view, $options);
}

/**
* Get the name of the "mollie_mandate_id" column.
*
* @return string
*/
public function getMollieMandateIdColumn()
{
return 'mollie_mandate_id';
}

/**
* @return null|string
*/
public function mollieMandateId()
{
return $this->mollie_mandate_id;
return $this->{$this->getMollieMandateIdColumn()};
}

/**
Expand Down Expand Up @@ -494,7 +514,7 @@ public function validateMollieMandate()
*/
public function guardMollieMandate()
{
throw_unless($this->validateMollieMandate(), new InvalidMandateException);
throw_unless($this->validateMollieMandate(), new InvalidMandateException());

return true;
}
Expand All @@ -510,7 +530,7 @@ public function clearMollieMandate()

$previousId = $this->mollieMandateId();

$this->fill(['mollie_mandate_id' => null]);
$this->fill([$this->getMollieMandateIdColumn() => null]);
$this->save();

event(new MandateClearedFromBillable($this, $previousId));
Expand Down

0 comments on commit 236e4c1

Please sign in to comment.