protected string ProcessPaypalTransaction(string token, string payerId, decimal total) { var client = new PayPalAPIAAInterfaceClient(); var credentials = GetPaypalCredentials(); var request = new DoExpressCheckoutPaymentReq { DoExpressCheckoutPaymentRequest = new DoExpressCheckoutPaymentRequestType { Version = "89.0", DoExpressCheckoutPaymentRequestDetails = new DoExpressCheckoutPaymentRequestDetailsType { Token = token, PayerID = payerId, PaymentAction = PaymentActionCodeType.Sale, PaymentDetails = new PaymentDetailsType[] { new PaymentDetailsType { OrderTotal = new BasicAmountType { Value = total.ToString(), currencyID = CurrencyCodeType.USD } } } } } }; var response = client.DoExpressCheckoutPayment(ref credentials, request); if (response.Ack == AckCodeType.Failure) { throw new InvalidOperationException("Paypal returned the following error: " + response.Errors.FirstOrDefault().LongMessage); } if (response.DoExpressCheckoutPaymentResponseDetails.PaymentInfo.Count() == 0) { throw new InvalidOperationException("No payment transaction returned from paypal"); } if (string.IsNullOrWhiteSpace(response.DoExpressCheckoutPaymentResponseDetails.PaymentInfo.First().TransactionID)) { throw new InvalidOperationException("No payment transaction ID returned"); } return(response.DoExpressCheckoutPaymentResponseDetails.PaymentInfo.First().TransactionID); }
public ActionResult PaymentConfirmation(int?orderId) { if (!orderId.HasValue) { return(RedirectToAction("PayPalCancel")); } var response = Session["CheckoutDetails"] as GetExpressCheckoutDetailsResponseType; if (response == null) { return(RedirectToAction("PayPalCancel")); } DoExpressCheckoutPaymentReq payReq = PayPalHelper.GetDoExpressCheckoutPaymentReq(response); CustomSecurityHeaderType credentials = PayPalHelper.GetPayPalCredentials(); PayPalAPIAAInterfaceClient client = new PayPalAPIAAInterfaceClient(); DoExpressCheckoutPaymentResponseType doResponse = client.DoExpressCheckoutPayment(ref credentials, payReq); if (doResponse.Errors != null && doResponse.Errors.Length > 0) { throw new Exception("Exception occured when calling PayPal: " + doResponse.Errors[0].LongMessage); } PayPalHelper.UpdateOrderAfterConfirmation(orderId.Value); return(RedirectToAction("PayPalSuccess")); }
public PostProcessPaymentResponse PostProcessPayment(PostProcessPaymentRequest postProcessPaymentRequest) { var paymentResponse = new PostProcessPaymentResponse(); var req = new DoExpressCheckoutPaymentReq(); var request = new DoExpressCheckoutPaymentRequestType(); var payment = postProcessPaymentRequest.Payment; var order = postProcessPaymentRequest.Order; req.DoExpressCheckoutPaymentRequest = request; request.Version = Settings.Version; var details = new DoExpressCheckoutPaymentRequestDetailsType(); request.DoExpressCheckoutPaymentRequestDetails = details; details.PaymentAction = PaymentActionCodeType.Authorization; details.PaymentActionSpecified = true; details.Token = postProcessPaymentRequest.Token; details.PayerID = postProcessPaymentRequest.PayerId; var payer = GetPayerInfo(postProcessPaymentRequest.Token, postProcessPaymentRequest.PayerId); var paymentDetail = new PaymentDetailsType(); paymentDetail.OrderTotal = new BasicAmountType(); paymentDetail.OrderTotal.Value = order.GrandTotal.Raw.WithTax.ToString("N", new CultureInfo("en-us")); var currencyCode = (CurrencyCodeType)Utils.GetEnumValueByName(typeof(CurrencyCodeType), order.CurrencyCode); paymentDetail.OrderTotal.currencyID = currencyCode; paymentDetail.ButtonSource = ""; paymentDetail.PaymentAction = PaymentActionCodeType.Authorization; paymentDetail.PaymentActionSpecified = true; PaymentDetailsType[] paymentDetails = { paymentDetail }; details.PaymentDetails = paymentDetails; // System.Net.ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); var credentials = PaypalSecurityHeader(); DoExpressCheckoutPaymentResponseType response = _paypalService2.DoExpressCheckoutPayment(ref credentials, req); if (response.Ack == AckCodeType.Success) { payment.PaymentMethod = "ExpressCheckout"; payment.CvcResult = payer.AccountVerifyCode; payment.AvsResult = payer.AddressVerifyCode; payment.Secure3DResult = ""; payment.CardHolderName = payer.Email; payment.IssuerCountry = payer.CountryCode; payment.CardNo = payer.PayerId; payment.IsVerify = payer.IsVerify; payment.IsValidAddress = payer.IsValidAddress; payment.Info1 = ""; if (!string.IsNullOrEmpty(payer.Address1)) { payment.Info1 = payer.Address1 + ", "; } if (!string.IsNullOrEmpty(payer.Address2)) { payment.Info1 = payment.Info1 + payer.Address2 + ", "; } if (!string.IsNullOrEmpty(payer.City)) { payment.Info1 = payment.Info1 + payer.City + ", "; } if (!string.IsNullOrEmpty(payer.State)) { payment.Info1 = payment.Info1 + payer.State + ", "; } if (!string.IsNullOrEmpty(payer.PostCode)) { payment.Info1 = payment.Info1 + payer.PostCode + ", "; } if (!string.IsNullOrEmpty(payer.CountryCode)) { payment.Info1 = payment.Info1 + payer.CountryCode; } var billAddress = new AddressModel { FirstName = payer.FirstName, LastName = payer.LastName, Address1 = payer.Address1, Address2 = payer.Address2, City = payer.City, State = payer.State, PostCode = payer.PostCode, Country = payer.Country, CountryCode = payer.CountryCode, PhoneNo = payer.CountryCode }; order.BillingAddress = billAddress;//Mapper.Map<PayerInfo, Address>(payer); paymentResponse.Payment = payment; paymentResponse.Order = order; paymentResponse.Payment.AuthCode = response.DoExpressCheckoutPaymentResponseDetails.PaymentInfo[0].TransactionID; paymentResponse.Payment.Status = PaymentStatus.Authorized.GetHashCode(); } else { paymentResponse.Order = order; payment.Status = PaymentStatus.Declined.GetHashCode(); payment.IsValid = false; paymentResponse.Payment = payment; paymentResponse.AddError("decline"); foreach (var er in response.Errors) { paymentResponse.AddError(er.ErrorCode + " : " + er.ShortMessage); } } //if (Settings.IsImmediateCapture) //{ // var capturePaymentRequest = new CapturePaymentRequest(); // var pRes = new CapturePaymentResult(); // capturePaymentRequest.CurrencyCode = order.CurrencyCode; // capturePaymentRequest.CaptureTransactionId = Convert.ToString(paymentResponse.Payment.AuthCode); // capturePaymentRequest.OrderTotal = paymentResponse.Payment.OrderAmount; // pRes = Capture(capturePaymentRequest); // if (pRes.Success && string.IsNullOrEmpty(pRes.CaptureTransactionCode) == false) // { // paymentResponse.Payment.AuthCode = pRes.CaptureTransactionCode; // paymentResponse.Payment.Status = PaymentStatus.Paid.GetHashCode(); // paymentResponse.Payment.PaidAmount = capturePaymentRequest.OrderTotal; // paymentResponse.Order.PaymentStatus = PaymentStatus.Paid; // } // else // { // paymentResponse.Payment.PSPResponseMessage = pRes.CaptureTransactionResult; // } //} return(paymentResponse); }
public static OrderInfo Charge(decimal total, string PayPalToken, string payerId) { System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12; using (var client = new PayPalAPIAAInterfaceClient(new BasicHttpsBinding(), new EndpointAddress(EndpointUrl))) { var credentials = new CustomSecurityHeaderType() { Credentials = new UserIdPasswordType() { Username = APIUserName, Password = APIPassword, Signature = APISignature } }; DoExpressCheckoutPaymentReq req = new DoExpressCheckoutPaymentReq() { DoExpressCheckoutPaymentRequest = new DoExpressCheckoutPaymentRequestType() { Version = "121.0", DoExpressCheckoutPaymentRequestDetails = new DoExpressCheckoutPaymentRequestDetailsType() { PaymentAction = PaymentActionCodeType.Sale, PaymentActionSpecified = true, Token = PayPalToken, PayerID = payerId, PaymentDetails = new PaymentDetailsType[1] { new PaymentDetailsType() { OrderTotal = new BasicAmountType() { currencyID = CurrencyCodeType.USD, Value = total.ToString("F2") }, ShippingTotal = new BasicAmountType() { currencyID = CurrencyCodeType.USD, Value = "0.00" }, TaxTotal = new BasicAmountType() { currencyID = CurrencyCodeType.USD, Value = "0.00" }, ItemTotal = new BasicAmountType() { currencyID = CurrencyCodeType.USD, Value = total.ToString("F2") } } } } } }; DoExpressCheckoutPaymentResponseType resp = client.DoExpressCheckoutPayment(ref credentials, req); string errors = CheckErrors(resp); OrderInfo info = new OrderInfo(); if (errors == String.Empty) { info.Ack = resp.Ack.ToString(); info.TransactionID = resp.DoExpressCheckoutPaymentResponseDetails.PaymentInfo[0].TransactionID; info.ReceiptID = resp.DoExpressCheckoutPaymentResponseDetails.PaymentInfo[0].ReceiptID; } else { info.Ack = errors; } return(info); } }
protected void Page_Load(object sender, EventArgs e) { try { CustomSecurityHeaderType _credentials = new CustomSecurityHeaderType(); //{ // Credentials = new UserIdPasswordType() // { // Username = "******", // Password = "******", // Signature = "Ao--X15RhKPZUeA3L9bihv008SFUAc03629aPWDtEWfOvvTchBkLtV3A", // } //}; //GetExpressCheckoutDetailsRequestType request = new GetExpressCheckoutDetailsRequestType(); //request.Token = Request.QueryString["Token"].ToString(); //// Invoke the API //GetExpressCheckoutDetailsReq wrapper = new GetExpressCheckoutDetailsReq(); //wrapper.GetExpressCheckoutDetailsRequest = request; PayPalAPI.PayPalAPIAAInterfaceClient objPayPalAPI = new PayPalAPIAAInterfaceClient(); //var getExpRes = objPayPalAPI.GetExpressCheckoutDetails(ref _credentials,wrapper); //if(getExpRes.Ack==AckCodeType.Success && getExpRes.GetExpressCheckoutDetailsResponseDetails.PaymentInfo[0].PaymentStatus==PaymentStatusCodeType.Pending) //{ // //Show Error Message and save details in DB //} //else //{ Dictionary <string, string> PPPayment = new Dictionary <string, string>(); PPPayment = (Dictionary <string, string>)Session["PPPaymentDetails"]; var payReq = new DoExpressCheckoutPaymentReq { DoExpressCheckoutPaymentRequest = new DoExpressCheckoutPaymentRequestType { Version = "98", DoExpressCheckoutPaymentRequestDetails = new DoExpressCheckoutPaymentRequestDetailsType { Token = Request.QueryString["Token"].ToString(), PayerID = Request.QueryString["PayerId"].ToString(), PaymentAction = PaymentActionCodeType.Sale, PaymentActionSpecified = true, PaymentDetails = new[] { new PaymentDetailsType { OrderTotal = new BasicAmountType { currencyID = CurrencyCodeType.AUD, Value = PPPayment["Amount"].ToString(), } } } } } }; _credentials = new CustomSecurityHeaderType { Credentials = new UserIdPasswordType() { Username = System.Configuration.ConfigurationManager.AppSettings["UserName"].ToString(), Password = System.Configuration.ConfigurationManager.AppSettings["Password"].ToString(), Signature = System.Configuration.ConfigurationManager.AppSettings["Signature"].ToString(), } }; var DoExpRes = objPayPalAPI.DoExpressCheckoutPayment(ref _credentials, payReq); //Read the connection string from Web.Config file string ConnectionString = ConfigurationManager.ConnectionStrings["DefaultConnectionsql"].ConnectionString; using (SqlConnection con = new SqlConnection(ConnectionString)) { try { //Create the SqlCommand object SqlCommand cmd = new SqlCommand("sp_InsertPayPalPaymentDetails", con); //Specify that the SqlCommand is a stored procedure cmd.CommandType = System.Data.CommandType.StoredProcedure; //Add the input parameters to the command object cmd.Parameters.AddWithValue("@AccNum", PPPayment["AccNum"]); cmd.Parameters.AddWithValue("@Amount", PPPayment["Amount"]); cmd.Parameters.AddWithValue("@Token", Request.QueryString["Token"].ToString()); cmd.Parameters.AddWithValue("@PayerId", Request.QueryString["PayerId"].ToString()); cmd.Parameters.AddWithValue("@ExpressChkResp", DoExpRes.Ack); if (DoExpRes.Errors != null) { if (DoExpRes.Errors.Count() > 0) { cmd.Parameters.AddWithValue("@PPError", DoExpRes.Errors[0].ShortMessage); } } else { cmd.Parameters.AddWithValue("@PPError", ""); } if (DoExpRes.DoExpressCheckoutPaymentResponseDetails.BillingAgreementID != null) { cmd.Parameters.AddWithValue("@BillingAgreementId", DoExpRes.DoExpressCheckoutPaymentResponseDetails.BillingAgreementID); } else { cmd.Parameters.AddWithValue("@BillingAgreementId", ""); } if (DoExpRes.DoExpressCheckoutPaymentResponseDetails.MsgSubID != null) { cmd.Parameters.AddWithValue("@MsgSubId", DoExpRes.DoExpressCheckoutPaymentResponseDetails.MsgSubID); } else { cmd.Parameters.AddWithValue("@MsgSubId", ""); } if (DoExpRes.DoExpressCheckoutPaymentResponseDetails.Note != null) { cmd.Parameters.AddWithValue("@Note", DoExpRes.DoExpressCheckoutPaymentResponseDetails.Note); } else { cmd.Parameters.AddWithValue("@Note", ""); } if (DoExpRes.DoExpressCheckoutPaymentResponseDetails.PaymentInfo[0].GrossAmount != null) { cmd.Parameters.AddWithValue("@GrossAmount", DoExpRes.DoExpressCheckoutPaymentResponseDetails.PaymentInfo[0].GrossAmount.Value); } else { cmd.Parameters.AddWithValue("@GrossAmount", ""); } if (DoExpRes.DoExpressCheckoutPaymentResponseDetails.PaymentInfo[0].PaymentDate != null) { cmd.Parameters.AddWithValue("@PaymentDate", DoExpRes.DoExpressCheckoutPaymentResponseDetails.PaymentInfo[0].PaymentDate); } else { cmd.Parameters.AddWithValue("@PaymentDate", ""); } if (DoExpRes.DoExpressCheckoutPaymentResponseDetails.PaymentInfo[0].PaymentError != null) { cmd.Parameters.AddWithValue("@PaymentError", DoExpRes.DoExpressCheckoutPaymentResponseDetails.PaymentInfo[0].PaymentError.ShortMessage); } else { cmd.Parameters.AddWithValue("@PaymentError", ""); } if (DoExpRes.DoExpressCheckoutPaymentResponseDetails.PaymentInfo[0].PaymentRequestID != null) { cmd.Parameters.AddWithValue("@PaymentRequestID", DoExpRes.DoExpressCheckoutPaymentResponseDetails.PaymentInfo[0].PaymentRequestID); } else { cmd.Parameters.AddWithValue("@PaymentRequestID", ""); } if (DoExpRes.DoExpressCheckoutPaymentResponseDetails.PaymentInfo[0].PaymentStatus != null) { cmd.Parameters.AddWithValue("@PaymentStatus", DoExpRes.DoExpressCheckoutPaymentResponseDetails.PaymentInfo[0].PaymentStatus); } else { cmd.Parameters.AddWithValue("@PaymentStatus", ""); } if (DoExpRes.DoExpressCheckoutPaymentResponseDetails.PaymentInfo[0].PaymentType != null) { cmd.Parameters.AddWithValue("@PaymentType", DoExpRes.DoExpressCheckoutPaymentResponseDetails.PaymentInfo[0].PaymentType.ToString()); } else { cmd.Parameters.AddWithValue("@PaymentType", ""); } if (DoExpRes.DoExpressCheckoutPaymentResponseDetails.PaymentInfo[0].ReceiptID != null) { cmd.Parameters.AddWithValue("@ReceiptID", DoExpRes.DoExpressCheckoutPaymentResponseDetails.PaymentInfo[0].ReceiptID); } else { cmd.Parameters.AddWithValue("@ReceiptID", ""); } if (DoExpRes.DoExpressCheckoutPaymentResponseDetails.PaymentInfo[0].ReceiptReferenceNumber != null) { cmd.Parameters.AddWithValue("@ReceiptReferenceNumber", DoExpRes.DoExpressCheckoutPaymentResponseDetails.PaymentInfo[0].ReceiptReferenceNumber); } else { cmd.Parameters.AddWithValue("@ReceiptReferenceNumber", ""); } if (DoExpRes.DoExpressCheckoutPaymentResponseDetails.PaymentInfo[0].TransactionID != null) { cmd.Parameters.AddWithValue("@TransactionID", DoExpRes.DoExpressCheckoutPaymentResponseDetails.PaymentInfo[0].TransactionID); } else { cmd.Parameters.AddWithValue("@TransactionID", ""); } if (DoExpRes.DoExpressCheckoutPaymentResponseDetails.PaymentInfo[0].TransactionType != null) { cmd.Parameters.AddWithValue("@TransactionType", DoExpRes.DoExpressCheckoutPaymentResponseDetails.PaymentInfo[0].TransactionType.ToString()); } else { cmd.Parameters.AddWithValue("@TransactionType", ""); } //Open the connection and execute the query con.Open(); cmd.ExecuteScalar(); } catch (Exception ex) { StreamWriter file2 = File.AppendText(HttpContext.Current.Server.MapPath("~/Logs/file.txt")); file2.WriteLine("Saving Error : " + DateTime.Now + " " + ex.Message); file2.Close(); } } if (DoExpRes.Ack == AckCodeType.Success && DoExpRes.DoExpressCheckoutPaymentResponseDetails.PaymentInfo[0].PaymentStatus == PaymentStatusCodeType.Completed) { Label1.Text = "PayPal Payment Successful "; } } catch (Exception ex) { StreamWriter file2 = File.AppendText(HttpContext.Current.Server.MapPath("~/Logs/file.txt")); file2.WriteLine("Exception : " + DateTime.Now + " " + ex.Message); file2.Close(); Response.Redirect("Default.aspx?PP=Error"); } //} }