$migrBill = new AutoBill();
// The subscription was originally started,
// in the existing system, on Jan. 25, 2014.
$migrBill->setStartTimestamp('2014-01-25');
// Set unique subscription ID.
$migrBill->setMerchantAutoBillId('SampleMigratedAutoBill-ABC123');
// Specify the Billing Plan the migrated AutoBill will be on.
// Make sure the Billing Plan used below is created in Subscribe in advance.
// This is a monthly Billing Plan with first cycle marked FREE.
$bp = new BillingPlan();
$bp->setMerchantBillingPlanId('first-free-monthly');
$migrBill->setBillingPlan($bp);
// Specify the Product the migrated AutoBill will be using.
// Make sure the Product used below is created in Subscribe in advance.
// First, fetch the Product to get more info about the Product,
// which can be used to fill into the migration transactions.
$product_factory = new Product();
$vr = $product_factory->fetchByMerchantProductId('monthlySub');
// assuming here that the return is 200
$prod = $vr->getProduct(); // The product that the fetch returned.
$item = new AutoBillItem();
$item->setProduct($prod);
$migrBill->items[0] = $item;
// For actual migration the Account used here with its associated
// PaymentMethod is expected to preexist in Subscribe.
$account_factory = new Account();
$vrc = $account_factory->fetchByMerchantAccountId('user_xyz123');
$acct = $vrc->account(); // Assuming that the return is 200.
$migrBill->setAccount($acct);
// *************************************************
// Construct the "Free" Transaction for the first
// billing cycle to migrate.
$mt0 = new MigrationTransaction();
$mt0->setMerchantTransactionId('MIGR-SAMPL-0-987654');
// Each transaction should have a unique ID.
$mt0->setAutoBillCycle(0);
$mt0->setType('Recurring');
$mt0->setBillingPlanCycle(0);
$mt0->setMerchantBillingPlanId($bp->getMerchantBillingPlanId);
$mt0->setRetryNumber(0);
$pm_array = $acct->getPaymentMethods();
$target_pm = $pm_array[0];
$mt0->setPaymentMethod($target_pm);
$mt0->setAccount($acct);
$mt0->setSalesTaxAddress($target_pm->getBillingAddress());
$mt0->setBillingDate('2014-01-25');
$mt0->setCurrency('USD');
$mt0->setAmount(0.0);
// This amount must be 0 to make this transaction FREE.
$mTxItem00 = new MigrationTransactionItem();
$mTxItem00->setItemType('RecurringCharge');
$mTxItem00->setSku('FREE');
$mTxItem00->setName('Free Cycle');
$mTxItem00->setPrice(0.0);
$mTxItem00->setServicePeriodStartDate('2014-01-25');
$mTxItem00->setServicePeriodEndDate('2014-02-24');
$mt0->setMigrationTransactionItems([ $mTxItem00 ]);
// Report this transaction in "captured" status.
$status00 = new TransactionStatus();
$status00->setStatus('Captured');
$status00->setPaymentMethodType('CreditCard');
$status00.setTimestamp('2014-01-25 01:30:40');
$mt0->setStatusLog( [ $status00 ] );
// Now construct the array of Transactions to be migrated.
$migrTxs = [ $mt0 ];
// The next billing for the AutoBill should happen a month
// from the last billing (2nd transaction in the migrated
// transaction array above) i.e. on Dec. 27, 2013.
// Now, make the Subscribe SOAP API call to migrate the AutoBill
// along with the transactions constructed above.
$vr = $migrBill->migrate('2014-01-25', $migrTxs);
if ($vr->getReturnCode() != 200) {
print('Migrate call failed - return code '
. $vr->getReturnCode()
. ' and return string '
. $vr->getReturnString());
print('Soap id ' . $vr->getSoapId());
}
else {
print('Migration successful. Created autobill id '
. $migrBill->getMerchantAutoBillId()
. ' in Subscribe. Its VID: '
. $migrBill->getVID()
. ' . This autobill is for customer Account ID '
. $migrBill->getAccount()->getMerchantAccountId());
print('Next billing will be on '
. $migrBill->getNextBilling()
. ' for $'
. $migrBill->getNextBilling()->getAmount());
$transaction_factory = new Transaction();
$tx_array = $transaction_factory->fetchByAccount($acct, false);
// Because we migrated only the $0 transaction,
// there should be only one transaction.
$the_tx = $tx_array[0];
$status_log = $the_tx->getStatusLog();
$the_status = $status_log[0];
print('Last transaction for this account was for $'
. $the_tx->getAmount()
. ' processed on '
. $the_status->getTimestamp);
}
Migrating an AutoBill During a Free Trial Period
This example demonstrates how to migrate an AutoBill that is currently within a one-month free trial period. Once migrated, Subscribe will begin billing when this trial period has completed.
Parent topic:Importing AutoBills from other Billing Systems to Subscribe

