Skip to content

Commit

Permalink
Add installments. (fixes #602)
Browse files Browse the repository at this point in the history
  • Loading branch information
srmklive committed Dec 20, 2023
1 parent 7829379 commit 6bde2b2
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 14 deletions.
35 changes: 21 additions & 14 deletions src/Traits/PayPalAPI/Subscriptions/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,13 @@ public function setupSubscription(string $customer_name, string $customer_email,
* @param string $interval_type
* @param int $interval_count
* @param float|int $price
* @param int $total_cycles
*
* @return \Srmklive\PayPal\Services\PayPal
*/
public function addPlanTrialPricing(string $interval_type, int $interval_count, float $price = 0): \Srmklive\PayPal\Services\PayPal
public function addPlanTrialPricing(string $interval_type, int $interval_count, float $price = 0, int $total_cycles = 1): \Srmklive\PayPal\Services\PayPal
{
$this->trial_pricing = $this->addPlanBillingCycle($interval_type, $interval_count, $price, true);
$this->trial_pricing = $this->addPlanBillingCycle($interval_type, $interval_count, $price, $total_cycles, true);

return $this;
}
Expand All @@ -126,18 +127,19 @@ public function addPlanTrialPricing(string $interval_type, int $interval_count,
* @param string $name
* @param string $description
* @param float|int $price
* @param int $total_cycles
*
* @throws Throwable
*
* @return \Srmklive\PayPal\Services\PayPal
*/
public function addDailyPlan(string $name, string $description, float $price): \Srmklive\PayPal\Services\PayPal
public function addDailyPlan(string $name, string $description, float $price, int $total_cycles = 0): \Srmklive\PayPal\Services\PayPal
{
if (isset($this->billing_plan)) {
return $this;
}

$plan_pricing = $this->addPlanBillingCycle('DAY', 1, $price);
$plan_pricing = $this->addPlanBillingCycle('DAY', 1, $price, $total_cycles);
$billing_cycles = empty($this->trial_pricing) ? [$plan_pricing] : collect([$this->trial_pricing, $plan_pricing])->filter()->toArray();

$this->addBillingPlan($name, $description, $billing_cycles);
Expand All @@ -151,18 +153,19 @@ public function addDailyPlan(string $name, string $description, float $price): \
* @param string $name
* @param string $description
* @param float|int $price
* @param int $total_cycles
*
* @throws Throwable
*
* @return \Srmklive\PayPal\Services\PayPal
*/
public function addWeeklyPlan(string $name, string $description, float $price): \Srmklive\PayPal\Services\PayPal
public function addWeeklyPlan(string $name, string $description, float $price, int $total_cycles = 0): \Srmklive\PayPal\Services\PayPal
{
if (isset($this->billing_plan)) {
return $this;
}

$plan_pricing = $this->addPlanBillingCycle('WEEK', 1, $price);
$plan_pricing = $this->addPlanBillingCycle('WEEK', 1, $price, $total_cycles);
$billing_cycles = empty($this->trial_pricing) ? [$plan_pricing] : collect([$this->trial_pricing, $plan_pricing])->filter()->toArray();

$this->addBillingPlan($name, $description, $billing_cycles);
Expand All @@ -176,18 +179,19 @@ public function addWeeklyPlan(string $name, string $description, float $price):
* @param string $name
* @param string $description
* @param float|int $price
* @param int $total_cycles
*
* @throws Throwable
*
* @return \Srmklive\PayPal\Services\PayPal
*/
public function addMonthlyPlan(string $name, string $description, float $price): \Srmklive\PayPal\Services\PayPal
public function addMonthlyPlan(string $name, string $description, float $price, int $total_cycles = 0): \Srmklive\PayPal\Services\PayPal
{
if (isset($this->billing_plan)) {
return $this;
}

$plan_pricing = $this->addPlanBillingCycle('MONTH', 1, $price);
$plan_pricing = $this->addPlanBillingCycle('MONTH', 1, $price, $total_cycles);
$billing_cycles = empty($this->trial_pricing) ? [$plan_pricing] : collect([$this->trial_pricing, $plan_pricing])->filter()->toArray();

$this->addBillingPlan($name, $description, $billing_cycles);
Expand All @@ -201,18 +205,19 @@ public function addMonthlyPlan(string $name, string $description, float $price):
* @param string $name
* @param string $description
* @param float|int $price
* @param int $total_cycles
*
* @throws Throwable
*
* @return \Srmklive\PayPal\Services\PayPal
*/
public function addAnnualPlan(string $name, string $description, float $price): \Srmklive\PayPal\Services\PayPal
public function addAnnualPlan(string $name, string $description, float $price, int $total_cycles = 0): \Srmklive\PayPal\Services\PayPal
{
if (isset($this->billing_plan)) {
return $this;
}

$plan_pricing = $this->addPlanBillingCycle('YEAR', 1, $price);
$plan_pricing = $this->addPlanBillingCycle('YEAR', 1, $price, $total_cycles);
$billing_cycles = empty($this->trial_pricing) ? [$plan_pricing] : collect([$this->trial_pricing, $plan_pricing])->filter()->toArray();

$this->addBillingPlan($name, $description, $billing_cycles);
Expand All @@ -228,12 +233,13 @@ public function addAnnualPlan(string $name, string $description, float $price):
* @param float|int $price
* @param string $interval_unit
* @param int $interval_count
* @param int $total_cycles
*
* @throws Throwable
*
* @return \Srmklive\PayPal\Services\PayPal
*/
public function addCustomPlan(string $name, string $description, float $price, string $interval_unit, int $interval_count): \Srmklive\PayPal\Services\PayPal
public function addCustomPlan(string $name, string $description, float $price, string $interval_unit, int $interval_count, int $total_cycles = 0): \Srmklive\PayPal\Services\PayPal
{
$billing_intervals = ['DAY', 'WEEK', 'MONTH', 'YEAR'];

Expand All @@ -245,7 +251,7 @@ public function addCustomPlan(string $name, string $description, float $price, s
throw new \RuntimeException('Billing intervals should either be '.implode(', ', $billing_intervals));
}

$plan_pricing = $this->addPlanBillingCycle($interval_unit, $interval_count, $price);
$plan_pricing = $this->addPlanBillingCycle($interval_unit, $interval_count, $price, $total_cycles);
$billing_cycles = empty($this->trial_pricing) ? [$plan_pricing] : collect([$this->trial_pricing, $plan_pricing])->filter()->toArray();

$this->addBillingPlan($name, $description, $billing_cycles);
Expand All @@ -259,11 +265,12 @@ public function addCustomPlan(string $name, string $description, float $price, s
* @param string $interval_unit
* @param int $interval_count
* @param float $price
* @param int $total_cycles
* @param bool $trial
*
* @return array
*/
protected function addPlanBillingCycle(string $interval_unit, int $interval_count, float $price, bool $trial = false): array
protected function addPlanBillingCycle(string $interval_unit, int $interval_count, float $price, int $total_cycles, bool $trial = false): array
{
$pricing_scheme = [
'fixed_price' => [
Expand All @@ -285,7 +292,7 @@ protected function addPlanBillingCycle(string $interval_unit, int $interval_coun
],
'tenure_type' => ($trial === true) ? 'TRIAL' : 'REGULAR',
'sequence' => ($trial === true) ? 1 : $plan_sequence,
'total_cycles' => ($trial === true) ? 1 : 0,
'total_cycles' => $total_cycles,
'pricing_scheme' => $pricing_scheme,
];
}
Expand Down
41 changes: 41 additions & 0 deletions tests/Feature/AdapterCreateSubscriptionHelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -569,4 +569,45 @@ public function it_can_set_tax_percentage_when_creating_subscription()
$this->assertArrayHasKey('id', $response);
$this->assertArrayHasKey('plan_id', $response);
}

/** @test */
public function it_can_create_a_subscription_with_fixed_installments()
{
$this->client->setAccessToken([
'access_token' => self::$access_token,
'token_type' => 'Bearer',
]);

$this->client->setClient(
$this->mock_http_client(
$this->mockCreateCatalogProductsResponse()
)
);

$start_date = Carbon::now()->addDay()->toDateString();

$this->client = $this->client->addProduct('Demo Product', 'Demo Product', 'SERVICE', 'SOFTWARE');

$this->client->setClient(
$this->mock_http_client(
$this->mockCreatePlansResponse()
)
);

$this->client = $this->client->addPlanTrialPricing('DAY', 7)
->addMonthlyPlan('Demo Plan', 'Demo Plan', 100, 12);

$this->client->setClient(
$this->mock_http_client(
$this->mockCreateSubscriptionResponse()
)
);

$response = $this->client->setReturnAndCancelUrl('https://example.com/paypal-success', 'https://example.com/paypal-cancel')
->setupSubscription('John Doe', '[email protected]', $start_date);

$this->assertNotEmpty($response);
$this->assertArrayHasKey('id', $response);
$this->assertArrayHasKey('plan_id', $response);
}
}

0 comments on commit 6bde2b2

Please sign in to comment.