### Complete Payment Flow Example Source: https://context7.com/cpcn-ios/cpcnpaysdk/llms.txt A comprehensive example demonstrating how to integrate both Alipay and WeChat Pay within an iOS application. It includes logic for selecting the payment method, initiating the respective payment SDK, and handling the results. Proper error handling and UI updates are crucial for a good user experience. ```objective-c #import #import typedef NS_ENUM(NSInteger, PaymentMethod) { PaymentMethodAlipay, PaymentMethodWeixin }; @interface PaymentManager : NSObject - (void)payWithMethod:(PaymentMethod)method authCode:(NSString *)authCode; @end @implementation PaymentManager - (void)payWithMethod:(PaymentMethod)method authCode:(NSString *)authCode { if (authCode.length == 0) { NSLog(@"授权码不能为空"); return; } switch (method) { case PaymentMethodAlipay: { NSString *scheme = [[NSBundle mainBundle] bundleIdentifier]; NSString *alipayScheme = [NSString stringWithFormat:@"alipay%@", scheme]; BOOL result = [[CPCNAlipay shared] aliPayWithAuthCode:authCode fromScheme:alipayScheme wapcallback:^(NSDictionary *resultDict) { [self handleAlipayResult:resultDict]; }]; if (!result) { [self handlePaymentError:@"支付宝调起失败"]; } break; } case PaymentMethodWeixin: { [[CPCNWeixinPay shared] wxPayWithAuthCode:authCode completion:^(BOOL success) { [self handleWeixinResult:success]; }]; break; } } } - (void)handleAlipayResult:(NSDictionary *)resultDict { // 支付宝结果状态码处理 // 9000: 支付成功 // 8000: 正在处理中 // 4000: 支付失败 // 6001: 用户取消 // 6002: 网络连接错误 NSString *status = resultDict[@"resultStatus"]; NSLog(@"支付宝支付结果: %@", status); } - (void)handleWeixinResult:(BOOL)success { NSLog(@"微信支付结果: %@", success ? @"成功" : @"失败"); } - (void)handlePaymentError:(NSString *)error { NSLog(@"支付错误: %@", error); } @end ``` -------------------------------- ### Complete Payment Flow Example Source: https://context7.com/cpcn-ios/cpcnpaysdk/llms.txt Demonstrates a comprehensive payment flow, handling user selection between Alipay and WeChat Pay, including error handling and UI updates. ```APIDOC ## Complete Payment Flow Example ### Description This example demonstrates how to integrate both Alipay and WeChat Pay within an iOS application. It shows how to select the appropriate payment method based on user choice, initiate the payment using the respective SDK methods, and handle the results and potential errors. This example assumes you have a `PaymentManager` class to orchestrate the process. ### Method N/A (Illustrative example combining SDK methods) ### Endpoint N/A ### Parameters N/A (This is a client-side implementation example) ### Request Example ```objective-c #import #import typedef NS_ENUM(NSInteger, PaymentMethod) { PaymentMethodAlipay, PaymentMethodWeixin }; @interface PaymentManager : NSObject - (void)payWithMethod:(PaymentMethod)method authCode:(NSString *)authCode; @end @implementation PaymentManager - (void)payWithMethod:(PaymentMethod)method authCode:(NSString *)authCode { if (authCode.length == 0) { NSLog(@"Authorization code cannot be empty"); return; } switch (method) { case PaymentMethodAlipay: { NSString *scheme = [[NSBundle mainBundle] bundleIdentifier]; NSString *alipayScheme = [NSString stringWithFormat:@"alipay%@", scheme]; BOOL result = [[CPCNAlipay shared] aliPayWithAuthCode:authCode fromScheme:alipayScheme wapcallback:^(NSDictionary *resultDict) { [self handleAlipayResult:resultDict]; }]; if (!result) { [self handlePaymentError:@"Failed to initiate Alipay payment"]; } break; } case PaymentMethodWeixin: { [[CPCNWeixinPay shared] wxPayWithAuthCode:authCode completion:^(BOOL success) { [self handleWeixinResult:success]; }]; break; } } } - (void)handleAlipayResult:(NSDictionary *)resultDict { // Alipay result status code handling // 9000: Payment successful // 8000: Processing // 4000: Payment failed // 6001: User cancelled // 6002: Network connection error NSString *status = resultDict[@"resultStatus"]; NSLog(@"Alipay payment result: %@", status); // Update UI based on status } - (void)handleWeixinResult:(BOOL)success { NSLog(@"WeChat payment result: %@", success ? @"Success" : @"Failed"); // Update UI based on success status if (success) { [self updateOrderStatus:OrderStatusPaid]; [self showPaymentSuccessView]; } else { [self showPaymentFailedAlert]; } } - (void)handlePaymentError:(NSString *)error { NSLog(@"Payment error: %@", error); // Display error message to user } // Placeholder methods for UI updates and order status - (void)updateOrderStatus:(NSInteger)status {} - (void)showPaymentSuccessView {} - (void)showPaymentFailedAlert {} @end ``` ### Response (Responses are handled via callbacks within the `aliPayWithAuthCode:fromScheme:wapcallback:` and `wxPayWithAuthCode:completion:` methods.) ``` -------------------------------- ### Get CPCNWeixinPay Singleton Instance Source: https://context7.com/cpcn-ios/cpcnpaysdk/llms.txt Obtain the shared singleton instance of CPCNWeixinPay for WeChat Pay functionalities. All WeChat Pay operations should be performed through this instance. ```objective-c // 获取微信支付单例 CPCNWeixinPay *weixinPay = [CPCNWeixinPay shared]; ``` -------------------------------- ### Get CPCNAlipay Singleton Instance Source: https://context7.com/cpcn-ios/cpcnpaysdk/llms.txt Retrieve the shared singleton instance of CPCNAlipay to access Alipay payment functionalities. This instance should be used for all Alipay-related operations. ```objective-c // 获取支付宝支付单例 CPCNAlipay *alipay = [CPCNAlipay shared]; ``` -------------------------------- ### Initiate Alipay Payment with Auth Code Source: https://context7.com/cpcn-ios/cpcnpaysdk/llms.txt Call the Alipay client for payment using an authorization code. Requires the authorization code, a URL Scheme configured in Info.plist, and a callback block for WAP payment results. Ensure the URL Scheme is correctly set up in your project's Info.plist. ```objective-c #import // 在Info.plist中配置URL Scheme,例如: alipay + bundleId NSString *schemeStr = @"alipayYourBundleIdentifier"; NSString *authCode = @"your_authorization_code_from_server"; // 调起支付宝支付 BOOL success = [[CPCNAlipay shared] aliPayWithAuthCode:authCode fromScheme:schemeStr wapcallback:^(NSDictionary *resultDict) { // 处理WAP支付结果 NSString *resultStatus = resultDict[@"resultStatus"]; if ([resultStatus isEqualToString:@"9000"]) { NSLog(@"支付成功"); // 更新UI,显示支付成功状态 } else if ([resultStatus isEqualToString:@"6001"]) { NSLog(@"用户取消支付"); } else { NSLog(@"支付失败: %@", resultDict[@"memo"]); } }]; if (!success) { NSLog(@"调起支付宝失败,请检查授权码或URL Scheme配置"); } ``` -------------------------------- ### Alipay Payment Call - aliPayWithAuthCode:fromScheme:wapcallback: Source: https://context7.com/cpcn-ios/cpcnpaysdk/llms.txt Initiates an Alipay payment using an authorization code, requiring the URL scheme and a callback for WAP payment results. ```APIDOC ## Alipay Payment Call - aliPayWithAuthCode:fromScheme:wapcallback: ### Description Initiates an Alipay payment by invoking the Alipay client using an authorization code. Requires the authorization code, the URL Scheme configured in Info.plist, and a completion block for WAP payment results. If the `completionBlock` of the `processOrderWithPaymentResult` interface is nil, the provided `wapcallback` will be used for result callbacks. ### Method `aliPayWithAuthCode:fromScheme:wapcallback:` ### Endpoint N/A (Method of CPCNAlipay singleton) ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A (Parameters are passed directly to the method) - **authCode** (NSString) - Required - The authorization code obtained from the server. - **fromScheme** (NSString) - Required - The URL Scheme configured in Info.plist (e.g., `alipay` + `bundleId`). - **wapcallback** (^(NSDictionary *resultDict)) - Optional - A block that handles the WAP payment result. If nil, a default callback might be used. ### Request Example ```objective-c #import // Configure URL Scheme in Info.plist, e.g.: alipay + bundleId NSString *schemeStr = @"alipayYourBundleIdentifier"; NSString *authCode = @"your_authorization_code_from_server"; // Initiate Alipay payment BOOL success = [[CPCNAlipay shared] aliPayWithAuthCode:authCode fromScheme:schemeStr wapcallback:^(NSDictionary *resultDict) { // Handle WAP payment result NSString *resultStatus = resultDict[@"resultStatus"]; if ([resultStatus isEqualToString:@"9000"]) { NSLog(@"Payment successful"); // Update UI, show payment success status } else if ([resultStatus isEqualToString:@"6001"]) { NSLog(@"User cancelled payment"); } else { NSLog(@"Payment failed: %@", resultDict[@"memo"]); } }]; if (!success) { NSLog(@"Failed to initiate Alipay payment. Please check authorization code or URL Scheme configuration."); } ``` ### Response #### Success Response (BOOL) - **YES** (BOOL) - Indicates the payment initiation was successful. - **NO** (BOOL) - Indicates the payment initiation failed (e.g., invalid parameters). #### Response Example (The actual response is a boolean indicating initiation success. The detailed payment result is handled by the `wapcallback` block.) ```json // Example of callback result dictionary: { "resultStatus": "9000", "memo": "操作成功", "result": "..." } ``` ``` -------------------------------- ### Initiate WeChat Pay with Auth Code Source: https://context7.com/cpcn-ios/cpcnpaysdk/llms.txt Invoke the WeChat client for payment using an authorization code. This method takes the authorization code and a completion block that indicates whether the payment was successful. Ensure the WeChat SDK is properly integrated and initialized. ```objective-c #import NSString *authCode = @"your_authorization_code_from_server"; // 调起微信支付 [[CPCNWeixinPay shared] wxPayWithAuthCode:authCode completion:^(BOOL success) { dispatch_async(dispatch_get_main_queue(), ^{ if (success) { NSLog(@"微信支付成功"); // 更新订单状态 [self updateOrderStatus:OrderStatusPaid]; // 显示支付成功页面 [self showPaymentSuccessView]; } else { NSLog(@"微信支付失败或用户取消"); // 显示支付失败提示 [self showPaymentFailedAlert]; } }); }]; ``` -------------------------------- ### WeChat Payment Call - wxPayWithAuthCode:completion: Source: https://context7.com/cpcn-ios/cpcnpaysdk/llms.txt Initiates a WeChat payment using an authorization code and provides a completion block for the payment result. ```APIDOC ## WeChat Payment Call - wxPayWithAuthCode:completion: ### Description Initiates a WeChat payment by invoking the WeChat client using an authorization code. This method accepts the authorization code and a completion block, which returns a boolean indicating whether the payment was successful. ### Method `wxPayWithAuthCode:completion:` ### Endpoint N/A (Method of CPCNWeixinPay singleton) ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A (Parameters are passed directly to the method) - **authCode** (NSString) - Required - The authorization code obtained from the server. - **completion** (^(BOOL success)) - Required - A block that is called upon completion of the payment process. `success` is YES if the payment was successful, NO otherwise. ### Request Example ```objective-c #import NSString *authCode = @"your_authorization_code_from_server"; // Initiate WeChat Pay [[CPCNWeixinPay shared] wxPayWithAuthCode:authCode completion:^(BOOL success) { dispatch_async(dispatch_get_main_queue(), ^{ if (success) { NSLog(@"WeChat payment successful"); // Update order status [self updateOrderStatus:OrderStatusPaid]; // Show payment success page [self showPaymentSuccessView]; } else { NSLog(@"WeChat payment failed or user cancelled"); // Show payment failed alert [self showPaymentFailedAlert]; } }); }]; ``` ### Response (The response is delivered via the `completion` block.) #### Success Response (BOOL) - **YES** (BOOL) - Indicates the WeChat payment was successful. - **NO** (BOOL) - Indicates the WeChat payment failed or was cancelled by the user. #### Response Example (The result is handled within the completion block.) ``` -------------------------------- ### CPCNAlipay Singleton Acquisition Source: https://context7.com/cpcn-ios/cpcnpaysdk/llms.txt Acquires the shared singleton instance of CPCNAlipay for Alipay payment functionalities. ```APIDOC ## CPCNAlipay Singleton Acquisition ### Description Acquires the shared singleton instance of CPCNAlipay, used for invoking Alipay payment-related functions. All Alipay payment operations should be performed through this singleton. ### Method `shared` ### Endpoint N/A (Singleton method) ### Request Example ```objective-c // Get the Alipay payment singleton CPCNAlipay *alipay = [CPCNAlipay shared]; ``` ### Response - **CPCNAlipay** (object) - The shared singleton instance of CPCNAlipay. ``` -------------------------------- ### CPCNWeixinPay Singleton Acquisition Source: https://context7.com/cpcn-ios/cpcnpaysdk/llms.txt Retrieves the shared singleton instance of CPCNWeixinPay for WeChat Pay functionalities. ```APIDOC ## CPCNWeixinPay Singleton Acquisition ### Description Acquires the shared singleton instance of CPCNWeixinPay, used for invoking WeChat Pay-related functions. All WeChat Pay operations should be performed through this singleton. ### Method `shared` ### Endpoint N/A (Singleton method) ### Request Example ```objective-c // Get the WeChat Pay singleton CPCNWeixinPay *weixinPay = [CPCNWeixinPay shared]; ``` ### Response - **CPCNWeixinPay** (object) - The shared singleton instance of CPCNWeixinPay. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.