def parse_rsp_body(self, external_payment_method_detail_body): if type(external_payment_method_detail_body) == str: external_payment_method_detail_body = json.loads( external_payment_method_detail_body) if 'discountId' in external_payment_method_detail_body: self.__discount_id = external_payment_method_detail_body[ 'discountId'] if 'availableAmount' in external_payment_method_detail_body: available_amount = Amount() available_amount.parse_rsp_body( external_payment_method_detail_body['availableAmount']) self.__available_amount = available_amount if 'discountName' in external_payment_method_detail_body: self.__discount_name = external_payment_method_detail_body[ 'discountName'] if 'discountDescription' in external_payment_method_detail_body: self.__discount_description = external_payment_method_detail_body[ 'discountDescription'] if 'paymentMethodDetailMetadata' in external_payment_method_detail_body: self.__payment_method_detail_metadata = external_payment_method_detail_body[ 'paymentMethodDetailMetadata']
def parse_rsp_body(self, transaction_body): if type(transaction_body) == str: transaction_body = json.loads(transaction_body) if 'transactionResult' in transaction_body: transaction_result = Result() transaction_result.parse_rsp_body( transaction_body['transactionResult']) self.__transaction_result = transaction_result if 'transactionId' in transaction_body: self.__transaction_id = transaction_body['transactionId'] if 'transactionType' in transaction_body: transaction_type = TransactionType.value_of( transaction_body['transactionType']) self.__transaction_type = transaction_type if 'transactionStatus' in transaction_body: transaction_status = TransactionStatusType.value_of( transaction_body['transactionStatus']) self.__transaction_status = transaction_status if 'transactionAmount' in transaction_body: transaction_amount = Amount() transaction_amount.parse_rsp_body( transaction_body['transactionAmount']) self.__transaction_amount = transaction_amount if 'transactionRequestId' in transaction_body: self.__transaction_request_id = transaction_body[ 'transactionRequestId'] if 'transactionTime' in transaction_body: self.__transaction_time = transaction_body['transactionTime']
def __parse_rsp_body(self, rsp_body): response = super(AlipayCaptureResponse, self).parse_rsp_body(rsp_body) if 'captureRequestId' in response: self.__capture_request_id = response['captureRequestId'] if 'paymentId' in response: self.__payment_id = response['paymentId'] if 'captureId' in response: self.__capture_id = response['captureId'] if 'captureAmount' in response: capture_amount = Amount() capture_amount.parse_rsp_body(response['captureAmount']) self.__capture_amount = capture_amount if 'captureTime' in response: self.__capture_time = response['captureTime']
def __init__(self, payment_request_id, order, currency, amount_in_cents, payment_notify_url=None, payment_redirect_url=None, payment_expiry_time=None): super(AlipayPayRequest, self).__init__() self.path = '/ams/api/v1/payments/pay' self.product_code = ProductCodeType.IN_STORE_PAYMENT self.payment_amount = Amount(currency, amount_in_cents) self.payment_method = PaymentMethod() self.payment_method.payment_method_type = 'CONNECT_WALLET' self.payment_factor = PaymentFactor() self.payment_factor.in_store_payment_scenario = 'EntryCode' self.payment_request_id = payment_request_id self.order = order self.payment_notify_url = payment_notify_url self.payment_expiry_time = payment_expiry_time self.payment_redirect_url = payment_redirect_url
def pay_consult(): default_alipay_client = DefaultAlipayClient("https://open-na.alipay.com", CLIENT_ID, MERCHANT_PRIVATE_KEY, ALIPAY_PUBLICK_KEY) pay_consult_request = AlipayPayConsultRequest() pay_consult_request.path = "/ams/sandbox/api/v1/payments/consult" pay_consult_request.product_code = ProductCodeType.CASHIER_PAYMENT pay_consult_request.customer_id = "aac_123445566" payment_amount = Amount("PHP", "30000") pay_consult_request.payment_amount = payment_amount env = Env() env.terminal_type = TerminalType.APP env.os_types = OsType.IOS env.device_token_id = "60e62513a925bf5575111cad5976835b" env.client_ip = "127.0.0.1" pay_consult_request.env = env rsp_body = default_alipay_client.execute(pay_consult_request) pay_consult_response = AlipayPayConsultResponse(rsp_body) if pay_consult_response.result.result_status.name != ResultStatusType.F.name: print(pay_consult_response.payment_method_infos) else: print(pay_consult_response.result.result_message)
def __parse_rsp_body(self, rsp_body): response = super(AlipayCreateOrderResponse, self).parse_rsp_body(rsp_body) if 'paymentRequestId' in response: self.__payment_request_id = response['paymentRequestId'] if 'paymentId' in response: self.__payment_id = response['paymentId'] if 'paymentAmount' in response: payment_amount = Amount() payment_amount.parse_rsp_body(response['paymentAmount']) self.__payment_amount = payment_amount if 'clientPaymentToken' in response: self.__client_payment_token = response['clientPaymentToken'] if 'redirectActionForm' in response: redirect_action_form = RedirectActionForm() redirect_action_form.parse_rsp_body(response['redirectActionForm']) self.__redirect_action_form = redirect_action_form
def __parse_rsp_body(self, rsp_body): response = super(AlipayPayResponse, self).parse_rsp_body(rsp_body) if 'paymentRequestId' in response: self.__payment_request_id = response['paymentRequestId'] if 'paymentId' in response: self.__payment_id = response['paymentId'] if 'paymentAmount' in response: payment_amount = Amount() payment_amount.parse_rsp_body(response['paymentAmount']) self.__payment_amount = payment_amount if 'actualPaymentAmount' in response: actual_payment_amount = Amount() actual_payment_amount.parse_rsp_body( response['actualPaymentAmount']) self.__actual_payment_amount = actual_payment_amount if 'paymentQuote' in response: quote = Quote() quote.parse_rsp_body(response['paymentQuote']) self.__payment_quote = quote if 'paymentTime' in response: self.__payment_time = response['paymentTime'] if 'paymentCreateTime' in response: self.__payment_create_time = response['paymentCreateTime'] if 'authExpiryTime' in response: self.__auth_expiry_time = response['authExpiryTime'] if 'nonGuaranteeCouponValue' in response: self.__non_guarantee_coupon_value = response[ 'nonGuaranteeCouponValue'] if 'paymentActionForm' in response: self.__payment_action_form = response['paymentActionForm'] if 'pspCustomerInfo' in response: psp_customer_info = PspCustomerInfo() psp_customer_info.parse_rsp_body(response['pspCustomerInfo']) self.__psp_customer_info = psp_customer_info if 'challengeActionForm' in response: challenge_action_form = ChallengeActionForm() challenge_action_form.parse_rsp_body( response['challengeActionForm']) self.__challenge_action_form = challenge_action_form if 'redirectActionForm' in response: redirect_action_form = RedirectActionForm() redirect_action_form.parse_rsp_body(response['redirectActionForm']) self.__redirect_action_form = redirect_action_form if 'orderCodeForm' in response: order_code_form = OrderCodeForm() order_code_form.parse_rsp_body(response['orderCodeForm']) self.__order_code_form = order_code_form if 'extendInfo' in response: self.__extend_info = response['extendInfo']
def parse_rsp_body(self, coupon_payment_method_detail_body): if type(coupon_payment_method_detail_body) == str: coupon_payment_method_detail_body = json.loads(coupon_payment_method_detail_body) if 'couponId' in coupon_payment_method_detail_body: self.__coupon_id = coupon_payment_method_detail_body['couponId'] if 'availableAmount' in coupon_payment_method_detail_body: available_amount = Amount() available_amount.parse_rsp_body(coupon_payment_method_detail_body['availableAmount']) self.__available_amount = available_amount if 'couponName' in coupon_payment_method_detail_body: self.__coupon_name = coupon_payment_method_detail_body['couponName'] if 'couponDescription' in coupon_payment_method_detail_body: self.__coupon_description = coupon_payment_method_detail_body['couponDescription'] if 'couponExpireTime' in coupon_payment_method_detail_body: self.__coupon_expire_time = coupon_payment_method_detail_body['couponExpireTime'] if 'paymentMethodDetailMetadata' in coupon_payment_method_detail_body: self.__payment_method_detail_metadata = coupon_payment_method_detail_body['paymentMethodDetailMetadata']
def __parse_rsp_body(self, rsp_body): response = super(AlipayRefundResponse, self).parse_rsp_body(rsp_body) if 'refundRequestId' in response: self.__refund_request_id = response['refundRequestId'] if 'paymentId' in response: self.__payment_id = response['paymentId'] if 'refundId' in response: self.__refund_id = response['refundId'] if 'refundAmount' in response: refund_amount = Amount() refund_amount.parse_rsp_body(response['refundAmount']) self.__refund_amount = refund_amount if 'refundTime' in response: self.__refund_time = response['refundTime'] if 'refundNonGuaranteeCouponAmount' in response: refund_non_guarantee_coupon_amount = Amount() refund_non_guarantee_coupon_amount.parse_rsp_body( response['refundNonGuaranteeCouponAmount']) self.__refund_non_guarantee_coupon_amount = refund_non_guarantee_coupon_amount
def refund(): default_alipay_client = DefaultAlipayClient("https://open-na.alipay.com", CLIENT_ID, MERCHANT_PRIVATE_KEY, ALIPAY_PUBLICK_KEY) alipay_refund_request = AlipayRefundRequest() alipay_refund_request.path = "/ams/sandbox/api/v1/payments/refund" alipay_refund_request.refund_request_id = "python_test_refund_01" alipay_refund_request.payment_id = "060511159168271108778369768472747804ykDUtomosY202006080001966591" refund_amount = Amount("PHP", "9000") alipay_refund_request.refund_amount = refund_amount print(alipay_refund_request.to_ams_json()) rsp_body = default_alipay_client.execute(alipay_refund_request) alipay_refund_response = AlipayRefundResponse(rsp_body) if alipay_refund_response.result.result_status.name != ResultStatusType.F.name: print(alipay_refund_response.refund_request_id) else: print(alipay_refund_response.result.result_message)
def capture(): default_alipay_client = DefaultAlipayClient("https://open-na.alipay.com", CLIENT_ID, MERCHANT_PRIVATE_KEY, ALIPAY_PUBLICK_KEY) alipay_capture_request = AlipayCaptureRequest() alipay_capture_request.path = "/ams/sandbox/api/v1/payments/capture" alipay_capture_request.payment_id = "035622159167498257460439798959626996HSfvJALvrw202006080001969186" alipay_capture_request.capture_request_id = "python_test_03" capture_amount = Amount("PHP", "10000") alipay_capture_request.capture_amount = capture_amount print(alipay_capture_request.to_ams_json()) rsp_body = default_alipay_client.execute(alipay_capture_request) alipay_capture_response = AlipayCaptureResponse(rsp_body) if alipay_capture_response.result.result_status.name != ResultStatusType.F.name: print(alipay_capture_response.capture_id) else: print(alipay_capture_response.result.result_message)
def parse_rsp_body(self, amount_limit_body): if type(amount_limit_body) == str: amount_limit_body = json.loads(amount_limit_body) if 'maxAmount' in amount_limit_body: max_amount = Amount() max_amount.parse_rsp_body(amount_limit_body['maxAmount']) self.__max_amount = max_amount if 'minAmount' in amount_limit_body: min_amount = Amount() min_amount.parse_rsp_body(amount_limit_body['minAmount']) self.__min_amount = min_amount if 'remainAmount' in amount_limit_body: remain_amount = Amount() remain_amount.parse_rsp_body(amount_limit_body['remainAmount']) self.__remain_amount = remain_amount
def test_order_code(self): TEST_CLIENT_ID = 'T_385XSM502Y108602A' script_dir = os.path.dirname(__file__) with io.open(os.path.join(script_dir, '../private-pkcs1.pem'), encoding='utf-8') as pri, io.open( os.path.join(script_dir, '../public.pem'), encoding='utf-8') as pub: PRIVATE_KEY = pri.read() alipay_public_key = pub.read() order = Order() order.order_description = 'This is an order.' order.order_amount = Amount('USD', 1231) merchant = Merchant() merchant.reference_merchant_id = 'seller2322174590000' merchant.merchant_mcc = '7011' merchant.merchant_name = 'Some_Mer' store = Store() store.store_mcc = '7011' store.store_name = 'Some_Store' store.reference_store_id = 'store232217459000011' merchant.store = store order.merchant = merchant orderCodeReq = OrderCodePaymentRequest( payment_request_id=int(time.time()), order=order, currency="USD", amount_in_cents=1231, payment_notify_url='http://alipay.com/test') body = orderCodeReq.to_ams_json() #print(body) #print('-' * 120) # #DATA = body.encode('utf-8') #requestTime = get_cur_iso8601_time() # #req = urllib.request.Request(url='https://open-na.alipay.com/ams/api/v1/payments/pay', data=DATA,method='POST') #req.add_header("content-type","application/json; charset=UTF-8") #req.add_header("client-id",TEST_CLIENT_ID) #req.add_header("request-time",requestTime) #req.add_header("signature","algorithm=RSA256,keyVersion=1,signature=" + sign(orderCodeReq.http_method.value, orderCodeReq.path, TEST_CLIENT_ID, requestTime, body, PRIVATE_KEY)) # #with urllib.request.urlopen(req) as f: # #print(f.status) # print(f.read().decode('utf-8')) # print(f.headers) default_alipay_client = DefaultAlipayClient( "https://open-na.alipay.com", TEST_CLIENT_ID, PRIVATE_KEY, alipay_public_key) rsp_body = default_alipay_client.execute(orderCodeReq) response = AlipayPayResponse(rsp_body) #print(rsp_body) #print('-' * 120) def onF(response): print('onF: ' + response.result.result_code) def onU(response): print('onU: ' + response.result.result_code) def onS(response): print("%s, %s" % (response.payment_id, response.order_code_form.code_details[0].code_value)) self.assertIsNotNone( response.order_code_form.code_details[0].code_value) { 'F': onF, 'U': onU, 'S': onS }.get(response.result.result_status.value)(response)
def pay(): default_alipay_client = DefaultAlipayClient("https://open-na.alipay.com", CLIENT_ID, MERCHANT_PRIVATE_KEY, ALIPAY_PUBLICK_KEY) alipay_pay_request = AlipayPayRequest() alipay_pay_request.path = "/ams/sandbox/api/v1/payments/pay" alipay_pay_request.product_code = ProductCodeType.AGREEMENT_PAYMENT alipay_pay_request.payment_notify_url = "https://www.taobao.com" alipay_pay_request.payment_redirect_url = "https://www.taobao.com?param1=sl" alipay_pay_request.payment_request_id = "pay_python_test_06" payment_method = PaymentMethod() payment_method.payment_method_type = "GCASH" payment_method.payment_method_id = "20200404095550158596535057754730166962669640ZMvNAaATop" alipay_pay_request.payment_method = payment_method amount = Amount("PHP", "10000") alipay_pay_request.payment_amount = amount order = Order() order.reference_order_id = "102775765675669" order.order_description = "Mi Band 3 Wrist Strap Metal Screwless Stainless Steel For Xiaomi Mi Band 3" order.order_amount = amount goods_arr = [] good = Goods() good.goods_brand = "goods_brand" good.goods_name = "goods_name" goods_arr.append(good) order.goods = goods_arr merchant = Merchant() merchant.merchant_mcc = "merchantMcc" merchant.reference_merchant_id = "referenceMerchantId" order.merchant = merchant alipay_pay_request.order = order settlement_strategy = SettlementStrategy() settlement_strategy.settlement_currency = "USD" alipay_pay_request.settlement_strategy = settlement_strategy env = Env() env.terminal_type = TerminalType.APP env.os_type = OsType.IOS alipay_pay_request.env = env # alipay_pay_request.is_authorization = "true" print(alipay_pay_request.to_ams_json()) rsp_body = default_alipay_client.execute(alipay_pay_request) alipay_pay_response = AlipayPayResponse(rsp_body) if alipay_pay_response.result.result_status.name != ResultStatusType.F.name: print(alipay_pay_response.payment_id) print(alipay_pay_response.payment_create_time) else: print(alipay_pay_response.result.result_message)
def test_entry_code(self): TEST_CLIENT_ID = 'T_385XSM502Y108602A' script_dir = os.path.dirname(__file__) with io.open(os.path.join(script_dir, '../private-pkcs1.pem'), encoding='utf-8') as pri, io.open( os.path.join(script_dir, '../public.pem'), encoding='utf-8') as pub: PRIVATE_KEY = pri.read() alipay_public_key = pub.read() order = Order() order.order_description = 'This is an order.' order.order_amount = Amount('USD', 1231) merchant = Merchant() merchant.reference_merchant_id = 'seller2322174590000' merchant.merchant_mcc = '7011' merchant.merchant_name = 'Some_Mer' store = Store() store.store_mcc = '7011' store.store_name = 'Some_Store' store.reference_store_id = 'store232217459000011' merchant.store = store order.merchant = merchant order.env = Env() order.env.user_agent = "Mozilla/5.0 (iPhone; CPU iPhone OS 11_4_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15G77 NebulaSDK/1.8.100112 Nebula PSDType(1) AlipayDefined(nt:4G,ws:320|504|2.0) AliApp(AP/10.1.32.600) AlipayClient/10.1.32.600 Alipay Language/zh-Hans AlipayConnect" orderCodeReq = EntryCodePaymentRequest( payment_request_id=int(time.time()), order=order, currency="USD", amount_in_cents=1231, payment_notify_url='http://alipay.com/test') body = orderCodeReq.to_ams_json() #print(body) #print('-' * 120) # #DATA = body.encode('utf-8') #requestTime = get_cur_iso8601_time() # #req = urllib.request.Request(url='https://open-na.alipay.com/ams/api/v1/payments/pay', data=DATA,method='POST') #req.add_header("content-type","application/json; charset=UTF-8") #req.add_header("client-id",TEST_CLIENT_ID) #req.add_header("request-time",requestTime) #req.add_header("signature","algorithm=RSA256,keyVersion=1,signature=" + sign(orderCodeReq.http_method.value, orderCodeReq.path, TEST_CLIENT_ID, requestTime, body, PRIVATE_KEY)) # #with urllib.request.urlopen(req) as f: # #print(f.status) # print(f.read().decode('utf-8')) # print(f.headers) default_alipay_client = DefaultAlipayClient( "https://open-na.alipay.com", TEST_CLIENT_ID, PRIVATE_KEY, alipay_public_key) rsp_body = default_alipay_client.execute(orderCodeReq) response = AlipayPayResponse(rsp_body) #print(rsp_body) #print('-' * 120) def onF(response): print('onF: ' + response.result.result_code) def onU(response): print('onU: ' + response.result.result_code) def onS(response): print("%s, %s" % (response.payment_id, response.redirect_action_form.redirect_url)) self.assertIsNotNone(response.redirect_action_form.redirect_url) { 'F': onF, 'U': onU, 'S': onS }.get(response.result.result_status.value)(response)
def __parse_rsp_body(self, rsp_body): response = super(AlipayPayQueryResponse, self).parse_rsp_body(rsp_body) if 'paymentStatus' in response: payment_status = TransactionStatusType.value_of(response['paymentStatus']) self.__payment_status = payment_status if 'paymentRequestId' in response: self.__payment_request_id = response['paymentRequestId'] if 'paymentResultCode' in response: self.__payment_result_code = response['paymentResultCode'] if 'paymentResultMessage' in response: self.__payment_result_message = response['paymentResultMessage'] if 'paymentId' in response: self.__payment_id = response['paymentId'] if 'paymentRequestId' in response: self.__payment_request_id = response['paymentRequestId'] if 'authPaymentId' in response: self.__auth_payment_id = response['authPaymentId'] if 'paymentAmount' in response: payment_amount = Amount() payment_amount.parse_rsp_body(response['paymentAmount']) self.__payment_amount = payment_amount if 'actualPaymentAmount' in response: actual_payment_amount = Amount() actual_payment_amount.parse_rsp_body(response['actualPaymentAmount']) self.__actual_payment_amount = actual_payment_amount if 'paymentQuote' in response: quote = Quote() quote.parse_rsp_body(response['paymentQuote']) self.__payment_quote = quote if 'paymentTime' in response: self.__payment_time = response['paymentTime'] if 'paymentCreateTime' in response: self.__payment_create_time = response['paymentCreateTime'] if 'authExpiryTime' in response: self.__auth_expiry_time = response['authExpiryTime'] if 'nonGuaranteeCouponValue' in response: self.__non_guarantee_coupon_value = response['nonGuaranteeCouponValue'] if 'paymentActionForm' in response: self.__payment_action_form = response['paymentActionForm'] if 'pspCustomerInfo' in response: psp_customer_info = PspCustomerInfo() psp_customer_info.parse_rsp_body(response['pspCustomerInfo']) self.__psp_customer_info = psp_customer_info if 'redirectActionForm' in response: redirect_action_form = RedirectActionForm() redirect_action_form.parse_rsp_body(response['redirectActionForm']) self.__redirect_action_form = redirect_action_form if 'extendInfo' in response: self.__extend_info = response['extendInfo'] if 'transactions' in response: transactions = [] for transaction_body in response['transactions']: transaction = Transaction() transaction.parse_rsp_body(transaction_body) transactions.append(transaction) self.__transactions = transactions