Skip to content

Instantly share code, notes, and snippets.

@nishapanFFF
Last active November 1, 2019 17:04
Show Gist options
  • Save nishapanFFF/540abfa2ed58f18a724858465f5acfc0 to your computer and use it in GitHub Desktop.
Save nishapanFFF/540abfa2ed58f18a724858465f5acfc0 to your computer and use it in GitHub Desktop.
$purchase = new Recurly_Purchase();
$purchase->currency = 'USD';
$purchase->account = new Recurly_Account();
$purchase->account->account_code = 'user1@hotmail.com';
$purchase->account->first_name = 'jane';
$purchase->account->last_name = 'doe';
$billing_info = new Recurly_BillingInfo();
$billing_info->number = '4111-1111-1111-1111';
$billing_info->country = 'US';
$billing_info->state = 'NY';
$billing_info->zip = '10009';
$billing_info->address1 = '630 E 14th St';
$billing_info->city = 'New York';
$purchase->account->billing_info = $billing_info;
$adjustment = new Recurly_Adjustment();
$adjustment->account_code = 'user1@hotmail.com';
$adjustment->quantity = 1;
$adjustment->accounting_code = 'friend-gift';
$adjustment->tax_exempt = false;
$adjustment->unit_amount_in_cents = 500;
$purchase->adjustments = array($adjustment);
$subscription = new Recurly_Subscription();
$subscription->plan_code = 'ffftrial';
$purchase->subscriptions = array($subscription);
$previewCollection = Recurly_Purchase::preview($purchase);
if (isset($previewCollection)) {
// We expect previewCollection to be an invoice with two line items:
// one for the Subscription (using planCode) another for the Adjustment (using accountingCode).
// We find the Ajustment line_item by matching its accountingCode to the input.
// Then we determine the Adjustment rate by checking if tax_in_cents is non-zero.
foreach ($previewCollection->line_items as $line_item) {
if ($line_item->accounting_code === 'friend-gift') {
if ($line_item->tax_in_cents > 0) {
$result['tax_rate'] = $line_item->tax_rate;
} else {
$result['tax_rate'] = 0;
}
break;
}
}
}
return $result;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment