> ## Documentation Index
> Fetch the complete documentation index at: https://docs.slm.m1amdocs.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Migrating an AutoBill During a Billing Cycle

This example demonstrates how to migrate an AutoBill that has completed two monthly billing cycles on a pre-existing billing system. The AutoBill is migrated to Subscribe before its third billing date.

```
 
 // Construct the AutoBill to be migrated 
 
 $migrBill = new AutoBill();
 
 // The subscription was originally started on the 
 // existing system on Dec. 27, 2013.
 $migrBill->setStartTimestamp('2013-12-27');
 
 // Set a unique subscription ID. If the subscription 
 // existing in your current system has an ID.
 migrBill.setMerchantAutoBillId('SampleMigratedAutoBill-950681');
 
 // Specify the Billing Plan the migrated AutoBill will be on.
 // Make sure the Billing Plan used below preexists in Subscribe.
 $billPlanId = 'migrMonthly';
 $bp = new BillingPlan();
 $bp->setMerchantBillingPlanId($billPlanId);
 $migrBill->setBillingPlan($bp);
 
 // Specify the Product the migrated AutoBill will be using.
 // Make sure the Product used below is created in Subscribe in advance
 
 // Now, fetch the Product to get more info about the Product. 
 // That info can be used to fill in the migration transactions. 
 // Exception handling code for the fetch call is omitted for brevity.
 // This step is optional. The necessary product 
 // information can also be retrieved from your local store.
 
 $product_factory = new Product();
 $vrc = $product_factory->fetchByMerchantProductId('monthlySub');
 $prod = $vrc->product(); // assuming here that the return is 200
 
 $item = new AutoBillItem();
 $item->setProduct($prod);
 // The item was added the same day the AutoBill started, 
 // so you must specify the start date explicitly.
 
 $migrBill->setItems( [ $item ] );
 
 // This sample creates a new customer Account 
 // and a new PaymentMethod for every run.
 // For actual migration, the Account used here, and its associated 
 // PaymentMethod, is expected to be already present in Subscribe.
 $account_factory = new Account();
 $vrc = $account_factory->fetchByMerchantAccountId('user_207408');
 $acct = $vrc->account(); // assuming that the return is 200
 
 $migrBill->setAccount($acct);
 
 $paymentProcessor = 'Litle'; 
    // Your merchant account must already have an active 
    // routing set to process transactions at Litle.
 $paymentProcessorMerchantId = '9104658';

 // ***************************************************
 // Construct the Transaction for the first Billing Cycle to migrate.
 
 $mt0 = new MigrationTransaction();
 $mt0->setMerchantTransactionId('MIGR-SAMPL-0-196114'); // each transaction should have unique ID
 $mt0->setAutoBillCycle(0);
 $mt0->setType('Recurring');
 $mt0->setBillingPlanCycle(0);
 $mt0->setMerchantBillingPlanId(billPlanId);
 $mt0->setRetryNumber(0);
 $mt0->setPaymentMethod(acct.getPaymentMethods()[0]);
 $mt0->setAccount(acct);
 $mt0->setSalesTaxAddress(acct.getPaymentMethods()[0].getBillingAddress());
 $mt0->setPaymentProcessor(paymentProcessor);
 $mt0->setPaymentProcessorTransactionId('Litle-' . curTime);
 $mt0->setDivisionNumber(paymentProcessorMerchantId);
 
 $mt0->setBillingDate('2013-12-27');
 
 $mt0->setCurrency('USD');
 
 $txTotal = 0.0; // This must match the total of the items, 
             // so we will add to this total as we construct line items.
 
 $mTxItem00 = new MigrationTransactionItem();
 $mTxItem00->
    setItemType(com.vindicia.soap.v5_0.Vindicia.MigrationTransactionItemType
    .RecurringCharge);
 $mTxItem00->setSku(prod.getMerchantProductId());
 $mTxItem00->setName(prod.getDescriptions()[0].getDescription() 
    . ' - includes discounts (if any)');
 $discount = 0.15 ; // Customer got 15% discount for this transaction.
 $price_array = $prod->getPrices();
 $the_price = $price_array[0];
 $productPrice = $prod->getAmount();
 $discountedProductPrice = $productPrice - $productPrice * $discount;
 $mTxItem00->setPrice($discountedProductPrice);
 $txTotal = $txTotal + $discountedProductPrice;
 $mTxItem00->setTaxClassification('DC010500'); 
    // This should be the Avalara tax code associated with this product
 
 $mTxItem00->setServicePeriodStartDate('2013-12-27');
 $mTxItem00->setServicePeriodEndDate('2014-01-26');
 
 $mTxTaxItem000 = new MigrationTaxItem();
 $mTxTaxItem000->setAmount(7.50);
 $mTxTaxItem000->setJurisdiction('STATE_06');
 $mTxTaxItem000->setName('CALIFORNIA STATE SALES TAX');
 
 // Let's add the tax to our transaction total.
 $txTotal = $txTotal + 7.50;
 
 $mTxTaxItem001 = new MigrationTaxItem();
 $mTxTaxItem001->setAmount(1.00);
 $mTxTaxItem001->setJurisdiction('COUNTY_085');
 $mTxTaxItem001->setName('SANTA CLARA COUNTY SALES TAX');
 
 // Let's add the tax to our transaction total.
 $txTotal = $txTotal + 1.00;
 
 $mTxItem00->setMigrationTaxItems( [ $mTxTaxItem000, $mTxTaxItem001 ] );
 
 $mt0->setMigrationTransactionItems( [ $mTxItem00 ] );
 
 // Let's set the transaction total.
 $mt0->setAmount($txTotal); // Total tx amt should be same as. 
 
 // Report this transaction in 'captured' status so it can be refunded.
 $status00 = new TransactionStatus();
 $status00->setStatus('Captured');
 $status00->setPaymentMethodType('CreditCard');
 $status00->setTimestamp('2013-12-27 01:30:40');
 $ccStatus00 = new TransactionStatusCreditCard();
 $ccStatus00->setAuthCode('000');
 $status00->setCreditCardStatus($ccStatus00);
 
 $mt0->setStatusLog( [ $status00 ] );
 
 // **************************************************
 
 // Construct the Transaction for the second billing cycle to migrate.
 
 $mt1 = new MigrationTransaction();
 $mt1->setMerchantTransactionId('MIGR-SAMPL-1-' . curTime);
 $mt1->setAutoBillCycle(1);
 $mt1->
    setType(com.vindicia.soap.v5_0.Vindicia.MigrationTransactionType
    .Recurring);
 $mt1->setBillingPlanCycle(1);
 $mt1->setMerchantBillingPlanId(billPlanId);
 $mt1->setRetryNumber(0);
 $mt1->setPaymentMethod(acct.getPaymentMethods()[0]);
 $mt1->setAccount(acct);
 $mt1->setSalesTaxAddress(acct.getPaymentMethods()[0].getBillingAddress());
 $mt1->setPaymentProcessor(paymentProcessor);
 $mt1->setPaymentProcessorTransactionId('Litle-' . curTime);
 $mt1->setDivisionNumber(paymentProcessorMerchantId);
 $mt1->setCurrency('USD');
 $mt1->setBillingDate('2014-01-27');
 
 $txTotal = 0.0; // This must match the total of the items.
 
 $mTxItem10 = new MigrationTransactionItem();
 $mTxItem10->setItemType('RecurringCharge');
 $mTxItem10->setSku($prod->getMerchantProductId());
 $discount = 0.1 ; 
    // This product got a 10% discount.
    // Specify a price that includes a discount.
 $description_array = $prod->getDescriptions();
 $the_description = $description_array[0];
 $mTxItem10->setName($the_description->description() 
    . ' - includes discounts (if any)');
 $price_array = $prod->getPrices();
 $the_price = $price_array[0];
 $productPrice = $the_price->getAmount();
 $discountedProductPrice = $productPrice - $productPrice * $discount;
 $mTxItem10->setPrice($discountedProductPrice);
 $txTotal = $txTotal + $discountedProductPrice;
 
 $mTxItem10->setTaxClassification('DC010500');
 $mTxItem10->setServicePeriodStartDate('2014-01-27');
 $mTxItem10->setServicePeriodEndDate('2014-02-26');
 // Let's use same period as the Billing Period end.
 // Construct tax line items applicable to the above product line item.
 $mTxTaxItem100 = new MigrationTaxItem();
 $mTxTaxItem100->setAmount(7.50);
 $mTxTaxItem100->setJurisdiction('STATE_06');
 $mTxTaxItem100->setName('CALIFORNIA STATE SALES TAX');
 $txTotal = $txTotal + 7.50;
 
 $mTxTaxItem101 = new MigrationTaxItem();
 $mTxTaxItem101->setAmount(1.00);
 $mTxTaxItem101->setJurisdiction('COUNTY_085');
 $mTxTaxItem101->setName('SANTA CLARA COUNTY SALES TAX');
 $txTotal = $txTotal + 1.00;
 
 $mTxItem10->setMigrationTaxItems( [ $mTxTaxItem100, $mTxTaxItem101 ] );
 
 $mt1->setMigrationTransactionItems( [ $mTxItem10 ] );
 
 $mt1->setAmount($txTotal);
 
 // Report this transaction in 'captured' status so it can be refunded.
 $status10 = new TransactionStatus();
 $status10->setStatus('Captured');
 $status10->setPaymentMethodType('CreditCard');
 $status10->setTimestamp('2014-01-27 02:36:57');
 $ccstatus10 = new TransactionStatusCreditCard();
 $ccstatus10->setAuthCode('000');
 $status10->setCreditCardStatus($ccstatus10);
 
 $mt1->setStatusLog( [ $status10 ] ); 
 
 // Now construct an array of the Transactions to be migrated.
 $migrTxs = [ $mt0, $mt1 ];
 
 // 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-27', $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());
 }
```

**Parent topic:**[Importing AutoBills from other Billing Systems to Subscribe](../topics/Importing_AutoBills_from)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.