import httplib
import time
import json

conn = httplib.HTTPSConnection("api.us.apiconnect.ibmcloud.com")
headers = {
    'x-ibm-client-id': "7872f061-154c-462e-a874-f2ed4c43b58c",
    'x-ibm-client-secret': "N2fJ1gG0nE4mA8uM0gW6uI8sQ3xG8dJ1bP8lH0dR5eO6uI1bU8",
    'content-type': "application/json",
    'accept': "application/json"
    }

class Transfer():
    def __init__(self, from_account, to_account, amount):
        self.from_account = from_account
        self.to_account = to_account
        self.amount = amount
        self.payload = "{\"channel_id\":\"BLUEMIX\",\"transaction_id\":\"%s\",\"source_account\":\"%s\",\"source_currency\":\"php\",\"target_account\":\"%s\",\"target_currency\":\"php\",\"amount\":%s}" %(int(time.time()),self.from_account, self.to_account, self.amount)
    def transfer(self):
        conn.request("POST", "/ubpapi-dev/sb/api/RESTs/transfer", self.payload, headers)
        res = conn.getresponse()
        data = res.read()
        return data

class Account():
    def __init__(self, account):
        self. account = account
    def summary(self):
        conn.request("GET", "/ubpapi-dev/sb/api/RESTs/getAccount?account_no=%s"%(self.account),headers=headers)
        res = conn.getresponse()
        data = res.read()
        return data
    def getBalance(self):
        sumJson = json.loads(self.summary())
        return sumJson[0]["current_balance"]

class BillsPayment():
    def __init__(self, account, biller_id,ref, amount):
        self.account = account
        self.biller_id = biller_id
        self.amount = amount
        self.ref = ref
    def payBills(self):
        conn = httplib.HTTPSConnection("api.us.apiconnect.ibmcloud.com")
        payload = "{\"channel_id\":\"BLUEMIX\",\"transaction_id\":\"%s\",\"source_account\":\"%s\",\"source_currency\":\"php\",\"biller_id\":\"%s\",\"reference1\":\"%s\",\"reference2\":\"000000P00A\",\"reference3\":\"00000Q000A\",\"amount\":%s}" %(int(time.time()/12),self.account,self.biller_id,self.ref,self.amount)
        conn.request("POST", "/ubpapi-dev/sb/api/RESTs/payment", payload, headers)
        res = conn.getresponse()
        data = res.read()
        return json.loads(data)

if __name__ == '__main__':
    j = Transfer(100171501394,102048048478,0)
    print(j.transfer())
    k = Account(101760280109)
    print k.summary()
    print k.getBalance()
    h = BillsPayment(100171501394,"TELCO","0019313AA",1)
    print h.payBills()['error_message']
