def parseUserInput(request_json): logger.debug('parseUserInput {0}'.format(request_json)) logger.debug('parseUserInput to string {0}'.format( json.dumps(request_json, ensure_ascii=False))) request_obj = TradeAPIRequest.parseFromJson(request_json) api_user = APIUserManager.get_api_user_by_apikey(request_obj.apikey) return request_obj, api_user
def test_validate_request(self): request_json = { "method": "wallet.trade.buy", "version": "1.0", "api_key": "L2CLMSBYJAPF0HX0PY4VIW0XFPCNT6Y8", "charset": "utf-8", "sign_type": "MD5", "timestamp": "20180630064937", "biz_content": "{\"api_account_type\":\"Account\",\"attach\":\"1235\",\"client_ip\":\"42.96.158.70\",\"expire_minute\":\"0\",\"meta_option\":\"123\",\"notify_url\":\"http://game.p2pinfo.cn/api_notify.php\",\"out_trade_no\":\"2018063006493725900\",\"payment_account\":\"13910978598\",\"payment_provider\":\"heepay\",\"return_url\":\"http://game.p2pinfo.cn/api_notify.php\",\"subject\":\"test\",\"total_fee\":\"1\"}", "sign": "E5AFC1466C676A2277DA3DC218480256" } request_obj = TradeAPIRequest.parseFromJson(request_json) str1 = 'api_key=L2CLMSBYJAPF0HX0PY4VIW0XFPCNT6Y8&biz_content={"api_account_type":"Account","attach":"1235","client_ip":"42.96.158.70","expire_minute":"0","meta_option":"123","notify_url":"http://game.p2pinfo.cn/api_notify.php","out_trade_no":"2018063006493725900","payment_account":"13910978598","payment_provider":"heepay","return_url":"http://game.p2pinfo.cn/api_notify.php","subject":"测试","total_fee":"1"}&charset=utf-8&method=wallet.trade.buy&sign_type=MD5×tamp=20180630064937&version=1.0&key=6521126bd7b0907aa2671c547db671f0' str2 = 'api_key=L2CLMSBYJAPF0HX0PY4VIW0XFPCNT6Y8&biz_content={"api_account_type":"Account","attach":"1235","client_ip":"42.96.158.70","expire_minute":"0","meta_option":"123","notify_url":"http://game.p2pinfo.cn/api_notify.php","out_trade_no":"2018063006493725900","payment_account":"13910978598","payment_provider":"heepay","return_url":"http://game.p2pinfo.cn/api_notify.php","subject":"测试","total_fee":"1"}&charset=utf-8&method=wallet.trade.buy&sign_type=MD5×tamp=20180630064937&version=1.0&key=6521126bd7b0907aa2671c547db671f0' self.maxDiff = None self.assertEqual(str1, str2) m = hashlib.md5() m.update(str1.encode('utf-8')) print("signature str 1 is {0}".format(m.hexdigest())) m = hashlib.md5() m.update(str2.encode('utf-8')) print("signature str 2 is {0}".format(m.hexdigest())) self.assertTrue( request_obj.is_valid('6521126bd7b0907aa2671c547db671f0')) """
def handle_pend_api_trans(api_trans): tradex = TradeExchangeManager() logger.info("handle_pend_api_trans: {0}".format( api_trans.original_request)) request_obj = TradeAPIRequest.parseFromJson( json.loads(api_trans.original_request)) tradex.post_sell_order(request_obj, api_trans.api_user, api_trans)
def cancel_order(request): try: logger.debug('receive request from: {0}'.format(request.get_host())) logger.info('receive request {0}'.format(request.body.decode('utf-8'))) try: request_json = json.loads(request.body.decode('utf-8')) except: raise ValueError(ERR_INVALID_JSON_INPUT) request_obj = TradeAPIRequest.parseFromJson(request_json) api_user = APIUserManager.get_api_user_by_apikey(request_obj.api_key) validate_request(request_obj, api_user, 'wallet.trade.cancel') tradeex = TradeExchangeManager() api_trans = tradeex.find_transaction(request_obj.trx_bill_no) sitesettings = context_processor.settings(request)['settings'] appId = sitesettings.heepay_app_id appKey = sitesettings.heepay_app_key # if trade status is alreadu in failed state, just return the status if api_trans.trade_status in [ 'ExpiredInvalid', 'DevClose', 'UserAbandon', 'PaidSuccess', 'PaidSuccess' ]: return JsonResponse( create_cancel_response(api_trans, api_user).to_json()) if api_trans.payment_status in ['Unknow', 'NotStart', 'PaySuccess']: if api_trans.payment_provider != 'heepay': logger.error( 'query_order_status(): found unsupported payment provider {0}' .format(api_trans.payment_provider)) raise ValueError('PAYMENT_PROVIDER_NOT_SUPPORTED') logger.info( 'api trans id {0}, reference_order {1}: payment_status: {2}. Query heepay for status...' .format(api_trans.transactionId, api_trans.reference_order.order_id, api_trans.payment_status)) heepay = HeePayManager() json_response = heepay.get_payment_status( api_trans.reference_order.order_id, api_trans.reference_bill_no, appId, appKey) ordermanager.update_order_with_heepay_notification( json_response, 'admin') api_trans.refresh_from_db() if api_trans.payment_status in ['Unknow', 'NotStart']: APIUserTransactionManager.abandon_trans(api_trans) api_trans.refresh_from_db() return JsonResponse( create_query_status_response(api_trans, api_user).to_json()) #TODO: should handle different error here. # what if network issue, what if the return is 30x, 40x, 50x except: error_msg = 'query_order_status()遇到错误: {0}'.format(sys.exc_info()[0]) logger.exception(error_msg) resp = create_error_trade_response(request_obj, api_user, '系统错误', '系统错误', '') return JsonResponse(resp.to_json())