ios paypal简单支付与多个对象

Posted

技术标签:

【中文标题】ios paypal简单支付与多个对象【英文标题】:ios paypal simple payment with multiple objects 【发布时间】:2013-01-27 21:00:45 【问题描述】:

更新代码 感谢 ragnesh,我可以支付多件商品,但没有折扣,我需要为每种产品提供 10% 的折扣,有人可以帮忙吗?

- (void)simplePayment 
    [PayPal getPayPalInst].shippingEnabled = TRUE;

    [PayPal getPayPalInst].dynamicAmountUpdateEnabled = TRUE;

    [PayPal getPayPalInst].feePayer = FEEPAYER_EACHRECEIVER;

    PayPalPayment *payment = [[PayPalPayment alloc] init];

    payment.recipient = @"email@example.com";
    payment.paymentCurrency = @"USD";

    payment.invoiceData = [[PayPalInvoiceData alloc] init];

    payment.invoiceData.totalShipping = [NSDecimalNumber decimalNumberWithString:@"0"];

    payment.invoiceData.totalTax = [NSDecimalNumber decimalNumberWithString:@"0.00"];

    payment.invoiceData.invoiceItems = [NSMutableArray array];

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

    for (int i = 0; i < [db.array count]; i++) 
            myData =[db.array objectAtIndex:i];
        [dic setValue:myData.itemName forKey:[NSString stringWithFormat:@"title%d",i]];
        [dic setValue:[NSString stringWithFormat:@"%d",myData.itemPrice] forKey:[NSString stringWithFormat:@"price%d",i]];
        [dic setValue:[NSString stringWithFormat:@"%d",myData.itemQuantity] forKey:[NSString stringWithFormat:@"quantity%d",i]];
    

    NSLog(@"%@",dic);


    for (int i=0; i<[db.array count]; i++) 
        PayPalInvoiceItem *item = [[PayPalInvoiceItem alloc] init];
        [item setItemCount:[NSDecimalNumber decimalNumberWithString:[dic objectForKey:[NSString stringWithFormat:@"quantity%d",i]]]];
        [item setItemPrice:[NSDecimalNumber decimalNumberWithString:[dic objectForKey:[NSString stringWithFormat:@"price%d",i]]]];
        item.name = [dic objectForKey:[NSString stringWithFormat:@"title%d",i]];

        [payment.invoiceData.invoiceItems addObject:item];


        NSDecimalNumber *paypalItemQuantity = [NSDecimalNumber decimalNumberWithString:[dic objectForKey:[NSString stringWithFormat:@"quantity%d",i]]];

        NSDecimalNumber *paypalItemPrice = [NSDecimalNumber decimalNumberWithString:[dic objectForKey:[NSString stringWithFormat:@"price%d",i]]];


        payment.subTotal =[NSDecimalNumber decimalNumberWithString: [NSString stringWithFormat:@"%f",[payment.subTotal floatValue]
                                                                 +[paypalItemPrice floatValue] *

                                                                 [paypalItemQuantity floatValue]

                                                                 ]];


        NSLog(@"%@",[dic objectForKey:[NSString stringWithFormat:@"price%d",i]]);
        NSLog(@"%@",[dic objectForKey:[NSString stringWithFormat:@"title%d",i]]);
        NSLog(@"%@",payment.subTotal);


    

    [[PayPal getPayPalInst] checkoutWithPayment:payment];

为了打折,我做了类似的事情:但它弹出错误

payment.subTotal =[NSDecimalNumber decimalNumberWithString: [NSString stringWithFormat:@"%f",([payment.subTotal floatValue]*

                                                                 [paypalItemQuantity floatValue])
                                                                 -([paypalItemPrice floatValue] *

                                                                 [paypalItemQuantity floatValue] *0.1)

                                                                 ]];

【问题讨论】:

