public string postExecute(PaypalPayment data) { // paypal info loaded from database, set at controller creation var auth = getPaypalAuthToken(paypalUrl, paypalId, paypalSecret); ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; var client = new RestClient(paypalUrl + "/payments/payment/" + data.paymentID + "/execute"); var request = new RestRequest(Method.POST) { RequestFormat = DataFormat.Json }; request.AddHeader("Authorization", string.Format("bearer {0}", auth.access_token)); request.AddHeader("Accept", "application/json"); request.AddParameter("application/json", "{ \"payer_id\": \"" + data.payerID + "\"}", ParameterType.RequestBody); var result = client.Execute(request); if (result.StatusCode != HttpStatusCode.OK) { throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError) { ReasonPhrase = "Payment execute failed" }); } return(result.Content); }
public void validateNoPreviousPayment(Domain.WorkOrder wo, PaypalPayment pp) { if (wo.ppPayerID != null && wo.ppPayerID != pp.payerID) { var res = new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = new StringContent(string.Format("PaypalID already set to {0}, conflicts with {1}", pp.payerID, wo.ppPayerID)), ReasonPhrase = "PaypalID already set to a different ID" }; throw new HttpResponseException(res); } if (wo.ppPaymentID != null && wo.ppPaymentID != pp.paymentID) { var res = new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = new StringContent(string.Format("PaymentID already set to {0}, conflicts with {1}", pp.paymentID, wo.ppPaymentID)), ReasonPhrase = "PaymentID already set to a different ID" }; throw new HttpResponseException(res); } if (wo.ppPaymentToken != null && wo.ppPaymentToken != pp.paymentToken) { var res = new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = new StringContent(string.Format("PaymentToken already set to {0}, conflicts with {1}", pp.paymentToken, wo.ppPaymentToken)), ReasonPhrase = "PaymentToken already set to a different ID" }; throw new HttpResponseException(res); } }
public IHttpActionResult PaypalExecute(int orderID, [FromBody] PaypalPayment data) { validatePaypalData(data); Domain.WorkOrder order = null; order = serv.Get(orderID); if (order.EmployerID != employer.ID) { throwInvalidOrder(orderID); } validateNoPreviousPayment(order, data); if (order.ppState == null) { order.ppPayerID = data.payerID; order.ppPaymentID = data.paymentID; order.ppPaymentToken = data.paymentToken; order.ppState = "created"; woServ.Save(order, userEmail); } var result = postExecute(data); var payment = JsonConvert.DeserializeObject <PayPal.Api.Payment>(result); order.ppResponse = result; order.ppState = payment.state; order.ppFee = Double.Parse(payment.transactions.Single().amount.total); woServ.Save(order, userEmail); return(Json(payment)); }
public void validatePaypalData(PaypalPayment pp) { if (pp.payerID == null || pp.paymentID == null || pp.paymentToken == null) { var res = new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = new StringContent(string.Format("Paypal data: {0}", JsonConvert.SerializeObject(pp))), ReasonPhrase = "Incomplete Paypal data" }; throw new HttpResponseException(res); } }