> ## 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.

# Handling Recurring Billing with Tokens

To enable customers to pay for recurring billing with tokens, construct an `AutoBill` object with a `BillingPlan` object that is transacted in tokens, and a token-based `PaymentMethod` object.

Create a Billing Plan priced in Tokens:

```

 //Create a new BillingPlan object
 $bp = new BillingPlan();
  
 // first, create a TokenAmount object that will be used as price of
 // the billing plan
 $tok = new Token();
 $tok->setMerchantTokenId("US_FREQ_BOOK_BUYER_PT");
 $tokAmt = new TokenAmount();
 $tokAmt->setToken($tok2);
 $tokAmt->setAmount(2); 
  
 // Price of the billing plan in terms of tokens
 $price = new BillingPlanPrice();
 $price->setTokenAmount($tokAmt);
  
 // create a billing plan period that uses the token-based price
 $bperiod = new BillingPlanPeriod();
 $bperiod->setType('Month');
 $bperiod->setQuantity(1); // a billing period of 1 month
 $bperiod->setCycles(0); // infinite
 $bperiod->setPrices(array($price)); 
 // token-based price as defined above
  
 // now create a billing plan that uses the period that uses 
 // token-based price
 $bp->setMerchantBillingPlanId("bpid-111");
 $bp->setDescription("Token priced billing plan");
 $bp->setPeriods(array($bperiod)); // use the period defined above
  
 // set other billing plan attributes below
  
 …
  
 // Next, create an AutoBill object for the BillingPlan object:
 $abill = new AutoBill();
  
 // Reference an existing account. The monthly transaction will 
 // deduct tokens from this account
 $acct = new Account();
 $acct->setMerchantAccountId('9876-5432');
 $abill->setAccount($acct);
  
 // Use the token-based billing plan created above
 $abill->setBillingPlan($bp);
  
 // you may also reference a Product that grants Tokens for 
 // an AutoBill (use this to exchange one type of tokens for 
 // another)
  
 // The payment method used for the autobill must be a token-based
 // payment method
 $pm = new PaymentMethod ();
 $pm->setType('Token');
  
 // since the billing plan uses tokens of certain type, create
 // Token object of that type to specify in the payment method
 $tok = new Token();
 $tok->setMerchantTokenId("US_FREQ_BOOK_BUYER_PT");
 $pm->setToken($tok);
  
 // set the payment method in the autobill
 $abill->setPaymentMethod($pm);
  
 // Populate other autobill attributes here
 …
 $dupBehave = 'Duplicate';
 $minChargebackProb = 100; // don't check for risk screen
 $validatePm = false; 
 // do not validate payment method since it is tokens
  
 // Make SOAP call to create the autobill
 $response = $abill->update($dupBehave, $validateP, $minChargebackProb); 
  
 if($response['returnCode']==200) {
 print 'AutoBill created!';
 }
```

The `AutoBill` object you created generates monthly transactions that decrement tokens from the related `Account` object. If you fetch the transactions with the Subscribe API, note the following:

* The `PaymentMethod` object in the `Transaction` object’s `sourcePaymentMethod` attribute has a type setting of `Token`. The `Token` attribute of the `PaymentMethod` object contains the token type for the transaction.
* The `currency` attribute of the `Transaction` object is `_VT`, Subscribe’s internal currency code for tokens.
* The `Transaction` object’s `amount` attribute refers to the total number of tokens used (decremented from the `Account` object in question) for the transaction.

Process a Token-based Transaction fetched from Subscribe:

```

 // Create a transaction object so we can make the fetch call
 $soapTx = new Transaction();
 // now load the most recently changed 100 transaction records,
 // using a date prior to Jan 1 1970
 $paymentMethod = null; 
 // do not filter returned transactions by payment method
 $since = '1969-01-01T00:00:00Z';
 $today = '2009-05-19 T00:00:00Z'; // until today
 $pageNumber = 0; // want to get the first page
 $pageSize = 100; // want 100 records in a page
 $response = $tx->fetchDeltaSince($since, $today, $paymentMethod, 
 $pageNumber, $pageSize);
 if($response['returnCode'] == 200) {
 $fetchedTxs = $response['transactions'];
 foreach ($fetchedTxs as $fetchedTx) {
 $srcPm = $fetchedTx->sourcePaymentMethod;
 if (srcPm->type == 'Token'&& $fetchedTx->currency == '_VT') 
 {
 // This is a token-based transaction
 print "This transaction used tokens of type " 
 . $srcPm->token->merchantTokenId . "\n";
 print "Amount of tokens decremented by this transaction " 
 . $tx->amount;
 }
 }
 }
```

**Parent topic:**[Working with Tokens](../topics/ProgGuide_Tokens)


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