Skip to content

Commit

Permalink
Merge pull request #459 from recurly/dx-1017-convert-trial
Browse files Browse the repository at this point in the history
Add convertTrial() to Subscription
  • Loading branch information
bhelx committed Feb 18, 2020
2 parents d66600a + cf6f434 commit a9ea3fb
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 1 deletion.
28 changes: 27 additions & 1 deletion Tests/Recurly/Subscription_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,4 +307,30 @@ public function testResumeSubscription() {
$subscription = Recurly_Subscription::get('012345678901234567890123456789ab', $this->client);
$subscription->resume();
}
}

public function testConvertTrialWithTransactionType() {
$this->client->addResponse('GET', '/subscriptions/012345678901234567890123456789ab', 'subscriptions/show-200-trial.xml');
$this->client->addResponse('PUT', 'https://api.recurly.com/v2/subscriptions/012345678901234567890123456789ab/convert_trial', 'subscriptions/convert-trial-200.xml');
$subscription = Recurly_Subscription::get('012345678901234567890123456789ab', $this->client);
$subscription->convertTrialMoto();
$this->assertEquals($subscription->trial_ends_at, $subscription->current_period_started_at);
}

public function testConvertTrialWith3DSToken() {
$this->client->addResponse('GET', '/subscriptions/012345678901234567890123456789ab', 'subscriptions/show-200-trial.xml');
$this->client->addResponse('PUT', 'https://api.recurly.com/v2/subscriptions/012345678901234567890123456789ab/convert_trial', 'subscriptions/convert-trial-200.xml');
$subscription = Recurly_Subscription::get('012345678901234567890123456789ab', $this->client);
$subscription->convertTrial("token");
$this->assertEquals($subscription->trial_ends_at, $subscription->current_period_started_at);
}

public function testConvertTrialWithout3DSToken() {
$this->client->addResponse('GET', '/subscriptions/012345678901234567890123456789ab', 'subscriptions/show-200-trial.xml');
$this->client->addResponse('PUT', 'https://api.recurly.com/v2/subscriptions/012345678901234567890123456789ab/convert_trial', 'subscriptions/convert-trial-200.xml');
$subscription = Recurly_Subscription::get('012345678901234567890123456789ab', $this->client);
$subscription->convertTrial();
$this->assertEquals($subscription->trial_ends_at, $subscription->current_period_started_at);
}


}
61 changes: 61 additions & 0 deletions Tests/fixtures/subscriptions/convert-trial-200.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
HTTP/1.1 200 OK
Content-Type: application/xml; charset=utf-8

<?xml version="1.0" encoding="UTF-8"?>
<subscription href="https://api.recurly.com/v2/subscriptions/012345678901234567890123456789ab">
<account href="https://api.recurly.com/v2/accounts/verena"/>
<redemptions href="https://api.recurly.com/v2/subscriptions/012345678901234567890123456789ab/redemptions"/>
<plan href="https://api.recurly.com/v2/plans/silver">
<plan_code>silver</plan_code>
<name>Silver Plan</name>
</plan>
<uuid>012345678901234567890123456789ab</uuid>
<state>active</state>
<net_terms type="integer">10</net_terms>
<collection_method>manual</collection_method>
<po_number>1000</po_number>
<quantity type="integer">1</quantity>
<total_amount_in_cents type="integer">1000</total_amount_in_cents>
<activated_at type="datetime">2011-04-30T07:00:00Z</activated_at>
<canceled_at nil="nil"></canceled_at>
<expires_at nil="nil"></expires_at>
<current_period_started_at type="datetime">2020-02-10T20:38:21Z</current_period_started_at>
<current_period_ends_at type="datetime">2020-03-10T20:38:21Z</current_period_ends_at>
<trial_started_at type="datetime">2020-02-10T20:38:17Z</trial_started_at>
<trial_ends_at type="datetime">2020-02-10T20:38:21Z</trial_ends_at>
<tax_in_cents type="integer">0</tax_in_cents>
<tax_type>usst</tax_type>
<terms_and_conditions>Some Terms and Conditions</terms_and_conditions>
<customer_notes>Some Customer Notes</customer_notes>
<vat_reverse_charge_notes>Some VAT Notes</vat_reverse_charge_notes>
<started_with_gift type="boolean">false</started_with_gift>
<no_billing_info_reason>plan_free_trial</no_billing_info_reason>
<converted_at nil="nil"></converted_at>
<remaining_pause_cycles type="integer">1</remaining_pause_cycles>>
<paused_at type="datetime">2020-01-01T01:00:00Z</paused_at>
<subscription_add_ons type="array">
<subscription_add_on>
<add_on_type>usage</add_on_type>
<measured_unit href="https://api.recurly.com/v2/measured_units/123456"/>
<usage href="https://api.recurly.com/v2/subscriptions/012345678901234567890123456789ab/add_ons/marketing_emails/usage"/>
<add_on_code>marketing_emails</add_on_code>
<name>Marketing Emails</name>
<unit_amount_in_cents type="integer">5</unit_amount_in_cents>
<quantity type="integer">1</quantity>
<usage_type>price</usage_type>
<usage_percentage nil="nil"/>
</subscription_add_on>
</subscription_add_ons>
<custom_fields type="array">
<custom_field>
<name>shasta</name>
<value>ate my tacos</value>
</custom_field>
<custom_field>
<name>license-number</name>
<value>0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz</value>
</custom_field>
</custom_fields>
<a name="cancel" href="https://api.recurly.com/v2/subscriptions/012345678901234567890123456789ab/cancel" method="put"/>
<a name="terminate" href="https://api.recurly.com/v2/subscriptions/012345678901234567890123456789ab/terminate" method="put"/>
</subscription>
61 changes: 61 additions & 0 deletions Tests/fixtures/subscriptions/show-200-trial.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
HTTP/1.1 200 OK
Content-Type: application/xml; charset=utf-8

