def test_parse_xml_message(self): wechat_pay_apis = WechatPayApis(self.test_app_id, self.test_mch_id, self.test_mch_key, self.test_wechat_public_key) xml_msg = '<xml><body>测试订单</body><openid>oUpF8uMuAJO_M2pxb1Q9zNjWeS6o</openid>' \ '<trade_type>JSAPI</trade_type><nonce_str>c7FP0AzOVMcu3lgek3OUxSMx4GXG' \ 'XoYN</nonce_str><mch_id>1313364101</mch_id><out_trade_no>201807161719' \ '57238778426463428649</out_trade_no><total_fee>22</total_fee><appid>wx' \ '028a01935f0d0fc2</appid><notify_url>http://mishitu.net</notify_url><s' \ 'pbill_create_ip>180.44.23.5</spbill_create_ip></xml>' params = wechat_pay_apis.parse_xml_message(xml_msg) expected_params = { 'body': u'\u6d4b\u8bd5\u8ba2\u5355', 'openid': 'oUpF8uMuAJO_M2pxb1Q9zNjWeS6o', 'trade_type': 'JSAPI', u'nonce_str': u'c7FP0AzOVMcu3lgek3OUxSMx4GXGXoYN', u'mch_id': '1313364101', 'out_trade_no': '20180716171957238778426463428649', 'total_fee': '22', u'appid': 'wx028a01935f0d0fc2', u'notify_url': 'http://mishitu.net', 'spbill_create_ip': '180.44.23.5' } assert len(params) == len(expected_params) for k, v in params.items(): assert v == expected_params[k]
def test_get_wechat_public_key(self): wechat_pay_apis = WechatPayApis(self.test_app_id, self.test_mch_id, self.test_mch_key, self.test_wechat_public_key, self.test_cert, self.test_cert_key) result = wechat_pay_apis.get_wechat_public_key() assert result['pub_key']
def test_generate_client_payment_params(self): wechat_pay_apis = WechatPayApis(self.test_app_id, self.test_mch_id, self.test_mch_key, self.test_wechat_public_key) client_payment_params = wechat_pay_apis.generate_client_payment_params( 'wx1716211050672758efcbdf2e1219937119') assert client_payment_params
def test_query_refund(self): wechat_pay_apis = WechatPayApis(self.test_app_id, self.test_mch_id, self.test_mch_key, self.test_wechat_public_key, self.test_cert, self.test_cert_key) result = wechat_pay_apis.refund_query( '20180907161025889841392482212063') assert result['refund_status_0'] == 'SUCCESS'
def test_decrypt_data(self): wechat_pay_apis = WechatPayApis(self.test_app_id, self.test_mch_id, self.test_mch_key, self.test_wechat_public_key, self.test_cert, self.test_cert_key) raw_data = wechat_pay_apis.decrypt_data( "Zi4PIwGuIhEQUh/z7zKEH0TWHqiwWW4Tfys/5o4YzEsqYVe1NndQk0HlDbfLkOTxdnJ56bxJSyuP9XXgBezmRdqwi/p6maI9P3+ZAyYxVBkR85aYwWXWvVWNQj2iIBmsGuOgi5pD3ZeqmCM3c4AwK9ifHJ48l8H4sj8elGQ1c2kPPgMS4zhn9W8cE6O1i8ai1h82NEr0E/aKebt62xb3FqBrVrioImH8BdFuSmUua0toatrpVWC+/kMAWWIBl2wyJC+bdsFFQVP+/0+PCuDFVSKUidkxzNE8e8g8AD2HRowcbILj5nDp4LvZ2WTx25mTezJoe6DDgRXinjgtSDB194NueanNMHEzE6E5TEiBCPfjI0Ag/59S0Ww7FSoJsaYhQAGYvyhIFpRWvycy7u0Ocr6CWF/gwAvPlLzQZ9wUoKlXVX74MaI1g5Pk0JjFQH9rcr8lh/mT5hUaeNxK5brQ39LZqeWlV+ygGOqEilNgDC41LKWckrnqSHVPqtbQcxQkEBr0SZ1mIhv1YrzisGPdtQge9co5zwYnmwIaQBBiqaLR4mN50dvv/6C0WRnfIygHJHvotzrIdtVJamRzpujvnXmnNTRaVpNwUxQj+dYPYMRcgdIqPv2g0r0CSreycLFwjK57q0NDDDmiRFjoCXThc7mYPCIHem6+7Tf0yNm95tZjjNYF2N9zWOwP2KC9ZzNhSMYHMwYVfPCofQkSed7PcJyMRDcNwRMsfNMAPT/yKifKuA4+h+UNgd4jRVQqUQpSyyxl2VfQb57GYUVkHY3T9GxwBgokeTlr89V/2Q/tov3aHMj1pFPMU8MsUQDprJgaJQqgQm8cr9irccxRCav7CEn3wS1xWfgXXhdLmrFpXztt7WlZ80xTBos3jb0HO+O5edNjaEkO9szLV0DL937MDBOlWvEj3WWc55ifRbRaBLYSQktyDGCUQtp5n0UoiJbiAZxh7UK/IRPQqKzwSD4jfufGkUYPBY6vjVPL5a3Y6K5+q/Hhu2YfK02OQ9RF0CJRkSPJd5X0jihUdDDR33TkDvccIhwf2l24dqdiSvwJLW5AIAmNWTYFroL+Zgs93bm2HcPoFsYOHnDY9+CZKrmZEw==" ) assert raw_data == """<root>
def test_place_order(self): wechat_pay_apis = WechatPayApis(self.test_app_id, self.test_mch_id, self.test_mch_key, self.test_wechat_public_key) result = wechat_pay_apis.place_order( order_title='测试订单', payment_serial_number=generate_serial_number(), total_fee=10000, notify_url='http://mishitu.net', spbill_create_ip='180.44.23.5', openid='oUkVN5ZFKfLOkAFwkk4oGYVc0rfg') assert result['prepay_id']
def test_pay_to_wechat(self): wechat_pay_apis = WechatPayApis('wx3f75ca357f606548', self.test_mch_id, self.test_mch_key, self.test_wechat_public_key, self.test_cert, self.test_cert_key) result = wechat_pay_apis.pay_to_wechat( partner_trade_no=generate_serial_number(), openid='ocBww1n_-2iKlWyp4lXBS73iWwAc', amount=1000, desc='测试付款', spbill_create_ip='180.2.4.23') assert result
def test_pay_to_bank(self): wechat_pay_apis = WechatPayApis(self.test_app_id, self.test_mch_id, self.test_mch_key, self.test_wechat_public_key, self.test_cert, self.test_cert_key) result = wechat_pay_apis.pay_to_bank( partner_trade_no=generate_serial_number(), bank_no='6214930121469976', true_name='谢汪益', bank_code=1001, amount=1, desc='测试付款') assert result
def test_refund(self): wechat_pay_apis = WechatPayApis(self.test_app_id, self.test_mch_id, self.test_mch_key, self.test_wechat_public_key, self.test_cert, self.test_cert_key) result = wechat_pay_apis.refund( payment_serial_number='20180918103408054636745554716518', refund_serial_number=generate_serial_number(), total_fee=2, refund_fee=2, notify_url= 'http://payserver.alpha.muchbo.com/api/user/payment_callback/wechat_refund_callback', ) assert result['refund_id']
def mock_withdraw_success(self, request=None, context=None): body = request.body params = WechatPayApis.parse_xml_message(body) partner_trade_no = params['partner_trade_no'] nonce_str = params['nonce_str'] return f"""<xml>
def mock_refund_query_success(self, request=None, content=None): body = request.body params = WechatPayApis.parse_xml_message(body) refund_serial_number = params['out_refund_no'] refund = Refund.objects.get(serial_number=refund_serial_number) paid_price = refund.payment.order_price - refund.payment.coupon.discount if refund.payment.coupon else refund.payment.order_price return f"""<xml>
def test_sign_message(self): wechat_pay_apis = WechatPayApis(self.test_app_id, self.test_mch_id, self.test_mch_key, self.test_wechat_public_key) params = { 'body': u'\u6d4b\u8bd5\u8ba2\u5355', 'openid': 'oUpF8uMuAJO_M2pxb1Q9zNjWeS6o', 'trade_type': 'JSAPI', u'nonce_str': u'c7FP0AzOVMcu3lgek3OUxSMx4GXGXoYN', u'mch_id': '1313364101', 'out_trade_no': '20180716171957238778426463428649', 'total_fee': 22, u'appid': 'wx028a01935f0d0fc2', u'notify_url': 'http://mishitu.net', 'spbill_create_ip': '180.44.23.5' } wechat_pay_apis.sign_message(params) assert params['sign'] == '345C48AAFB0C627E2F41E245204D95C8'
def mock_refund_success(self, request=None, content=None): request_params = WechatPayApis.parse_xml_message(request.body) return f"""<xml><return_code><![CDATA[SUCCESS]]></return_code>