获取价格和不同产品的总和并发送到Paypal idk iOS

时间:2017-02-02 05:23:04

标签: ios paypal

我正在开发iOS中的PayPal SDK。当我将单个产品的总量及其总数量传递给payPal SDK时,它可以完美地运行。但是,如果我在购物车中有超过1个产品,并且每个产品的数量和数量不同,如何将总金额传递给PayPal。帮助我摆脱它。我正在计算并通过" EURO"货币格式。提前致谢。我的代码是: -

        - (IBAction)paypalButtonForSubmitData:(id)sender  
        {  
            NSDecimalNumber *subTotal;    
            NSArray *items;    
            self.resultText = nil;    
    // Here _arrayToCarryDataFromCartToPaymentPageInPaypal is array through which I am getting data from Plist.         
       for (int j=0; j<_arrayToCarryDataFromCartToPaymentPageInPaypal.count; j++)   
            {  
                NSMutableDictionary *paypalArrayDataInDictionary=[_arrayToCarryDataFromCartToPaymentPageInPaypal objectAtIndex:j];    
// priceDetail is giving me the price of any particular product.  
                NSString *priceDetail=[paypalArrayDataInDictionary objectForKey:@"PRICE"];      
                NSString *stringWithoutSpaces = [priceDetail stringByReplacingOccurrencesOfString:@"€" withString:@""];  
                NSLog(@"Price=%@",stringWithoutSpaces);  
                PayPalItem *item1=[PayPalItem itemWithName:@"IPHONE" withQuantity:[[paypalArrayDataInDictionary objectForKey:@"STOCK"]intValue] withPrice:[NSDecimalNumber decimalNumberWithString:stringWithoutSpaces] withCurrency:@"EUR" withSku:@"SKU-iPhone6"];    
                items= @[item1];    
                subTotal=[PayPalItem totalPriceForItems:items];    
                NSDecimalNumber *shipping=[[NSDecimalNumber alloc]initWithString:@"0,00"];   
                NSDecimalNumber *tax=[[NSDecimalNumber alloc]initWithString:@"0,00"];     

                PayPalPaymentDetails *paymentDetails=[PayPalPaymentDetails paymentDetailsWithSubtotal:subTotal withShipping:shipping withTax:tax];    
                NSDecimalNumber *total=[[subTotal decimalNumberByAdding:shipping]decimalNumberByAdding:tax];    

                PayPalPayment *payment=[[PayPalPayment alloc]init];    
                payment.amount=total;    
                payment.currencyCode=@"EUR";    
                payment.shortDescription=@"My Payment";    
                payment.items=items;    
                payment.intent = PayPalPaymentIntentSale;    
                payment.paymentDetails=paymentDetails;    
                NSLog(@"Payment=%@",payment);    

                if (!payment.processable)   {    
                    NSLog(@"Error");    
                }  
                // self.payPalConfig.acceptCreditCards=self.acceptCreditCards;    
                PayPalPaymentViewController *paymentViewController=[[PayPalPaymentViewController alloc]initWithPayment:payment configuration:self.payPalConfig delegate:self];    
                [self presentViewController:paymentViewController animated:true completion:nil];    
            }    
        }    

1 个答案:

答案 0 :(得分:0)

请写出代表PaypalViewController的for循环代码,

为此你需要声明下面的代码,

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

在开始循环之前。

请尝试以下编辑的代码

   - (IBAction)paypalButtonForSubmitData:(id)sender  
    {  
        NSDecimalNumber *subTotal;    
        NSArray *items;    
        self.resultText = nil;    
// Here _arrayToCarryDataFromCartToPaymentPageInPaypal is array through which I am getting data from Plist.         
   for (int j=0; j<_arrayToCarryDataFromCartToPaymentPageInPaypal.count; j++)   
        {  
            NSMutableDictionary *paypalArrayDataInDictionary=[_arrayToCarryDataFromCartToPaymentPageInPaypal objectAtIndex:j];    
           // priceDetail is giving me the price of any particular product.  
            NSString *priceDetail=[paypalArrayDataInDictionary objectForKey:@"PRICE"];      
            NSString *stringWithoutSpaces = [priceDetail stringByReplacingOccurrencesOfString:@"€" withString:@""];  
            NSLog(@"Price=%@",stringWithoutSpaces);  
            PayPalItem *item1=[PayPalItem itemWithName:@"IPHONE" withQuantity:[[paypalArrayDataInDictionary objectForKey:@"STOCK"]intValue] withPrice:[NSDecimalNumber decimalNumberWithString:stringWithoutSpaces] withCurrency:@"EUR" withSku:@"SKU-iPhone6"];    
            items= @[item1];    
        }
            subTotal=[PayPalItem totalPriceForItems:items];    
            NSDecimalNumber *shipping=[[NSDecimalNumber alloc]initWithString:@"0,00"];   
            NSDecimalNumber *tax=[[NSDecimalNumber alloc]initWithString:@"0,00"];     

            PayPalPaymentDetails *paymentDetails=[PayPalPaymentDetails paymentDetailsWithSubtotal:subTotal withShipping:shipping withTax:tax];    
            NSDecimalNumber *total=[[subTotal decimalNumberByAdding:shipping]decimalNumberByAdding:tax];    

            PayPalPayment *payment=[[PayPalPayment alloc]init];    
            payment.amount=total;    
            payment.currencyCode=@"EUR";    
            payment.shortDescription=@"My Payment";    
            payment.items=items;    
            payment.intent = PayPalPaymentIntentSale;    
            payment.paymentDetails=paymentDetails;    
            NSLog(@"Payment=%@",payment);    

            if (!payment.processable)   {    
                NSLog(@"Error");    
            }  
            // self.payPalConfig.acceptCreditCards=self.acceptCreditCards;    
            PayPalPaymentViewController *paymentViewController=[[PayPalPaymentViewController alloc]initWithPayment:payment configuration:self.payPalConfig delegate:self];    
            [self presentViewController:paymentViewController animated:true completion:nil];        
    }