美文网首页
iOS内购人走的心酸路(下)

iOS内购人走的心酸路(下)

作者: 默默码字的我 | 来源:发表于2022-02-07 17:22 被阅读0次

    现在先了解哈内购的流程

    好的思路我们都了解了 现在我们开始动工

    我写的比较简单没有封装就是在那个地方用到了然后就直接调支付的方法

    这个就是在我们要用的时候调后台的接口然后后台给我们返回订单号

    -(void)buttonActionTequan{

        [SVProgressHUD showWithStatus:@"正在支付"];

        NSMutableDictionary *dictParameters = [[NSMutableDictionary alloc]init];

        [dictParameterssetObject:@""forKey:@""];

        NSString*payType =@"";

        if ([currentPayType isEqualToString:@"产品id"]) {

            payType =@"";

        }

        [dictParameterssetObject:payTypeforKey:@"type"];

        [HttpManagerpostWithAction:@""andParams:dictParameterssuccess:^(NSDictionary*dict,BOOLsuccess) {

            intcode = [[dictobjectForKey:@"code"]intValue];

            NSString *message = [NSString stringWithFormat:@"%@",[dict objectForKey:@"msg"]];

            if(code ==1) {

                NSDictionary*dictData = [dictobjectForKey:@"data"];

                currentOrderNo = [NSString stringWithFormat:@"%@",[dictData objectForKey:@"order_num"]];

                [[SKPaymentQueue defaultQueue] addTransactionObserver:self];

                           if([SKPaymentQueuecanMakePayments]){

                                // 这里发起支付

                               [selfrequestProductData:currentPayType];

                           }else{

                               NSLog(@"不允许程序内付费");

                           }

            }else{

                [SVProgressHUD dismissWithError:message];

     }

        }fail:^(NSError*error) {

            [SVProgressHUD dismissWithError:@"请求失败,请检查网络"];

        }];}

    //收到产品返回信息

    - (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{

        NSLog(@"--------------收到产品反馈消息---------------------");

        NSArray*product = response.products;

        NSLog(@"productID:%@", response.invalidProductIdentifiers);

        if(product.count==0){

            [SVProgressHUD dismissWithError:@"查询不到对应消息"];

            return;

        }

        [SVProgressHUD showWithStatus:@"支付中"];

        SKProduct*p =nil;

        for(SKProduct*proinproduct) {

            if([pro.productIdentifier isEqualToString: currentPayType]){

                p = pro;

            }

        }

        SKPayment *payment = [SKPayment paymentWithProduct:p];

        NSLog(@"发送购买请求");

        [[SKPaymentQueue defaultQueue] addPayment:payment];

    }

    //请求失败

    - (void)request:(SKRequest*)requestdidFailWithError:(NSError*)error{

        [SVProgressHUD dismissWithError:@"支付失败"];

    }

    - (void)requestDidFinish:(SKRequest*)request{

        //[SVProgressHUD dismiss];

    }

    //监听购买结果

    - (void)paymentQueue:(SKPaymentQueue*)queueupdatedTransactions:(NSArray*)transaction{

        for(SKPaymentTransaction*tranintransaction){

            switch(tran.transactionState) {

                case SKPaymentTransactionStatePurchased:{

                //购买完成向自己的服务器验证

                    [self verifyPurchaseWithPaymentTransaction:[NSNumber numberWithInt:tran.transactionState]];

                    [[SKPaymentQueue defaultQueue] finishTransaction:tran];

                }

                    break;

                case SKPaymentTransactionStatePurchasing:

                    [SVProgressHUD showWithStatus:@"支付中"];

                    break;

                case SKPaymentTransactionStateRestored:{

                    [[SKPaymentQueue defaultQueue] finishTransaction:tran];

                }

                    break;

                case SKPaymentTransactionStateFailed:{

                    [[SKPaymentQueue defaultQueue] finishTransaction:tran];

                    [SVProgressHUD dismissWithError:@"购买失败"];

                }

                    break;

                default:

                    break;

            }

        }

    }

    //交易结束

    - (void)completeTransaction:(SKPaymentTransaction*)transaction{

        NSLog(@"交易结束");

        [[SKPaymentQueue defaultQueue] finishTransaction:transaction];

    }

    -(void)verifyPurchaseWithPaymentTransaction:(NSNumber*)resultState{

        //从沙盒中获取交易凭证并且拼接成请求体数据

        NSURL *receiptUrl=[[NSBundle mainBundle] appStoreReceiptURL];

        NSData*receiptData=[NSDatadataWithContentsOfURL:receiptUrl];

        NSString * base64String = [receiptData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];

        NSLog(@"receiptData = %@",base64String);    NSMutableDictionary *dictParameters = [[NSMutableDictionary alloc]init];

        [dictParameterssetObject:currentOrderNoforKey:@"order_no"];

        [dictParameterssetObject:base64StringforKey:@"trade_no"];

    }

    相关文章

      网友评论

          本文标题:iOS内购人走的心酸路(下)

          本文链接:https://www.haomeiwen.com/subject/iibmkrtx.html