Beispiel #1
0
    def test_wechatpay_get_info(self):
        plan = ChargePlan()
        plan.name = u'商品商品'
        plan.value = 10000
        plan.cost = 100
        plan.save()

        from goldencage.views import wechat_pay_get_access_token
        from goldencage.views import wechatpay_get_info
        content = wechat_pay_get_access_token()
        access_token = content.get('access_token')
        if not access_token:
            print content
        data, err = wechatpay_get_info(
            access_token, plan.id, '123321', '127.0.0.1', 'traceiddd')
Beispiel #2
0
class WechatpayTest(TestCase):

    def setUp(self):
        self.user = User.objects.create_user('jeff',
                                             '*****@*****.**', '123')
        self.plan = ChargePlan(name=u'plan1', code='plan1',
                               value=30, cost=750, coupon=50)
        self.plan.save()

    def test_gen_package(self):
        cli = Client()
        package = {'bank_type': 'WX', 'body': '千足 金箍棒',
                   'fee_type': '1', 'input_charset': 'UTF-8',
                   'getOutTradNo': '81282373272',
                   'spbill_create_ip': '192.168.1.1', 'total_fee': '1'}
        data = {'traceid': 'ikaoiaos', 'package': package}
        data = json.dumps(data)
        print 'data = %s' % data
        rsp = cli.post('/gc/wechatpaypackage/',
                       data=data, content_type='application/json')
        print rsp.content

    def test_xml_to_dict(self):
        from .views import _wechatpay_xml_to_dict
        raw_str = ("""
            <xml>
                <OpenId><![CDATA[111222]]></OpenId>
                <AppId><![CDATA[wwwwb4f85f3a797777]]></AppId>
                <IsSubscribe>1</IsSubscribe>
                <TimeStamp> 1369743511</TimeStamp>
                <NonceStr><![CDATA[jALldRTHAFd5Tgs5]]></NonceStr>
                <AppSignature><![CDATA[bafe07f060fdb4b5ff756f973aecffa]]>
                </AppSignature>
                <SignMethod><![CDATA[sha1]]></SignMethod >
            </xml>""")
        dict_ret = _wechatpay_xml_to_dict(raw_str)
        print dict_ret

    def test_wechatpay_get_info(self):
        plan = ChargePlan()
        plan.name = u'商品商品'
        plan.value = 10000
        plan.cost = 100
        plan.save()

        from goldencage.views import wechat_pay_get_access_token
        from goldencage.views import wechatpay_get_info
        content = wechat_pay_get_access_token()
        access_token = content.get('access_token')
        if not access_token:
            print content
        data, err = wechatpay_get_info(
            access_token, plan.id, '123321', '127.0.0.1', 'traceiddd')

    def test_wechatpay_notify(self):
        order = Order(id=1115, user=self.user, plan=self.plan, value=30)
        order.save()

        body = """
        <xml><OpenId><![CDATA[oaCDJju5TzPSv0ZT_GP5nLsPAQfY]]></OpenId>
<AppId><![CDATA[wx6745aaa6e2878f99]]></AppId>
<IsSubscribe>0</IsSubscribe>
<TimeStamp>1418365289</TimeStamp>
<NonceStr><![CDATA[kLI9t3MWRx4RYZVu]]></NonceStr>
<AppSignature><![CDATA[16d03d4341d62a3d635c7593cc84eb1554c36205]]></AppSignature>
<SignMethod><![CDATA[sha1]]></SignMethod>
</xml>
        """

        params = 'bank_billno=201412126100895338&bank_type=3006&discount=0&fee_type=1&input_charset=UTF-8&notify_id=epFRTtDSAK6AGztmmEb5cOpCQCzg06fiAj8D9w6l_0VbjHy2_6NnDpKIs5un-g5TJTsCCDC1ZA8jFy3WY2VV1nWNYehhK-Tg&out_trade_no=1115&partner=1222813501&product_fee=1&sign=6265C0C62683BE1F5F7C6D688A25CD00&sign_type=MD5&time_end=20141212142129&total_fee=1&trade_mode=1&trade_state=0&transaction_id=1222813501201412126039873136&transport_fee=0'
        url = '/gc/wechatcb/?%s' % params
        cli = Client()
        rsp = cli.post(url, data=body, content_type='application/xml')
        print '+++++++++++++++++'
        print rsp.content
