public string GetExpressCheckoutDetails(string token, string payerId) { string paypalEmail = string.Empty, shippingAddress = string.Empty, errorMessage = string.Empty; NVPCodec encoder = new NVPCodec(); encoder["METHOD"] = "GetExpressCheckoutDetails"; encoder["TOKEN"] = token; string nvpRequest = encoder.ConvertToNvpString(); string nvpResponse = HttpNvpRequestHandler(nvpRequest); NVPCodec decoder = new NVPCodec(); decoder.ConvertToNameValueCollection(nvpResponse); string acknowledgementStatus = decoder["ACK"].ToLower(); if (!string.IsNullOrWhiteSpace(acknowledgementStatus) && (acknowledgementStatus == "success" || acknowledgementStatus == "successwithwarning")) { paypalEmail = decoder["email"]; //shippingAddress = $"CustomerName: {decoder["PAYMENTREQUEST_0_SHIPTONAME"]} AddressLine: {decoder["PAYMENTREQUEST_0_SHIPTOSTREET"]} AddressLine2: {decoder["PAYMENTREQUEST_0_SHIPTOSTREET2"]} City: {decoder["PAYMENTREQUEST_0_SHIPTOCITY"]} State: {decoder["PAYMENTREQUEST_0_SHIPTOSTATE"]} Zipcode: {decoder["PAYMENTREQUEST_0_SHIPTOZIP"]}"; } return(paypalEmail); }
protected override async Task <IPaymentRedirectResponse> PaymentRedirectChargeGenerator(ICharge charge) { try { NVPCodec encoder = new NVPCodec(); encoder["METHOD"] = "SetExpressCheckout"; encoder["RETURNURL"] = _settings.GetConfigSetting <string>(SettingKeys.PaymentGateway.Paypal.ReturnUrl); encoder["CANCELURL"] = _settings.GetConfigSetting <string>(SettingKeys.PaymentGateway.Paypal.CancelUrl); encoder["PAYMENTREQUEST_0_AMT"] = charge.Amount.ToString(); encoder["PAYMENTREQUEST_0_PAYMENTACTION"] = "Sale"; encoder["PAYMENTREQUEST_0_CURRENCYCODE"] = "USD"; string nvpRequest = encoder.ConvertToNvpString(); string nvpResponse = HttpNvpRequestHandler(nvpRequest); NVPCodec decoder = new NVPCodec(); decoder.ConvertToNameValueCollection(nvpResponse); string acknowledgementStatus = decoder["ACK"].ToLower(); if (acknowledgementStatus != null && (acknowledgementStatus == "success" || acknowledgementStatus == "successwithwarning")) { string expressCheckoutUrl = "https://" + _settings.GetConfigSetting <string>(SettingKeys.PaymentGateway.Paypal.HostUrl) + "/cgi-bin/webscr?cmd=_express-checkout" + "&token=" + decoder["TOKEN"]; return(GetPaymentRedirectResponse(expressCheckoutUrl)); } else { return(GetPaymentRedirectResponse(null, decoder["L_SHORTMESSAGE0"].ToString())); } } catch (Exception ex) { _logger.Log(LogCategory.Error, new Exception("Failed to create redirect charge", ex)); return(GetPaymentRedirectResponse(null, ex.Message)); } }
private string SetApiCredentials() { NVPCodec codec = new NVPCodec(); if (!string.IsNullOrWhiteSpace(_settings.GetConfigSetting <string>(SettingKeys.PaymentGateway.Paypal.ApiUsername))) { codec["USER"] = _settings.GetConfigSetting <string>(SettingKeys.PaymentGateway.Paypal.ApiUsername); } if (!string.IsNullOrWhiteSpace(_settings.GetConfigSetting <string>(SettingKeys.PaymentGateway.Paypal.ApiPassword))) { codec[Pwd] = _settings.GetConfigSetting <string>(SettingKeys.PaymentGateway.Paypal.ApiPassword); } if (!string.IsNullOrWhiteSpace(_settings.GetConfigSetting <string>(SettingKeys.PaymentGateway.Paypal.ApiSignature))) { codec[Signature] = _settings.GetConfigSetting <string>(SettingKeys.PaymentGateway.Paypal.ApiSignature); } if (!string.IsNullOrWhiteSpace(Subject)) { codec["SUBJECT"] = Subject; } codec["VERSION"] = "65.0"; return(codec.ConvertToNvpString()); }
public IPaymentResponse PaypalResponseHandler(IGatewayCharge gatewayCharge) { NameValueCollection nvcResponseQueryString = HttpUtility.ParseQueryString(gatewayCharge.QueryString); var token = nvcResponseQueryString["TOKEN"]; var payerId = nvcResponseQueryString["PAYERID"]; try { if (token != null && payerId != null) { NVPCodec encoder = new NVPCodec(); NVPCodec decoder = new NVPCodec(); encoder["METHOD"] = "DoExpressCheckoutPayment"; encoder["TOKEN"] = token; encoder["PAYMENTREQUEST_0_PAYMENTACTION"] = "Sale"; encoder["PAYERID"] = payerId; encoder["PAYMENTREQUEST_0_AMT"] = gatewayCharge.Amount.ToString(); encoder["PAYMENTREQUEST_0_CURRENCYCODE"] = "USD"; string nvpRequest = encoder.ConvertToNvpString(); string nvpResponse = HttpNvpRequestHandler(nvpRequest); decoder.ConvertToNameValueCollection(nvpResponse); string acknowledgementStatus = decoder["ACK"].ToLower(); if (acknowledgementStatus != null && (acknowledgementStatus == "success" || acknowledgementStatus == "successwithwarning")) { string paypalEmail = GetExpressCheckoutDetails(token, payerId); var paymentConfirmationNumber = string.Empty; if (decoder["PAYMENTINFO_0_TRANSACTIONID"] != null) { paymentConfirmationNumber = decoder["PAYMENTINFO_0_TRANSACTIONID"].ToLower(); } if (!string.IsNullOrWhiteSpace(paymentConfirmationNumber)) { if (!string.IsNullOrWhiteSpace(paypalEmail)) { paymentConfirmationNumber = paypalEmail + "~" + paymentConfirmationNumber; } } else { paymentConfirmationNumber = paypalEmail; } return(GetPaymentResponse(true, PaymentGatewayError.None)); } else { return(GetPaymentResponse(false, GetPaymentGatewayErrorCode(decoder["L_ERRORCODE0"].ToString()))); } } else { return(token != null?GetPaymentResponse(false, PaymentGatewayError.InvalidToken) : GetPaymentResponse(false, PaymentGatewayError.InvalidPayerId)); } } catch (Exception ex) { _logger.Log(LogCategory.Error, new Exception("Failed to handle response", ex)); return(GetPaymentResponse(false, GetPaymentGatewayErrorCode(ex.Message))); } }