From 6bde2b232a38c413f730115743cc9dcc29d4228e Mon Sep 17 00:00:00 2001 From: Raza Mehdi Date: Wed, 20 Dec 2023 23:51:31 +0500 Subject: [PATCH] Add installments. (fixes #602) --- .../PayPalAPI/Subscriptions/Helpers.php | 35 +++++++++------- .../AdapterCreateSubscriptionHelpersTest.php | 41 +++++++++++++++++++ 2 files changed, 62 insertions(+), 14 deletions(-) diff --git a/src/Traits/PayPalAPI/Subscriptions/Helpers.php b/src/Traits/PayPalAPI/Subscriptions/Helpers.php index 6fcf936d..8b65faa2 100644 --- a/src/Traits/PayPalAPI/Subscriptions/Helpers.php +++ b/src/Traits/PayPalAPI/Subscriptions/Helpers.php @@ -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; } @@ -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); @@ -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); @@ -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); @@ -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); @@ -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']; @@ -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); @@ -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' => [ @@ -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, ]; } diff --git a/tests/Feature/AdapterCreateSubscriptionHelpersTest.php b/tests/Feature/AdapterCreateSubscriptionHelpersTest.php index e327842a..bbe51211 100644 --- a/tests/Feature/AdapterCreateSubscriptionHelpersTest.php +++ b/tests/Feature/AdapterCreateSubscriptionHelpersTest.php @@ -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', 'john@example.com', $start_date); + + $this->assertNotEmpty($response); + $this->assertArrayHasKey('id', $response); + $this->assertArrayHasKey('plan_id', $response); + } }