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

# Purchasing Tokens

Because token balances are associated with an `Account` object, to allow customers to purchase tokens, you must define `Product` objects and specify how many tokens of a certain type to grant to a customer (`Account` object) when that customer purchases the related products. After each purchase, Subscribe adds the appropriate number of tokens to the balance attached to the `Account` object.

The following example creates a `Product` object that grants 10 tokens, of the type defined in the previous example, to an `Account` object that purchases the `Product` object.

Use a Product purchase to grant Tokens:

```

 $prod = new Product();
 $prod->setMerchantProductId("sku101");
  
 // Populate various product attributes here
 $tok = new Token();
  
 // we created token with this id already – we want to refer to the
 // same token type
  
 $tok->setMerchantTokenId("US_FREQ_BOOK_BUYER_PT");
  
 // create a TokenAmount object and populate it with token type 
 // and quantity
 $tokAmt = new TokenAmount();
 $tokAmt->setToken($tok);
 $tokAmt->setAmount(10);
  
 // we need an array of token amounts since a product can 
 // grant multiple token types. However, in our example the 
 // product grants only one token type
 $tokAmounts = array($tokAmt);
  
 $cr = new Credit();
 $cr->setTokenAmounts($tokAmounts)
  
 // Specify token amounted granted by purchase of this product
 $prod->setCreditGranted($cr);
  
 // Make SOAP call to create the product in Subscribe database
 $response = $prod->update();
  
 if($response['returnCode']==200) {
 print "Product created successfully";
 }
```

The `Product` object created can be used in both one-time and recurring billing:

* \*\*Recurring billing:\*\*Construct an `AutoBill` object with the `Product` object. With successful capture of each recurring transaction generated by the `AutoBill`, Subscribe adds the tokens granted by the `Product` to the `Account` object associated with the `AutoBill`.
* \*\*Real-time billing:\*\*Construct a one-time transaction that refers to the `Product` object as one of its transaction items. Specify the SKU of `TransactionItem` as `merchantProductId` for `Product`. With a successful capture of a one-time transaction, Subscribe adds the tokens granted by `Product` to the `Account` object associated with the transaction.

Use a Product that grants Tokens in a one-time Transaction:

```

 $tx = new Transaction();
  
 // Reference an existing account to which tokens are to be granted
 $account = new Account();
 $account->setMerchantAccountId('9876-5432');
 $tx->setAccount($account);
  
 // One of the line items of the transaction should be the product
 // that grants tokens
 $tx_item = new TransactionItem();
 $tx_item->setSku('sku101'); // the id of the product that grants tokens
 $tx_item->setName('Token granter product');
 $tx_item->setPrice(75.00);
 $tx_item->setQuantity(1);
 $tx->setTransactionItems(array($tx_item));
  
 // set other transaction attributes here
  
 $sendEmailNotification=false;
 $response = $tx->authCapture($sendEmailNotification); 
 // SOAP call
 if($response['returnCode']==200) {
 if($tx->statusLog[0]->status=='Authorized') {
 print "Purchase complete. Tokens granted";
 }
 }
```

**Parent topic:**[Incrementing Token Balances](../topics/Incrementing_Token_Balan)


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