#coding=utf-8
import traceback
from django.conf import settings
from alipay.aop.api.AlipayClientConfig import AlipayClientConfig
from alipay.aop.api.DefaultAlipayClient import DefaultAlipayClient
from alipay.aop.api.domain.AlipayTradePrecreateModel import AlipayTradePrecreateModel
from alipay.aop.api.domain.AlipayTradeQueryModel import AlipayTradeQueryModel
from alipay.aop.api.request.AlipayTradePrecreateRequest import AlipayTradePrecreateRequest
from alipay.aop.api.request.AlipayTradeQueryRequest import AlipayTradeQueryRequest
from alipay.aop.api.response.AlipayTradePrecreateResponse import AlipayTradePrecreateResponse
from alipay.aop.api.response.AlipayTradeQueryResponse import AlipayTradeQueryResponse

from utils.exceptions import CustomError

class Alipay():
    @staticmethod
    def payUnifiedOrder(payment_no,amount):
        if amount < 0.01:
            raise CustomError(u'无效的金额')

        alipay_client_config = AlipayClientConfig()

        try:
            alipay_client_config.app_id = settings.ALIPAY_SETTING['app_id']
            alipay_client_config.app_private_key = settings.ALIPAY_SETTING['app_private_key']
            alipay_client_config.alipay_public_key  = settings.ALIPAY_SETTING['alipay_public_key']
            notify_url = settings.ALIPAY_SETTING['notify_url']
        except:
            raise CustomError(u'没有配置支付宝对接信息')

        alipay_client_config.timeout = 120
        client = DefaultAlipayClient(alipay_client_config=alipay_client_config)

        model = AlipayTradePrecreateModel()
        model.out_trade_no = payment_no
        model.total_amount = amount
        model.subject = ""

        request = AlipayTradePrecreateRequest(biz_model=model)
        request.notify_url = notify_url

        try:
            response_content = client.execute(request)
        except Exception as e:
            traceback.print_exc()
            raise CustomError(str(e))
        if not response_content:
            raise CustomError(u'支付宝请求失败！')
        response = AlipayTradePrecreateResponse()
        response.parse_response_content(response_content)
        if not response.is_success():
            raise CustomError(response.code + "," + response.msg + "," + response.sub_code + "," + response.sub_msg)
        return response.qr_code

    @staticmethod
    def queryUnifiedOrder(payment_no):
        if not payment_no:
            raise CustomError(u'无效的支付单号')

        alipay_client_config = AlipayClientConfig()

        try:
            alipay_client_config.app_id = settings.ALIPAY_SETTING['app_id']
            alipay_client_config.app_private_key = settings.ALIPAY_SETTING['app_private_key']
            alipay_client_config.alipay_public_key  = settings.ALIPAY_SETTING['alipay_public_key']
        except:
            raise CustomError(u'没有配置支付宝对接信息')

        alipay_client_config.timeout = 120
        client = DefaultAlipayClient(alipay_client_config=alipay_client_config)

        model = AlipayTradeQueryModel()
        model.out_trade_no = payment_no

        request = AlipayTradeQueryRequest(biz_model=model)

        try:
            response_content = client.execute(request)
        except Exception as e:
            traceback.print_exc()
            raise CustomError(str(e))
        if not response_content:
            raise CustomError(u'支付宝请求失败！')
        response = AlipayTradeQueryResponse()
        response.parse_response_content(response_content)
        if not response.is_success():
            raise CustomError(response.code + "," + response.msg + "," + response.sub_code + "," + response.sub_msg)
        return response.buyer_pay_amount

