1

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

将多个项目添加到购物车。添加到购物车的每件商品可能不超过一个,也可能不超过一个。在简单的方法中,我能够创建一个 PayPalPayment 对象并将值分配给 PayPalPayment 的属性。

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 添加更多项目时,我收到了一些警报,例如“为项目价格、税金和运费指定的金额不等于总金额”。

谁能让我知道我在这里做错了什么。

提前感谢您的帮助。

4

1 回答 1

0

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

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];
}
于 2012-09-06T20:16:28.393 回答