使用PayPal处理多个项目的购买



我正在将PayPal与我的应用程序集成。我能够用简单的付款方式处理单个项目的购买。我有点困惑,对于以下情况,我应该采用哪种方法:

将多个项目添加到购物车中。添加到购物车中的每个项目可能不超过一个。在简单的方法中,我能够创建一个PayPalPayment的对象,并为PayPalPayments的属性赋值。

PayPalPayment *payment = [[[PayPalPayment alloc] init] autorelease];
payment.recipient = @"s_biz@gmail.com";//@"example-merchant-1@paypal.com";
payment.paymentCurrency = @"USD";
payment.description = @"Gift Purchase";//@"Teddy Bear";
payment.merchantName = @"Test Merchant";//@"Joe's Bear Emporium";
payment.subTotal = [NSDecimalNumber decimalNumberWithString:@"100"];
payment.invoiceData = [[[PayPalInvoiceData alloc] init] autorelease];
payment.invoiceData.totalShipping = [NSDecimalNumber decimalNumberWithString:@"2"];
payment.invoiceData.totalTax = [NSDecimalNumber decimalNumberWithString:@"0.35"];
payment.invoiceData.invoiceItems = [NSMutableArray array];
PayPalInvoiceItem *item = [[[PayPalInvoiceItem alloc] init] autorelease];
item.totalPrice = payment.subTotal;    
item.name = @"Flower";
[payment.invoiceData.invoiceItems addObject:item];    
[[PayPal getPayPalInst] checkoutWithPayment:payment];

当我试图向payment.invoiceData.invoiceItems添加更多项目时,我收到了一些警告,比如"为项目价格、税款和运费指定的金额加起来不等于总金额"。

有人能告诉我我在这里做错了什么吗。

提前感谢您的帮助。

如果您遵循PayPal示例并将其扩展为具有多个行项目,请确保为添加到invoiceItems数组的每个项目分配一个新的PayPalInvoiceItem *。如果你不把分配放在循环中,你会覆盖每一个额外的行项目,发票总额也不会正确。你没有发布问题代码,但我敢打赌你只有一个

PayPalInvoiceItem *item = [[[PayPalInvoiceItem alloc] init] autorelease];

相反,为发票中的每个行项目分配并初始化一个额外的项目对象。

例如,假设您有一组LineItem对象要放入PayPal发票中。你的"支付"方法可能看起来像这样:

- (void)payViaPayPal {
    PayPal *payPal = [PayPal getPayPalInst];
    payPal.shippingEnabled = TRUE;
    PayPalPayment *payment = [[[PayPalPayment alloc] init] autorelease];
    payment.recipient = @"email@example.com";
    payment.paymentCurrency = @"USD";
    payment.description = @"Your order of example stuff";
    payment.merchantName = @"Company Name";
    //subtotal of all items, without tax and shipping
    payment.subTotal = [NSDecimalNumber zero];
    payment.invoiceData = [[[PayPalInvoiceData alloc] init] autorelease];
    // No shipping or sales tax in this example
    payment.invoiceData.totalShipping = [NSDecimalNumber zero];
    payment.invoiceData.totalTax = [NSDecimalNumber zero];
    payment.invoiceData.invoiceItems = [NSMutableArray array];
    for (LineItem *lineItem in self.lineItems) {
        PayPalInvoiceItem *item = [[[PayPalInvoiceItem alloc] init] autorelease];
        // If we want these items to show up on the PayPal invoice list, uncomment them.
        //
        // item.itemId = @"...";
        // item.itemCount = ...;
        // item.itemPrice = ...;
        item.name = lineItem.productTypeId.title;
        item.totalPrice = [lineItem.price
                           decimalNumberByMultiplyingBy:[
                                                         NSDecimalNumber
                                                         decimalNumberWithDecimal:[lineItem.quantity decimalValue]]];
        payment.subTotal = [payment.subTotal decimalNumberByAdding:item.totalPrice];
        [payment.invoiceData.invoiceItems addObject:item];
    }
    [payPal checkoutWithPayment:payment];
}

最新更新