Skip to content

Commit

Permalink
Merge pull request #2 from spira/feature/promo-code
Browse files Browse the repository at this point in the history
Adding preview/promo functionality to subscribe call.
  • Loading branch information
rayzor65 committed Jun 3, 2016
2 parents 41b7a39 + 364327a commit 9b7eb9a
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/DataObjects/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class Subscription extends DataObject
const TERM_TYPE_TERMED = 'TERMED';
const TERM_TYPE_EVERGREEN = 'EVERGREEN';

const RENEWAL_SETTING_EVERGREEN = 'RENEW_TO_EVERGREEN';

public static function getDefaultColumns()
{
return [
Expand Down
22 changes: 20 additions & 2 deletions src/Zuora.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,9 @@ public function subscribe(
ProductRatePlanCharge $ratePlanCharge = null,
PaymentMethod $paymentMethod = null,
Contact $contact = null,
SubscribeOptions $subscribeOptions = null
SubscribeOptions $subscribeOptions = null,
ProductRatePlan $promoPlan = null,
$preview = false
) {
$data = [];

Expand All @@ -294,7 +296,16 @@ public function subscribe(
$contact && $data['BillToContact'] = $contact->toArray();
$subscribeOptions && $data['SubscribeOptions'] = $subscribeOptions->toArray();

$ratePlanData = ['RatePlan' => ['ProductRatePlanId' => $ratePlan['Id']]];
$ratePlanData = [];
array_push($ratePlanData, [
'RatePlan' => ['ProductRatePlanId' => $ratePlan['Id']]
]);
if ($promoPlan) {
array_push($ratePlanData, [
'RatePlan' => ['ProductRatePlanId' => $promoPlan['Id']]
]);
}

if ($ratePlanCharge) {
$ratePlanData['RatePlanChargeData'] = [
['RatePlanCharge' => ['ProductRatePlanChargeId' => $ratePlanCharge['Id']]],
Expand All @@ -306,6 +317,13 @@ public function subscribe(
'RatePlanData' => $ratePlanData,
];

if ($preview) {
$data['PreviewOptions'] = [
'EnablePreviewMode' => true,
'NumberOfPeriods' => 1
];
}

return $this->api->subscribe($data);
}

Expand Down
52 changes: 52 additions & 0 deletions tests/SubscriptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Spira\ZuoraSdk\DataObjects\Account;
use Spira\ZuoraSdk\DataObjects\Subscription;
use Spira\ZuoraSdk\QueryBuilder;

class SubscriptionTest extends TestCase
{
Expand Down Expand Up @@ -61,6 +62,57 @@ public function testSubscribeAndCreateAccount()
$zuora->getApi()->delete('Account', $result->result->AccountId);
}

public function testSubscribePreviewPromotion()
{
$zuora = $this->getZuora();

/** @var \Spira\ZuoraSdk\DataObjects\RatePlan $promoRatePlan */
$promoRatePlan = current($zuora->getAllProductRatePlans([
'Id',
'Name',
'Description',
'ProductId',
'EffectiveEndDate',
'EffectiveStartDate',
'CreatedById',
'CreatedDate',
'UpdatedById',
'UpdatedDate',
'PromoCode__c'
], 1, function (QueryBuilder $query) {
$query->where('PromoCode__c', '=', 'IQSSTAFF');
}));

if (!$promoRatePlan) {
$this->markTestSkipped('If you want to run this test please ensure that a discount rate plan exists in Zuora.');
}

$product = $zuora->getOneProduct($promoRatePlan->ProductId);

$ratePlan = current($zuora->getRatePlansForProduct($product, null, 1));

if (!$ratePlan) {
$this->markTestSkipped('If you want to run this test please ensure that a rate plan exists in Zuora for product ' . $promoRatePlan->ProductId);
}

$account = $this->makeAccount();
$contact = $this->makeContact();
$subscription = $this->makeSubscription(true);

try {
$result = $zuora->subscribe($account, $subscription, $ratePlan, null, null, $contact, null, $promoRatePlan, true);

$this->assertNotEmpty($result->result->InvoiceData);
$this->assertNotEmpty($result->result->InvoiceData->Invoice);
$this->assertNotEmpty($result->result->InvoiceData->InvoiceItem);
$this->assertGreaterThanOrEqual(2, count($result->result->InvoiceData->InvoiceItem));
} catch (\Exception $e) {
print_r($zuora->getApi()->getClient()->__getLastRequest());

throw $e;
}
}

public function testGetAllSubscriptions()
{
$zuora = $this->getZuora();
Expand Down
23 changes: 21 additions & 2 deletions tests/support/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ protected function makeAccount()
{
return new Account(
[
'AccountNumber' => null,
'AutoPay' => false,
'Batch' => 'Batch1',
'Status' => Account::STATUS_DRAFT,
'Currency' => 'AUD',
Expand Down Expand Up @@ -137,11 +139,28 @@ protected function makePaymentMethod()
);
}

protected function makeSubscription()
protected function makeSubscription($termed = false)
{
if ($termed) {
$date = Date('Y-m-d');

return new Subscription(
[
'TermType' => Subscription::TERM_TYPE_TERMED,
'InitialTermPeriodType' => 'Day',
'InitialTerm' => 74,
'AutoRenew' => false,
'ContractEffectiveDate' => $date,
'ServiceActivationDate' => $date,
'TermStartDate' => $date,
'RenewalSetting' => Subscription::RENEWAL_SETTING_EVERGREEN
]
);
}

return new Subscription(
[
'TermType' => Subscription::TERM_TYPE_EVERGREEN,
'TermType' => Subscription::TERM_TYPE_EVERGREEN
]
);
}
Expand Down

0 comments on commit 9b7eb9a

Please sign in to comment.