欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS开发支付宝支付成功返回字符串的处理操作

程序员文章站 2023-12-20 20:37:34
{ memo=""; result="partner=\"311811\"&seller_id=\"nse@gmail.com\"&out_trad...

{
  memo="";
  result="partner=\"311811\"&seller_id=\"nse@gmail.com\"&out_trade_no=\"s005372\"&subject=\"\u522b\u5885\u8ba2\u5355\"&body=\"\u5885\"&total_fee=\"0.1\"&notify_url=\"http://baidu.com\"&service=\"mobile.secy.pay\"&payment_type=\"1\"&_input_charset=\"utf-8\"&it_b_pay=\"30m\"&success=\"true\"&sign_type=\"rsa\"&sign=\"dmiqkrzvnptnjp9zbwgnal3bu43rmocnwoasryfnuezx8uwy81zxhlg=\"";
  resultstatus=9000;
}

如上所示,为支付宝的返回数据结果。可能有的同学就懵了,这个result是一个字符串怎么把它分解成字典用那。

我封装了一个方法可以用

/**
 * 支付宝返回字段解析
 *
 * @param allstring      字段
 * @param firstseparatestring 第一个分离字段的词
 * @param secondseparatestring 第二个分离字段的词
 *
 * @return 返回字典
 */
+(nsdictionary *)vecomponentsstringtodic:(nsstring*)allstring withseparatestring:(nsstring *)firstseparatestring andseparatestring:(nsstring *)secondseparatestring;
+(nsmutabledictionary *)vecomponentsstringtodic:(nsstring*)allstring withseparatestring:(nsstring *)firstseparatestring andseparatestring:(nsstring*)secondseparatestring{
  nsmutabledictionary *dic=[nsmutabledictionary dictionary];
  nsarray *firstarr=[allstring componentsseparatedbystring:firstseparatestring];
  for (int i=0; i<firstarr.count; i++) {
    nsstring *firststr=firstarr[i];
    nsarray *secondarr=[firststr componentsseparatedbystring:secondseparatestring];
    [dic setobject:secondarr[1] forkey:secondarr[0]];
  }
  return dic;
}

在appdelegate用的时候代码如下

-(bool)application:(uiapplication *)application openurl:(nsurl *)url sourceapplication:(nsstring *)sourceapplication annotation:(id)annotation
{
    [[alipaysdk defaultservice] processorderwithpaymentresult:url standbycallback:^(nsdictionary *resultdic) {
      nsinteger orderstate=[resultdic[@"resultstatus"]integervalue];
      if (orderstate==9000) {
        nsstring *allstring=resultdic[@"result"];
        nsstring * firstseparatestring=@"\"&";
        nsstring * secondseparatestring=@"=\"";
        nsmutabledictionary *dic=[commontoolsvecomponentsstringtodic:allstring withseparatestring:firstseparatestring andseparatestring:secondseparatestring];
        nslog(@"ali=%@",dic);
        if ([dic[@"success"]isequaltostring:@"true"]) {
           [[nsnotificationcenter defaultcenter] postnotificationname:@"alipaysucceed" object:nil userinfo:dic];
        }
      }else{
        nsstring *returnstr;
        switch (orderstate) {
          case 8000:
            returnstr=@"订单正在处理中";
            break;
          case 4000:
            returnstr=@"订单支付失败";
            break;
          case 6001:
            returnstr=@"订单取消";
            break;
          case 6002:
            returnstr=@"网络连接出错";
            break;
          default:
            break;
        }
        [hudtooles showtexthud:returnstr];
        [hudtooles removehud:4];
      }
    }];
}

以上所述是小编给大家介绍的ios开发支付宝支付成功返回字符串的处理操作,希望对大家有所帮助

上一篇:

下一篇: