Getting started =============== Billy has a set of simple REST API. It means that you can use your favorite programming language to call the API. By the time I am writing this, there is a `Python API for Billy`_ is under development. If you are using Python, you can use it instead of writing REST API calling by yourself. .. _`Python API for Billy`: https://github.com/victorlin/billy-client We will show you how to use Billy to process recurring payments for you in this section. If you want to know how to setup a Billy server, unfortunately, this document doesn't cover that part yet. Register a company ------------------ Before you can use the Billy API, you need to register a company first. A company here is basically an user entity in the Billy system, there will be a corresponding API key generated and associated with the company entity. You will need to pass your API key in order to access any other API of the Billy server. As Billy's default payment processor is `Balanced Payments`_, you will need to have an account and get a Balanced API key for registerion. .. _`Balanced Payments`: https://balancedpayments.com Okay, let's say your Balanced API key is `ef13dce2093b11e388de026ba7d31e6f`. To register a company, here you call :: curl https://billy.balancedpayments.com/v1/companies \ -X POST \ -d "processor_key=ef13dce2093b11e388de026ba7d31e6f" and you should see the response :: { "api_key": "5MyxREWaEymNWunpGseySVGBZkTWDW57FUXsyTo2WtGC", "created_at": "2014-02-08T08:22:10.629000+00:00", "guid": "CP4MXZG4ThUdbLpiX8e9Yx3j", "updated_at": "2014-02-08T08:22:10.629000+00:00" } Currently, there is no easy way to Retrieve or reset the API key, so please do not lost it. If you lost it, you can only contact the administrator to get it back. Create a plan ------------- A plan is a configuration for recurring payment processing, it contains the amount to debit or credit and the frequency of payment processing. For instance, you are running a hosting business, you have three hosting service types: * Small plan - 100GB storage, $5 USD/ mo * Middle plan - 200GB storage, $10 USD / mo * Large plan - 300GB storage, $15 USD / mo To debit your customer montly, you can create three plan entities in Billy system correspond to your three hosting service types. Here we demonstrate how to create a Billy plan for your first hosting type: :: curl https://billy.balancedpayments.com/v1/plans \ -X POST \ -u 5MyxREWaEymNWunpGseySVGBZkTWDW57FUXsyTo2WtGC: \ -d "plan_type=debit" \ -d "amount=500" \ -d "frequency=monthly" As we mentioned above, to call any API other than the one for registering a company, you need to pass your API key, it's easy, all you need to do is set it as the username for HTTP authentication. As you want to charge your hosting service customers rather than paying them out, the `plan_type` is `debit` here. The price for the plan is 5 USD dollars, but as the money unit in Billy system are all in cents, so here you set the `amount` to `500`. And finally, it is a monthly debit, so you set the `frequency` to `monthly`. Here we are, you should see the response looks like this one: :: { "amount": 500, "company_guid": "CP4MXZG4ThUdbLpiX8e9Yx3j", "created_at": "2014-02-08T08:22:11.508000+00:00", "deleted": false, "frequency": "monthly", "guid": "PL4RHCKW7GsGMjpcozHveQuw", "interval": 1, "plan_type": "debit", "updated_at": "2014-02-08T08:22:11.508000+00:00" } Create a customer ----------------- When you have a new customer who wants to subscribe your hosting service, you need to create a customer entity in Billy system for him. To create a customer, you can call the API like this: :: curl https://billy.balancedpayments.com/v1/customers \ -X POST \ -u 5MyxREWaEymNWunpGseySVGBZkTWDW57FUXsyTo2WtGC: and the response would be :: { "company_guid": "CP4MXZG4ThUdbLpiX8e9Yx3j", "created_at": "2014-02-08T08:22:10.904000+00:00", "deleted": false, "guid": "CU4NheTMcQqXgmAtg1aGTJPK", "processor_uri": "/v1/customers/CUCChwFzuMRlBGgoBwjRgqr", "updated_at": "2014-02-08T08:22:10.904000+00:00" } Before Billy system charging or paying out to the customer, a corresponding customer entity will be created in Balanced. Sometimes, you want to map the customer in Billy system to an existing customer in Balanced system, you can set the `processor_uri` parameter as the URI of customer in Balanced. :: curl https://billy.balancedpayments.com/v1/customers \ -X POST \ -u 5MyxREWaEymNWunpGseySVGBZkTWDW57FUXsyTo2WtGC: \ -d "processor_uri=/v1/customers/CU1jqOF9TocQXGIXjuMVrpMu" .. note:: As the name `processor_uri` implies, it **MUST** be the URI to a Balanced customer rahter than its GUID. Subscribe to a plan ------------------- So far so good, you have a customer and a plan in the Billy system, however, before you can subscribe the customer to the plan, you will need a funding source in Balanced system to charge. In most cases, the funding source is a tokenlized credit card number or a bank account. In this example, we use a tokenlized credit card number looks like this: :: /v1/marketplaces/TEST-MP6lD3dBpta7OAXJsN766qA/cards/CCBXYdbpYDwX68hv69UH1eS For how to generate a tokenlized credit card number, you can reference to the `Balanced documents here`_. .. _`Balanced documents here`: https://docs.balancedpayments.com/current/api.html?language=bash#tokenize-a-card With that funding source, to subscribe the customer to our plan, here we call :: curl https://billy.balancedpayments.com/v1/subscriptions \ -X POST \ -u 5MyxREWaEymNWunpGseySVGBZkTWDW57FUXsyTo2WtGC: \ -d "customer_guid=CU4NheTMcQqXgmAtg1aGTJPK" \ -d "plan_guid=PL4RHCKW7GsGMjpcozHveQuw" \ -d "funding_instrument_uri=/v1/marketplaces/TEST-MP6lD3dBpta7OAXJsN766qA/cards/CCBXYdbpYDwX68hv69UH1eS" Then, here comes the subscription response: :: { "amount": null, "appears_on_statement_as": null, "canceled": false, "canceled_at": null, "created_at": "2014-02-08T08:22:11.782000+00:00", "customer_guid": "CU4NheTMcQqXgmAtg1aGTJPK", "effective_amount": 500, "funding_instrument_uri": "/v1/marketplaces/TEST-MP6lD3dBpta7OAXJsN766qA/cards/CCBXYdbpYDwX68hv69UH1eS", "guid": "SU4ST39srWVLGbiTg174QyfF", "invoice_count": 1, "next_invoice_at": "2014-03-08T08:22:11.782000+00:00", "plan_guid": "PL4RHCKW7GsGMjpcozHveQuw", "started_at": "2014-02-08T08:22:11.782000+00:00", "updated_at": "2014-02-08T08:22:11.782000+00:00" } Congratulations! The Billy system just generated an invoice and charged the credit card for you, and it will generate invoices and try to debit that credit card monthly afterward. To view your invoices, you can visit :: /v1/subscriptions/