<?xml version="1.0" encoding="UTF-8"?>
<subscription href="https://api.recurly.com/v2/subscriptions/012345678901234567890123456789ab">
<account href="https://api.recurly.com/v2/accounts/verena"/>
<redemptions href="https://api.recurly.com/v2/subscriptions/012345678901234567890123456789ab/redemptions"/>
<plan href="https://api.recurly.com/v2/plans/silver">
<plan_code>silver</plan_code>
<name>Silver Plan</name>
</plan>
<uuid>012345678901234567890123456789ab</uuid>
<state>active</state>
<net_terms type="integer">10</net_terms>
<collection_method>manual</collection_method>
<po_number>1000</po_number>
<quantity type="integer">1</quantity>
<total_amount_in_cents type="integer">1000</total_amount_in_cents>
<activated_at type="datetime">2011-04-30T07:00:00Z</activated_at>
<canceled_at nil="nil"></canceled_at>
<expires_at nil="nil"></expires_at>
<current_period_started_at type="datetime">2020-02-10T20:38:21Z</current_period_started_at>
<current_period_ends_at type="datetime">2020-03-10T20:38:21Z</current_period_ends_at>
<trial_started_at type="datetime">2020-02-10T20:38:17Z</trial_started_at>
<trial_ends_at type="datetime">2020-03-10T20:38:21Z</trial_ends_at>
<tax_in_cents type="integer">0</tax_in_cents>
<tax_type>usst</tax_type>
<terms_and_conditions>Some Terms and Conditions</terms_and_conditions>
<customer_notes>Some Customer Notes</customer_notes>
<vat_reverse_charge_notes>Some VAT Notes</vat_reverse_charge_notes>
<started_with_gift type="boolean">false</started_with_gift>
<no_billing_info_reason>plan_free_trial</no_billing_info_reason>
<converted_at nil="nil"></converted_at>
<remaining_pause_cycles type="integer">1</remaining_pause_cycles>>
<paused_at type="datetime">2020-01-01T01:00:00Z</paused_at>
<subscription_add_ons type="array">
<subscription_add_on>
<add_on_type>usage</add_on_type>
<measured_unit href="https://api.recurly.com/v2/measured_units/123456"/>
<usage href="https://api.recurly.com/v2/subscriptions/012345678901234567890123456789ab/add_ons/marketing_emails/usage"/>
<add_on_code>marketing_emails</add_on_code>
<name>Marketing Emails</name>
<unit_amount_in_cents type="integer">5</unit_amount_in_cents>
<quantity type="integer">1</quantity>
<usage_type>price</usage_type>
<usage_percentage nil="nil"/>
</subscription_add_on>
</subscription_add_ons>
<custom_fields type="array">
<custom_field>
<name>shasta</name>
<value>ate my tacos</value>
</custom_field>
<custom_field>
<name>license-number</name>
<value>0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz</value>
</custom_field>
</custom_fields>
<a name="cancel" href="https://api.recurly.com/v2/subscriptions/012345678901234567890123456789ab/cancel" method="put"/>
<a name="terminate" href="https://api.recurly.com/v2/subscriptions/012345678901234567890123456789ab/terminate" method="put"/>
</subscription>
25 changes: 25 additions & 0 deletions lib/recurly/subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,31 @@ public function resume() {
$this->_save(Recurly_Client::PUT, $this->uri() . '/resume');
}

/**
* Converts trial to paid subscription when transaction_type == "moto"
*/
public function convertTrialMoto() {
$doc = $this->createDocument();
$root = $doc->appendChild($doc->createElement($this->getNodeName()));
$root->appendChild($doc->createElement('transaction_type', "moto"));
$this->_save(Recurly_Client::PUT, $this->uri() . '/convert_trial', $this->renderXML($doc));
}

/**
* Converts trial to paid subscription.
*/
public function convertTrial($three_d_secure_action_result_token_id = null) {
if ($three_d_secure_action_result_token_id != null) {
$doc = $this->createDocument();
$root = $doc->appendChild($doc->createElement($this->getNodeName()));
$account = $root->appendChild($doc->createElement("account"));
$billingInfo = $account->appendChild($doc->createElement("billing_info"));
$billingInfo->appendChild($doc->createElement("three_d_secure_action_result_token_id", $three_d_secure_action_result_token_id));
$this->_save(Recurly_Client::PUT, $this->uri() . '/convert_trial', $this->renderXML($doc));
}
$this->_save(Recurly_Client::PUT, $this->uri() . '/convert_trial');
}

public function buildUsage($addOnCode, $client = null) {
return Recurly_Usage::build($this->uuid, $addOnCode, $client);
}
Expand Down

0 comments on commit a9ea3fb

Please sign in to comment.