具体是什么不起作用?是不是报错了? 出现的错误是为商品、价格、税金和运费指定的金额加起来不等于总金额 我已经把我更新的最新代码放到了循环之外。但还是有问题。 【参考方案1】:

关注payPal 集成

把这条线放在你的循环之外

payment.invoiceData.invoiceItems = [NSMutableArray array];

编辑

- (void)simplePayment

    //dismiss any native keyboards

    //optional, set shippingEnabled to TRUE if you want to display shipping
    //options to the user, default: TRUE
    [PayPal getPayPalInst].shippingEnabled = TRUE;

    //optional, set dynamicAmountUpdateEnabled to TRUE if you want to compute
    //shipping and tax based on the user's address choice, default: FALSE
    [PayPal getPayPalInst].dynamicAmountUpdateEnabled = TRUE;

    //optional, choose who pays the fee, default: FEEPAYER_EACHRECEIVER
    [PayPal getPayPalInst].feePayer = FEEPAYER_EACHRECEIVER;


    //for a payment with a single recipient, use a PayPalPayment object
    NSMutableDictionary *dic=[[NSUserDefaults standardUserDefaults]valueForKey:@"drinkDetailDic"];


    PayPalPayment *payment = [[PayPalPayment alloc] init];

    payment.recipient = @"email@example.com";
    payment.paymentCurrency = @"USD";

    payment.description = [dic objectForKey:@"title"];


    //invoiceData is a PayPalInvoiceData object which contains tax, shipping, and a list of PayPalInvoiceItem objects
    payment.invoiceData = [[PayPalInvoiceData alloc] init];

    //shippping charge
    payment.invoiceData.totalShipping = [NSDecimalNumber decimalNumberWithString:@"0"];

    payment.invoiceData.totalTax = [NSDecimalNumber decimalNumberWithString:@"0.00"];

    payment.invoiceData.invoiceItems = [NSMutableArray array];


    for (int i=0; i<2; i++)
    
        PayPalInvoiceItem *item = [[PayPalInvoiceItem alloc] init];
        item.totalPrice = payment.subTotal;
        [item setItemCount:[NSNumber numberWithInt:1]];
        [item setItemPrice:[NSDecimalNumber decimalNumberWithString:[dic objectForKey:@"price"]]];
        item.name = [dic objectForKey:@"title"];

        [payment.invoiceData.invoiceItems addObject:item];

        payment.subTotal =[NSDecimalNumber decimalNumberWithString: [NSString stringWithFormat:@"%f",[payment.subTotal floatValue]
                                                                     +[[dic objectForKey:@"price"] floatValue]]];

    

    [[PayPal getPayPalInst] checkoutWithPayment:payment];

注意

如果您要添加多个项目,则小计价格与所有项目价格的总和相同。

【讨论】:

你可以检查我的答案它的工作......你必须添加数组中的所有项目,并且 subTotal 可能与 allItemPrice 计数相同 我已经更新了我的代码,在你的帮助下它可以正常工作,我也需要给它 10% 的折扣,我尝试了很多东西,但没有工作你也可以帮忙。谢谢 兄弟现在让你的逻辑和迭代:) 我尝试了很多不同的方法,但随后出现的错误是为商品、价格、税金和运费指定的金额不等于总金额 对特定商品也适用 10% 的折扣 [item setItemPrice:[NSDecimalNumber decimalNumberWithString:[dic objectForKey:[NSString stringWithFormat:@"price%d",i]]]];

以上是关于ios paypal简单支付与多个对象的主要内容,如果未能解决你的问题,请参考以下文章

如何从 iOS 应用程序内的单个企业帐户向多个 paypal 或 venmo 或支票帐户付款

是否有多个支付提供商(paypal、ogone、...)用于 Web 应用程序的 php 模块?

多个卖家的 PayPal 支付链接加密

使用 PayPal REST API ASP.NET MVC 一次支付多个项目

在贝宝支付多个项目

PayPal iOS SDK授权支付