Hi,
I am trying to use the Paypal Direct Plugin to accept Payments via Credit/Debit Card.
I can see my API calls for both modes -'Authorize'and 'Authorize & Capture' are being sent successfully via payment.Create(...) method of Paypal .NET SDK. However, on next line the code expecting to get back an array of related_resources under payment.transactions[0] which is coming to be an empty array.
Can someone please let me know what might be missing here
Here is the code copied from Paypal Direct plugin:
public ProcessPaymentResult ProcessPayment(ProcessPaymentRequest processPaymentRequest)
{
var result = new ProcessPaymentResult();
var customer = _customerService.GetCustomerById(processPaymentRequest.CustomerId);
if (customer == null)
throw new Exception("Customer cannot be loaded");
try
{
var apiContext = PaypalHelper.GetApiContext(_paypalDirectPaymentSettings);
apiContext.Config.Add("connectionTimeout", "120000");
var currency = _currencyService.GetCurrencyById(_currencySettings.PrimaryStoreCurrencyId);
var payment = new Payment()
{
#region payer
payer = new Payer()
{
payment_method = "credit_card",
#region credit card info
funding_instruments = new List<FundingInstrument>
{
new FundingInstrument
{
credit_card = new CreditCard
{
type = processPaymentRequest.CreditCardType.ToLowerInvariant(),
number = processPaymentRequest.CreditCardNumber,
cvv2 = processPaymentRequest.CreditCardCvv2,
expire_month = processPaymentRequest.CreditCardExpireMonth,
expire_year = processPaymentRequest.CreditCardExpireYear
}
}
},
#endregion
#region payer info
payer_info = new PayerInfo
{
#region billing address
billing_address = customer.BillingAddress == null ? null : new Address
{
country_code = customer.BillingAddress.Country != null ? customer.BillingAddress.Country.TwoLetterIsoCode : null,
state = customer.BillingAddress.StateProvince != null ? customer.BillingAddress.StateProvince.Abbreviation : null,
city = customer.BillingAddress.City,
line1 = customer.BillingAddress.Address1,
line2 = customer.BillingAddress.Address2,
phone = customer.BillingAddress.PhoneNumber,
postal_code = customer.BillingAddress.ZipPostalCode
},
#endregion
email = customer.BillingAddress != null ? customer.BillingAddress.Email : null,
first_name = customer.BillingAddress != null ? customer.BillingAddress.FirstName : null,
last_name = customer.BillingAddress != null ? customer.BillingAddress.LastName : null
}
#endregion
},
#endregion
#region transaction
transactions = new List<Transaction>()
{
new Transaction
{
#region amount
amount = new Amount
{
total = processPaymentRequest.OrderTotal.ToString("N", new CultureInfo("en-US")),
currency = currency != null ? currency.CurrencyCode : null
},
#endregion
#region shipping address
item_list = customer.ShippingAddress == null ? null : new ItemList
{
shipping_address = new ShippingAddress
{
country_code = customer.ShippingAddress.Country != null ? customer.ShippingAddress.Country.TwoLetterIsoCode : null,
state = customer.ShippingAddress.StateProvince != null ? customer.ShippingAddress.StateProvince.Abbreviation : null,
city = customer.ShippingAddress.City,
line1 = customer.ShippingAddress.Address1,
line2 = customer.ShippingAddress.Address2,
phone = customer.ShippingAddress.PhoneNumber,
postal_code = customer.ShippingAddress.ZipPostalCode,
recipient_name = string.Format("{0} {1}", customer.ShippingAddress.FirstName, customer.ShippingAddress.LastName)
}
},
#endregion
invoice_number = processPaymentRequest.OrderGuid != Guid.Empty ? processPaymentRequest.OrderGuid.ToString() : null
}
},
#endregion
intent = _paypalDirectPaymentSettings.TransactMode == TransactMode.Authorize ? "authorize" : "sale",
}.Create(apiContext);
if (payment.transactions[0].related_resources.Any() && payment.transactions[0].related_resources[0] != null)
if (_paypalDirectPaymentSettings.TransactMode == TransactMode.Authorize)
{
var authorization = payment.transactions[0].related_resources[0].authorization;
if (authorization != null)
{
if (authorization.fmf_details != null)
{
result.AuthorizationTransactionResult = string.Format("Authorization is {0}. Based on fraud filter: {1}. {2}",
authorization.fmf_details.filter_type, authorization.fmf_details.name, authorization.fmf_details.description);
result.NewPaymentStatus = GetPaymentStatus(Authorization.Get(apiContext, authorization.id).state);
}
else
{
result.AuthorizationTransactionResult = authorization.state;
result.NewPaymentStatus = GetPaymentStatus(authorization.state);
}
result.AuthorizationTransactionId = authorization.id;
}
}
else
{
var sale = payment.transactions[0].related_resources[0].sale;
if (sale != null)
{
if (sale.fmf_details != null)
{
result.CaptureTransactionResult = string.Format("Sale is {0}. Based on fraud filter: {1}. {2}",
sale.fmf_details.filter_type, sale.fmf_details.name, sale.fmf_details.description);
result.NewPaymentStatus = GetPaymentStatus(Sale.Get(apiContext, sale.id).state);
}
else
{
result.CaptureTransactionResult = sale.state;
result.NewPaymentStatus = GetPaymentStatus(sale.state);
}
result.CaptureTransactionId = sale.id;
result.AvsResult = sale.processor_response != null ? sale.processor_response.avs_code : string.Empty;
}
}
else
result.AddError("PayPal error");
}
catch (PayPal.PayPalException exc)
{
if (exc is PayPal.ConnectionException)
{
var error = JsonFormatter.ConvertFromJson<Error>((exc as PayPal.ConnectionException).Response);
if (error != null)
{
result.AddError(string.Format("PayPal error: {0} ({1})", error.message, error.name));
if (error.details != null)
error.details.ForEach(x => result.AddError(string.Format("{0} {1}", x.field, x.issue)));
}
}
//if there are not the specific errors add exception message
if (result.Success)
result.AddError(exc.InnerException != null ? exc.InnerException.Message : exc.Message);
}
return result;
}