AlipayPlugin.m 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #import "AlipayPlugin.h"
  2. #import <AlipaySDK/AlipaySDK.h>
  3. @implementation AlipayPlugin
  4. -(void)pluginInitialize{
  5. CDVViewController *viewController = (CDVViewController *)self.viewController;
  6. self.partner = [viewController.settings objectForKey:@"partner"];
  7. }
  8. - (void) pay:(CDVInvokedUrlCommand*)command
  9. {
  10. self.currentCallbackId = command.callbackId;
  11. //partner和seller获取失败,提示
  12. if ([self.partner length] == 0)
  13. {
  14. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
  15. message:@"缺少partner。"
  16. delegate:self
  17. cancelButtonTitle:@"确定"
  18. otherButtonTitles:nil];
  19. [alert show];
  20. return;
  21. }
  22. //从API请求获取支付信息
  23. NSString *signedString = [command argumentAtIndex:0];
  24. if (signedString != nil) {
  25. [[AlipaySDK defaultService] payOrder:signedString fromScheme:[NSString stringWithFormat:@"a%@", self.partner] callback:^(NSDictionary *resultDic) {
  26. if ([[resultDic objectForKey:@"resultStatus"] isEqual: @"9000"]) {
  27. [self successWithCallbackID:self.currentCallbackId messageAsDictionary:resultDic];
  28. } else {
  29. [self failWithCallbackID:self.currentCallbackId messageAsDictionary:resultDic];
  30. }
  31. NSLog(@"reslut = %@",resultDic);
  32. }];
  33. }
  34. }
  35. - (void)handleOpenURL:(NSNotification *)notification
  36. {
  37. NSURL* url = [notification object];
  38. if ([url isKindOfClass:[NSURL class]] && [url.scheme isEqualToString:[NSString stringWithFormat:@"a%@", self.partner]])
  39. {
  40. [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
  41. if ([[resultDic objectForKey:@"resultStatus"] isEqual: @"9000"]) {
  42. [self successWithCallbackID:self.currentCallbackId messageAsDictionary:resultDic];
  43. } else {
  44. [self failWithCallbackID:self.currentCallbackId messageAsDictionary:resultDic];
  45. }
  46. }];
  47. }
  48. }
  49. - (void)successWithCallbackID:(NSString *)callbackID withMessage:(NSString *)message
  50. {
  51. CDVPluginResult *commandResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:message];
  52. [self.commandDelegate sendPluginResult:commandResult callbackId:callbackID];
  53. }
  54. - (void)failWithCallbackID:(NSString *)callbackID withMessage:(NSString *)message
  55. {
  56. CDVPluginResult *commandResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:message];
  57. [self.commandDelegate sendPluginResult:commandResult callbackId:callbackID];
  58. }
  59. - (void)successWithCallbackID:(NSString *)callbackID messageAsDictionary:(NSDictionary *)message
  60. {
  61. CDVPluginResult *commandResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:message];
  62. [self.commandDelegate sendPluginResult:commandResult callbackId:callbackID];
  63. }
  64. - (void)failWithCallbackID:(NSString *)callbackID messageAsDictionary:(NSDictionary *)message
  65. {
  66. CDVPluginResult *commandResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:message];
  67. [self.commandDelegate sendPluginResult:commandResult callbackId:callbackID];
  68. }
  69. @end