Beispiel #3
0
 def setUp(self):
     self.user = User.objects.create_user('jeff',
                                          '*****@*****.**', '123')
     self.plan = ChargePlan(name=u'plan1', code='plan1',
                            value=30, cost=750, coupon=50)
     self.plan.save()
Beispiel #4
0
class AlipayCallbackTest(TestCase):

    def setUp(self):
        self.user = User.objects.create_user('jeff',
                                             '*****@*****.**', '123')
        self.plan = ChargePlan(name=u'plan1', code='plan1',
                               value=3000, cost=750, coupon=50)
        self.plan.save()

    def create_payment_data(self):
        order = Order(user=self.user, plan=self.plan, value=3000)
        order.save()
        return {'notify_time': '', 'notify_type': 'trade_status_sync',
                'notify_id': 'csdfo834jr', 'sign_type': 'RSA',
                'sign': 'no sign this time',
                'out_trade_no': order.pk, 'subject': u'多啦A梦',
                'payment_type': 1, 'trade_no': '2014112323e',
                'trade_status': 'TRADE_FINISHED', 'seller_id': '2088xx',
                'seller_email': '*****@*****.**', 'buyer_id': '2088yy',
                'buyer_email': '*****@*****.**', 'total_fee': 30,
                'quantity': 1, 'price': 30, 'body': u'不错的叮当主题哦',
                'gmt_create': '', 'gmt_payment': '',
                'is_total_fee_adjust': 'N', 'use_coupon': 'N', 'discount': '0'}

    def test_alipay_callback(self):
        # 正常流程, 第一次状态为等待付款,第二次为交易完成
        data = self.create_payment_data()
        c = Client()
        data['trade_status'] = 'WAIT_BUYER_PAY'
        payment_done.send = Mock()
        task_done.send = Mock()
        views.verify_notify_id = Mock(return_value=True)
        views.verify_alipay_signature = Mock(return_value=True)
        cache.set = Mock(return_value=None)

        rsp = c.get(reverse('alipay_cb'), data)
        self.assertEqual('success', rsp.content)
        self.assertEqual(payment_done.send.call_count, 0)
        self.assertEqual(task_done.send.call_count, 0)

        data['trade_status'] = 'TRADE_FINISHED'
        rsp = c.get(reverse('alipay_cb'), data)
        self.assertEqual('success', rsp.content)
        cost = int(round(config.EXCHANGE_RATE * 30))
        payment_done.send.assert_called_with(sender=Charge,
                                             cost=cost, user=self.user)
        task_done.send.assert_called_with(sender=Task, cost=50,
                                          user=self.user)

    def test_alipay_callback_sign_error(self):
        # 签名错误
        data = self.create_payment_data()
        c = Client()
        views.verify_notify_id = Mock(return_value=True)
        views.verify_alipay_signature = Mock(return_value=False)
        rsp = c.get(reverse('alipay_cb'), data)
        self.assertEqual('error', rsp.content)

    def test_alipay_callback_invalidate_request(self):
        # 非来自支付宝的请求
        data = self.create_payment_data()
        c = Client()
        views.verify_notify_id = Mock(return_value=False)
        views.verify_alipay_signature = Mock(return_value=True)
        rsp = c.get(reverse('alipay_cb'), data)
        self.assertEqual('error', rsp.content)

    def test_alipay_notifyid_duplicated(self):
        # 重复收到同一个通知。通知ID同样。
        data = self.create_payment_data()
        views.verify_notify_id = Mock(return_value=True)
        views.verify_alipay_signature = Mock(return_value=True)

        cache.get = Mock(return_value=None)
        cache.set = Mock()
        payment_done.send = Mock()

        c = Client()
        rsp = c.get(reverse('alipay_cb'), data)
        self.assertEqual('success', rsp.content)
        payment_done.send.assert_called_with(sender=Charge, cost=750,
                                             user=self.user)

        cache.get = Mock(return_value='123')
        payment_done.send = Mock()
        rsp = c.get(reverse('alipay_cb'), data)
        self.assertEqual('error', rsp.content)

        self.assertTrue(cache.get.assert_called)
        self.assertEqual(0, payment_done.send.call_count)

    def test_alipay_callback_status_revert(self):
        # 同一个帐单,状态以先后不同的顺序回调。
        data = self.create_payment_data()
        data['trade_status'] = 'TRADE_FINISHED'
        views.verify_notify_id = Mock(return_value=True)
        views.verify_alipay_signature = Mock(return_value=True)
        cache.set = Mock(return_value=None)
        payment_done.send = Mock()
        c = Client()
        rsp = c.get(reverse('alipay_cb'), data)
        self.assertEqual('success', rsp.content)

        self.assertEqual(1, payment_done.send.call_count)

        payment_done.send = Mock()
        data['trade_status'] = 'WAIT_BUYER_PAY'
        data['notify_id'] = 'another_notify'
        rsp = c.get(reverse('alipay_cb'), data)
        self.assertEqual(0, payment_done.send.call_count)
        self.assertEqual('success', rsp.content)
        self.assertEqual(2, cache.set.call_count)

    def test_alipay_callback_duplicated(self):
        # 同一个帐单,相同状态重复发送,将不会充值成功。
        data = self.create_payment_data()
        data['trade_status'] = 'WAIT_BUYER_PAY'

        views.verify_notify_id = Mock(return_value=True)
        views.verify_alipay_signature = Mock(return_value=True)
        cache.set = Mock()
        payment_done.send = Mock()
        c = Client()
        rsp = c.get(reverse('alipay_cb'), data)
        self.assertEqual('success', rsp.content)

        data['notify_id'] = 'another_notify'
        rsp = c.get(reverse('alipay_cb'), data)
        self.assertEqual('error', rsp.content)

        self.assertEqual(1, cache.set.call_count)
        self.assertEqual(0, payment_done.send.call_count)

    def test_signature(self):
        """ 测试之前,要去settings拷贝一个支付宝公钥
            或者不对这个做单元测试
        """
        sign = (u"DoqHII4KFb5QRp5J/bAQPMI/1nJwHf8IcVHDZvvNR5CHCEmAkelExygYooWi"
                "yWchcBd2WHULCNtPKqFEWQALTynzUAkeF64zq9nyq8nzrVulwcKGnu+l"
                "ja6Sg+2EILb3o8RuFcPOL/YAD5y1FxjJBUM33Z+LDcWgb/+eSMDiTQk=")
        params = {
            u"seller_email": u"*****@*****.**",
            u"gmt_close": u"2014-09-02 11:37:03",
            u"sign": sign,
            u"subject": u"资助20元,赠送520金币",
            u"is_total_fee_adjust": u"N",
            u"gmt_create": u"2014-09-02 11:37:02",
            u"out_trade_no": u"117800",
            u"sign_type": u"RSA",
            u"price": u"20.00",
            u"buyer_email": u"*****@*****.**",
            u"discount": u"0.00",
            u"trade_status": u"TRADE_FINISHED",
            u"gmt_payment": u"2014-09-02 11:37:03",
            u"trade_no": u"2014090200701660",
            u"seller_id": u"2088311247579029",
            u"use_coupon": u"N",
            u"payment_type": u"1",
            u"total_fee": u"20.00",
            u"notify_time": u"2014-09-02 11:37:41",
            u"buyer_id": u"2088502310925605",
            u"notify_id": u"be431b210180989044cc985639b2a8635c",
            u"notify_type": u"trade_status_sync",
            u"quantity": u"1"
        }
        print 'views %s' % views.verify_alipay_signature
        result = views.verify_alipay_signature('RSA', sign, params)
        self.assertEqual(True, result)