Exemplo n.º 1
0
    def wap_pay(self, order_no, order_name, total_fee, user_open_id, notify_url, client_ip):
        """
        公众号支付
        :param order_name:
        :param notify_url:
        :param order_no:
        :param client_ip:
        :param total_fee:
        :param user_open_id: 用户OPENID
        :return:
        """
        if not user_open_id:
            raise ValueError(u"user_open_id not can is null.")

        nonce_str = self.gen_random_nonce_str()
        prepay_data = dict(
            appid=self.app_id,
            mch_id=self.__partner_id,
            nonce_str=nonce_str,
            body=order_name,
            out_trade_no=order_no,
            total_fee=total_fee,
            spbill_create_ip=client_ip,
            notify_url=notify_url,
            trade_type="JSAPI",
            device_info="WEB",
            openid=user_open_id
        )

        sign_param = self.filter_params(**prepay_data)
        prepay_data["sign"] = self.make_sign(sign_param=sign_param)
        prepay_xml = xml_json.unparse({"xml": prepay_data})
        rep_xml = self._post(url=self.pay_url_v3, data=prepay_xml.encode('utf-8'))
        rep_dict = xml_json.parse(rep_xml)
        prepay_id = rep_dict.get("xml", {}).get('prepay_id')
        now_time = int(time.time())
        prepay = {
            'timeStamp': now_time,
            'nonceStr': nonce_str,
            'appId': self.app_id,
            'package': 'prepay_id={}'.format(prepay_id),
            'signType': 'MD5'
        }
        filter_prepay = self.filter_params(**prepay)
        prepay['sign'] = self.make_sign(filter_prepay)
        return prepay
Exemplo n.º 2
0
    def app_pay_v3(self, order_no, order_name, total_fee, product_id, notify_url, client_ip):
        """
        微信app支付(第三版本)
        :param order_no:
        :param order_name:
        :param total_fee: 支付金额(分)
        :param product_id: 商品ID
        :param notify_url:
        :param client_ip:
        :return:
        """
        if not product_id:
            raise ValueError(u"product_id not can null.")

        nonce_str = self.gen_random_nonce_str()
        prepay_data = dict(
            appid=self.app_id,
            mch_id=self.__partner_id,
            nonce_str=nonce_str,
            body=order_name,
            out_trade_no=order_no,
            total_fee=total_fee,
            spbill_create_ip=client_ip,
            notify_url=notify_url,
            trade_type="APP",
            product_id=product_id
        )
        sign_param = self.filter_params(**prepay_data)
        prepay_data["sign"] = self.make_sign(sign_param=sign_param)
        prepay_xml = xml_json.unparse({"xml": prepay_data})
        rep_xml = self._post(url=self.pay_url_v3, data=prepay_xml.encode('utf-8'))
        rep_dict = xml_json.parse(rep_xml)
        prepay_id = rep_dict.get("xml", {}).get('prepay_id')
        now_time = int(time.time())
        prepay = {
            'appid': self.app_id,
            'prepayid': prepay_id,
            'noncestr': nonce_str,
            'timestamp': now_time,
            'package': 'Sign=WXPay',
            'partnerid': self.__partner_id
        }
        filter_prepay = self.filter_params(**prepay)
        prepay['sign'] = self.make_sign(filter_prepay)
        return prepay
Exemplo n.º 3
0
    def web_refund(self, trade_no, refund_no, order_no, total_fee, refund_fee):
        """
        公众号退款
        :param trade_no: 交易流水号
        :param refund_no: 退款单号
        :param order_no: 订单号
        :param total_fee: 交易总金额(分)
        :param refund_fee: 退款金额(分)
        :rtype: tuple
        :return:
        """
        nonce_str = self.gen_random_nonce_str()
        prepay_data = dict(
            appid=self.app_id,
            mch_id=self.__partner_id,
            nonce_str=nonce_str,
            transaction_id=trade_no,
            out_trade_no=order_no,
            total_fee=total_fee,
            out_refund_no=refund_no,
            refund_fee=refund_fee,
            op_user_id=self.__partner_id
        )
        sign_param = self.filter_params(**prepay_data)
        prepay_data["sign"] = self.make_sign(sign_param=sign_param)
        prepay_xml = xml_json.unparse({"xml": prepay_data})

        rep_xml = self._post(
            url='https://api.mch.weixin.qq.com/secapi/pay/refund',
            data=prepay_xml.encode('utf-8'),
            cert=(self.apiclient_cert_path, self.apiclient_key_path)
        )

        notify = xml_json.parse(rep_xml).get("xml", {})
        old_sign = notify.get("sign")
        if not old_sign:
            return False, None
        sign_param = self.filter_params(**notify)
        new_sign = self.make_sign(sign_param=sign_param)

        if new_sign == old_sign:
            return True, notify
        else:
            return False, None