Alipay SDK for Python

repository·master·Indexed 43 days ago

https://github.com/alipay/alipay-sdk-python-all

The official Alipay SDK for Python for interacting with Ant Financial Open Platform APIs. It provides tools for managing payments, file uploads, and business services through the DefaultAlipayClient, supporting standard system API requests, page-based redirects, and mobile app payment triggers.

Tokens
2.1K
Snippets
5
Records
5
Agent score
5%

What's inside Alipay

  1. Upload files using the SDK

    master

    To upload files (e.g., alipay.offline.material.image.upload), use the FileItem class to wrap the file content. If a specific Model class is not available for the business logic, you can set attributes directly on the Request class.

    from alipay.aop.api.request.AlipayOfflineMaterialImageUploadRequest import AlipayOfflineMaterialImageUploadRequest
    from alipay.aop.api.FileItem import FileItem
    from alipay.aop.api.response.AlipayOfflineMaterialImageUploadResponse import AlipayOfflineMaterialImageUploadResponse
    
    request = AlipayOfflineMaterialImageUploadRequest()
    request.image_name = "my_store"
    request.image_type = "jpg"
    
    # Set file parameter
    with open("path/to/image.jpg", "rb") as f:
        request.image_content = FileItem(file_name="image.jpg", file_content=f.read())
    
    response_content = client.execute(request)
    
    response = AlipayOfflineMaterialImageUploadResponse()
    response.parse_response_content(response_content)
    if response.is_success():
        print("Image URL:", response.image_url)
  2. Execute a standard system API request

    master

    For standard system interfaces (e.g., alipay.trade.pay), follow these steps:

    1. Construct a business model object (e.g., AlipayTradePayModel) and populate its fields.
    2. Wrap the model in a request object (e.g., AlipayTradePayRequest).
    3. If you have common parameters like auth_token or app_auth_token, add them to the request.udf_params dictionary.
    4. Use client.execute(request) to perform the call.
    5. Use a response object (e.g., AlipayTradePayResponse) to parse the result using parse_response_content(response_content).
    from alipay.aop.api.request.AlipayTradePayRequest import AlipayTradePayRequest
    from alipay.aop.api.domain.AlipayTradePayModel import AlipayTradePayModel
    from alipay.aop.api.response.AlipayTradePayResponse import AlipayTradePayResponse
    
    # 1. Construct model
    model = AlipayTradePayModel()
    model.out_trade_no = "20180510AB014"
    model.total_amount = 1
    # ... populate other fields
    
    # 2. Construct request
    request = AlipayTradePayRequest(biz_model=model)
    
    # 3. Execute
    try:
        response_content = client.execute(request)
        
        # 4. Parse response
        response = AlipayTradePayResponse()
        response.parse_response_content(response_content)
        
        if response.is_success():
            print("Success! Trade No:", response.trade_no)
        else:
            print("Failed:", response.code, response.msg)
    except Exception as e:
        import traceback
        print(traceback.format_exc())
  3. Initialize the Alipay Client

    master

    To use the SDK, you must first configure an AlipayClientConfig object with your credentials and then instantiate a DefaultAlipayClient.

    Important: A single AlipayClientConfig instance should correspond to exactly one DefaultAlipayClient. If you need to use different configurations, you must define a new DefaultAlipayClient for each. It is highly recommended to pass a logger to the client to facilitate debugging.

    from alipay.aop.api.AlipayClientConfig import AlipayClientConfig
    from alipay.aop.api.DefaultAlipayClient import DefaultAlipayClient
    import logging
    
    # Setup logging
    logger = logging.getLogger('')
    
    # Configure client settings
    alipay_client_config = AlipayClientConfig()
    alipay_client_config.server_url = 'https://openapi.alipay.com/gateway.do'
    alipay_client_config.app_id = '[your app_id]'
    alipay_client_config.app_private_key = '[your app private key]'
    alipay_client_config.alipay_public_key = '[alipay public key]'
    
    # Create the client
    client = DefaultAlipayClient(alipay_client_config=alipay_client_config, logger=logger)
  4. Execute a page-based API request

    master

    For interfaces that require a user to interact with a web page (e.g., alipay.trade.page.pay), use the client.page_execute() method. This returns either a URL (if http_method='GET') or an HTML form snippet (if http_method='POST') which can be used to redirect the user to Alipay.

    from alipay.aop.api.request.AlipayTradePagePayRequest import AlipayTradePagePayRequest
    from alipay.aop.api.domain.AlipayTradePagePayModel import AlipayTradePagePayModel
    
    model = AlipayTradePagePayModel()
    model.out_trade_no = "pay201805020000226"
    model.total_amount = 50
    # ... populate other fields
    
    request = AlipayTradePagePayRequest(biz_model=model)
    
    # Returns a URL (GET) or HTML form (POST)
    response = client.page_execute(request, http_method="GET")
    print("Redirect user to:", response)
  5. Execute an SDK-based mobile payment request

    master

    For interfaces designed to trigger the Alipay mobile app (e.g., alipay.trade.app.pay), use the client.sdk_execute(request) method. This returns the string required to invoke the Alipay client on a mobile device.

    from alipay.aop.api.request.AlipayTradeAppPayRequest import AlipayTradeAppPayRequest
    from alipay.aop.api.domain.AlipayTradeAppPayModel import AlipayTradeAppPayModel
    
    model = AlipayTradeAppPayModel()
    model.out_trade_no = "201800000001201"
    model.total_amount = "9.00"
    # ... populate other fields
    
    request = AlipayTradeAppPayRequest(biz_model=model)
    
    # Returns the request string for the mobile app
    response = client.sdk_execute(request)
    print("Mobile SDK string:", response)