我已经实施了最新的PayPal API,但每当我尝试通过PayPal的沙箱运行付款时,付款就会停留在“处理”上。完全难倒。谢谢!
-(void)payWithPayPal {
NSString *shortDescription = [NSString stringWithFormat:@"%i %@", [Global sharedInstance].currentOrder.itemQuantity, [Global sharedInstance].currentOrder.boozeBrand];
NSDecimalNumber *paymentDecimal = [NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%.02f", [Global sharedInstance].currentOrder.itemPrice]];
NSString *sku = [NSString stringWithFormat:@"DBAR-%i", [Global sharedInstance].currentOrder.orderNumber];
NSString *name = [NSString stringWithFormat:@"%@", [Global sharedInstance].currentOrder.boozeBrand];
PayPalItem *item = [PayPalItem itemWithName:name
withQuantity:[Global sharedInstance].currentOrder.itemQuantity
withPrice:paymentDecimal
withCurrency:@"USD"
withSku:sku];
float priceFloat = [item.price floatValue];
float totalFloat = priceFloat * item.quantity;
NSDecimalNumber *total = [NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%.02f", totalFloat]];
PayPalPayment *payment = [[PayPalPayment alloc] init];
payment.amount = total;
payment.currencyCode = @"USD";
payment.shortDescription = shortDescription;
payment.items = nil; // if not including multiple items, then leave payment.items as nil
payment.paymentDetails = nil; // if not including payment details, then leave payment.paymentDetails as nil
if (!payment.processable) {NSLog(@"Payment not processable.");}
// Update payPalConfig re accepting credit cards.
PayPalPaymentViewController *paymentViewController = [[PayPalPaymentViewController alloc] initWithPayment:payment configuration:self.payPalConfiguration delegate:self];
[self presentViewController:paymentViewController animated:YES completion:nil];
}
#pragma mark - PayPalDelegate
-(void)payPalPaymentViewController:(PayPalPaymentViewController *)paymentViewController willCompletePayment:(PayPalPayment *)completedPayment completionBlock:(PayPalPaymentDelegateCompletionBlock)completionBlock {
NSLog(@"Payment processing.");
//Stuck on this forever - this is what I'm trying to get past
}
-(void)payPalPaymentViewController:(PayPalPaymentViewController *)paymentViewController didCompletePayment:(PayPalPayment *)completedPayment {
//Never gets here
}
答案 0 :(得分:1)
您的问题出现在willCompletePayment
的完成块中,您需要在完成处理后返回完成块
拨打didCompletePayment
来自Paypal代码文档
您的代码必须通过调用completionBlock来完成 completionBlock在处理完成后执行的块。
所以你的代码就像
- (void)payPalPaymentViewController:(nonnull PayPalPaymentViewController *)paymentViewController
willCompletePayment:(nonnull PayPalPayment *)completedPayment
completionBlock:(nonnull PayPalPaymentDelegateCompletionBlock)completionBlock {
//do all the code you want
completionBlock();
}
写完这个完成块后,它将转到didCompletePayment
,可以是这样的:
- (void)payPalPaymentViewController:(PayPalPaymentViewController *)paymentViewController didCompletePayment:(PayPalPayment *)completedPayment {
NSLog(@"PayPal Payment Success!");
self.resultText = [completedPayment description];
[self showSuccess];
[self dismissViewControllerAnimated:YES completion:nil];
}
另一个选项
通过检查Paypal示例代码,实现willCompletePayment
方法不是必须的,您可以跳过此方法直接写didCompletePayment
这样你的代码就像这样:
- (void)payPalPaymentViewController:(PayPalPaymentViewController *)paymentViewController didCompletePayment:(PayPalPayment *)completedPayment {
NSLog(@"PayPal Payment Success!");
self.resultText = [completedPayment description];
[self showSuccess];
[self sendCompletedPaymentToServer:completedPayment]; // Payment was processed successfully; send to your server for verification and fulfillment
[self dismissViewControllerAnimated:YES completion:nil];
}
来自Paypal的didCompletePayment
的代码文档
您的代码可以处理completedPayment,如果它还没有 在你的选择范围内 payPalPaymentViewController:willCompletePayment:completionBlock: 方法
通过使用该方法,您无需调用willCompletePayment
方法,也不会遇到您遇到的问题。