public override ApiResult FetchPaymentStatus(OrderReadOnly order, PayPalCheckoutOneTimeSettings settings) { try { if (order.Properties.ContainsKey("PayPalOrderId")) { var payPalOrderId = order.Properties["PayPalOrderId"].Value; var clientConfig = GetPayPalClientConfig(settings); var client = new PayPalClient(clientConfig); var payPalOrder = client.GetOrder(payPalOrderId); var paymentStatus = GetPaymentStatus(payPalOrder, out PayPalPayment payPalPayment); return(new ApiResult() { TransactionInfo = new TransactionInfoUpdate() { TransactionId = payPalPayment.Id, PaymentStatus = paymentStatus } }); } } catch (Exception ex) { Vendr.Log.Error <PayPalCheckoutOneTimePaymentProvider>(ex, "PayPal - FetchPaymentStatus"); } return(ApiResult.Empty); }
public override CallbackResult ProcessCallback(OrderReadOnly order, HttpRequestBase request, PayPalCheckoutOneTimeSettings settings) { try { var clientConfig = GetPayPalClientConfig(settings); var client = new PayPalClient(clientConfig); var payPalWebhookEvent = GetPayPalWebhookEvent(client, request); if (payPalWebhookEvent != null && payPalWebhookEvent.EventType.StartsWith("CHECKOUT.ORDER.APPROVED")) { var webhookPayPalOrder = payPalWebhookEvent.Resource.ToObject <PayPalOrder>(); // Fetch persisted order as it may have changed since the webhook // was initially sent (it could be a webhook resend) var persistedPayPalOrder = client.GetOrder(webhookPayPalOrder.Id); PayPalOrder payPalOrder; PayPalPayment payPalPayment; if (persistedPayPalOrder.Intent == PayPalOrder.Intents.AUTHORIZE) { // Authorize payPalOrder = persistedPayPalOrder.Status != PayPalOrder.Statuses.APPROVED ? persistedPayPalOrder : client.AuthorizeOrder(persistedPayPalOrder.Id); payPalPayment = payPalOrder.PurchaseUnits[0].Payments?.Authorizations?.FirstOrDefault(); } else { // Capture payPalOrder = persistedPayPalOrder.Status != PayPalOrder.Statuses.APPROVED ? persistedPayPalOrder : client.CaptureOrder(persistedPayPalOrder.Id); payPalPayment = payPalOrder.PurchaseUnits[0].Payments?.Captures?.FirstOrDefault(); } return(CallbackResult.Ok(new TransactionInfo { AmountAuthorized = decimal.Parse(payPalPayment?.Amount.Value ?? "0.00"), TransactionId = payPalPayment?.Id ?? "", PaymentStatus = GetPaymentStatus(payPalOrder) }, new Dictionary <string, string> { { "PayPalOrderId", payPalOrder.Id } })); } } catch (Exception ex) { Vendr.Log.Error <PayPalCheckoutOneTimePaymentProvider>(ex, "PayPal - ProcessCallback"); } return(CallbackResult.BadRequest()); }