Python支付宝退款示例说明

退款方法

class AliPay:
     #退款地址
    http_api_url = 'https://openapi.alipay.com/gateway.do'

    def __init__(self):
        self.client = self._init()

    def _init(self):
        alipay_client_config = AlipayClientConfig()
        alipay_client_config.app_id = config.app_id
        alipay_client_config.app_private_key = config.alipay_private_key
        alipay_client_config.alipay_public_key = config.alipay_public_key
        return DefaultAlipayClient(alipay_client_config=alipay_client_config, logger=logger)


    def trade_refund(self, biz_content: dict, charset: str = 'utf-8', sign_type='RSA2', version='1.0'):
        """退款
        :param biz_content: 必选字段:"out_trade_no","trade_no","refund_amount","refund_reason",""
        :param charset:
        :param sign_type:
        :param version:
        :return:
        """

        params = {
            'app_id': config.app_id, 'method': 'alipay.trade.refund', 'charset': charset, 'sign_type': sign_type,
            'timestamp': datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), 'version': version,
            'biz_content': json.dumps(biz_content)
        }
        sign = sign_with_rsa2(config.alipay_private_key, sign_content=generate_sign_text(params, sign_type=True),
                              charset=charset)
        params.update({'sign': sign})

        return requests.get(url=self.http_api_url, params=params)

订单调用

params={'out_trade_no': result['order_id'], 'trade_no': result['pay_id'],
                    'refund_amount': result['price'],
                    'refund_reason': '交易错误'}

ali_pay_api = AliPay()

ali_pay_api.trade_refund(biz_content=params)

接口说明文档

alipay.trade.refund(统一收单交易退款接口)

Last modification:November 26th, 2019 at 11:09 am
如果觉得我的文章对你有用,请随意赞赏

Leave a Comment