/// <summary> /// createCheckoutRequest is the actual implementation of the Register method /// This separation serves as test hook to validate the Uri /// against the code returned by the service /// </summary> /// <param name="credentials">PagSeguro credentials</param> /// <param name="payment">Payment request information</param> /// <returns>The Uri to where the user needs to be redirected to in order to complete the payment process</returns> public static Uri CreateCheckoutRequest(Credentials credentials, PaymentRequest payment) { PagSeguroTrace.Info(String.Format(CultureInfo.InvariantCulture, "PaymentService.Register({0}) - begin", payment)); try { using (HttpWebResponse response = HttpURLConnectionUtil.GetHttpPostConnection( PagSeguroConfiguration.CurrentConfig.PaymentUrl.AbsoluteUri, BuildCheckoutUrl(credentials, payment))) { if (HttpStatusCode.OK.Equals(response.StatusCode)) { using (XmlReader reader = XmlReader.Create(response.GetResponseStream())) { PaymentRequestResponse paymentResponse = new PaymentRequestResponse(PagSeguroConfiguration.CurrentConfig.PaymentRedirectUrl); PaymentSerializer.Read(reader, paymentResponse); PagSeguroTrace.Info(String.Format(CultureInfo.InvariantCulture, "PaymentService.Register({0}) - end {1}", payment, paymentResponse.PaymentRedirectUri)); return(paymentResponse.PaymentRedirectUri); } } else { PagSeguroServiceException pse = HttpURLConnectionUtil.CreatePagSeguroServiceException(response); PagSeguroTrace.Error(String.Format(CultureInfo.InvariantCulture, "PaymentService.Register({0}) - error {1}", payment, pse)); throw pse; } } } catch (WebException exception) { PagSeguroServiceException pse = HttpURLConnectionUtil.CreatePagSeguroServiceException((HttpWebResponse)exception.Response); PagSeguroTrace.Error(String.Format(CultureInfo.InvariantCulture, "PaymentService.Register({0}) - error {1}", payment, pse)); throw pse; } }
public PaymentRequestResponse CreatePaymentRequest(PaymentRequest paymentRequest) { HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(baseURL + EndPoint_PaymentRequest); httpReq.Headers = Headers; httpReq.ContentType = "application/json"; httpReq.Method = "POST"; String json = JsonConvert.SerializeObject(paymentRequest, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); using (var writer = new StreamWriter(httpReq.GetRequestStream())) { writer.Write(json); writer.Flush(); writer.Close(); } using (HttpWebResponse response = (HttpWebResponse)httpReq.GetResponse()) { using (StreamReader reader = new StreamReader(response.GetResponseStream())) { PaymentRequestResponse prr = JsonConvert.DeserializeObject <PaymentRequestResponse>(reader.ReadToEnd()); reader.Close(); response.Close(); return(prr); } } }
/// <summary> /// ChargePreApproval /// </summary> /// <param name="credentials">PagSeguro credentials</param> /// <param name="payment">PreApproval payment request information</param> /// <returns>The Uri to where the user needs to be redirected to in order to complete the payment process</returns> public static string ChargePreApproval(Credentials credentials, PaymentRequest payment) { PagSeguroTrace.Info(String.Format(CultureInfo.InvariantCulture, "PreApprovalService.ChargePreApproval({0}) - begin", payment)); try { using (var response = HttpURLConnectionUtil.GetHttpPostConnection( PagSeguroConfiguration.PreApprovalPaymentUri.AbsoluteUri, BuildChargeUrl(credentials, payment))) { if (HttpStatusCode.OK.Equals(response.StatusCode)) { using (XmlReader reader = XmlReader.Create(response.Content.ReadAsStreamAsync().Result)) { PaymentRequestResponse chargeResponse = new PaymentRequestResponse(PagSeguroConfiguration.PreApprovalPaymentUri); PaymentSerializer.Read(reader, chargeResponse); PagSeguroTrace.Info(String.Format(CultureInfo.InvariantCulture, "PreApprovalService.ChargePreApproval({0}) - end {1}", payment, chargeResponse.PaymentRedirectUri)); return(chargeResponse.TransactionCode); } } else { PagSeguroServiceException pse = HttpURLConnectionUtil.CreatePagSeguroServiceException(new System.Exception(response.StatusCode.ToString())); PagSeguroTrace.Error(String.Format(CultureInfo.InvariantCulture, "PreApprovalService.ChargePreApproval({0}) - error {1}", payment, pse)); throw pse; } } } catch (System.Exception exception) { PagSeguroServiceException pse = HttpURLConnectionUtil.CreatePagSeguroServiceException(exception); PagSeguroTrace.Error(String.Format(CultureInfo.InvariantCulture, "PreApprovalService.ChargePreApproval({0}) - error {1}", payment, pse)); throw pse; } }
public async Task <PaymentRequestResponse> PaymentRequestWithExtra(string description, string additionalData, string callbackUrl, string email = null, string mobile = null) { var k = await Client.Create().PaymentRequestWithExtraAsync(this._merchantId, this._amount, description, additionalData, email, mobile, callbackUrl); PaymentRequestResponse obj = new PaymentRequestResponse(); obj.Authority = k.Body.Authority; obj.Status = k.Body.Status; return(obj); //using (HttpClient httpClient = new HttpClient()) //{ // using (HttpResponseMessage httpResponseMessage = await httpClient.PostAsync("https://www.zarinpal.com/pg/rest/WebGate/PaymentRequestWithExtra.json", // new StringContent(JsonConvert.SerializeObject(new // { // MerchantID = this._merchantId, // Amount = this._amount, // Description = description, // AdditionalData = additionalData, // Email = email, // Mobile = mobile, // CallbackURL = callbackUrl // }), Encoding.UTF8, "application/json"))) // { // return JsonConvert.DeserializeObject<PaymentRequestResponse>(await httpResponseMessage.Content.ReadAsStringAsync()); // } //} }
/// <summary> /// ChargePreApproval /// </summary> /// <param name="credentials">PagSeguro credentials</param> /// <param name="payment">PreApproval payment request information</param> /// <returns>The Uri to where the user needs to be redirected to in order to complete the payment process</returns> public static string ChargePreApproval(Credentials credentials, PaymentRequest payment) { PagSeguroTrace.Info(string.Format(CultureInfo.InvariantCulture, "PreApprovalService.ChargePreApproval({0}) - begin", payment)); try { using (var response = HttpUrlConnectionUtil.GetHttpPostConnection( PagSeguroUris.GetPreApprovalPaymentUri(credentials).AbsoluteUri, BuildChargeUrl(credentials, payment))) { if (HttpStatusCode.OK.Equals(response.StatusCode)) { using (var reader = XmlReader.Create(response.GetResponseStream())) { var chargeResponse = new PaymentRequestResponse(PagSeguroUris.GetPreApprovalPaymentUri(credentials)); PaymentSerializer.Read(reader, chargeResponse); PagSeguroTrace.Info(string.Format(CultureInfo.InvariantCulture, "PreApprovalService.ChargePreApproval({0}) - end {1}", payment, chargeResponse.PaymentRedirectUri)); return(chargeResponse.TransactionCode); } } var pse = HttpUrlConnectionUtil.CreatePagSeguroServiceException(response); PagSeguroTrace.Error(string.Format(CultureInfo.InvariantCulture, "PreApprovalService.ChargePreApproval({0}) - error {1}", payment, pse)); throw pse; } } catch (WebException exception) { var pse = HttpUrlConnectionUtil.CreatePagSeguroServiceException((HttpWebResponse)exception.Response); PagSeguroTrace.Error(string.Format(CultureInfo.InvariantCulture, "PreApprovalService.ChargePreApproval({0}) - error {1}", payment, pse)); throw pse; } }
private static void CheckStatus(PaymentRequestResponse response) { var status = response.Body.Status; if (status != 100) { throw new Exception($"Something is wrong! Error code: {status}"); } }
internal static void Read(XmlReader reader, PaymentRequestResponse paymentResponse) { if (reader == null) { throw new ArgumentNullException("reader"); } if (paymentResponse == null) { throw new ArgumentNullException("paymentResponse"); } if (reader.IsEmptyElement) { SerializationHelper.SkipNode(reader); return; } string rootElement = reader.Name; reader.ReadStartElement(); reader.MoveToContent(); while (!reader.EOF) { if (SerializationHelper.IsEndElement(reader, rootElement)) { SerializationHelper.SkipNode(reader); break; } if (reader.NodeType == XmlNodeType.Element) { switch (reader.Name) { case PaymentRequestResponseSerializer.Date: paymentResponse.RegistrationDate = reader.ReadElementContentAsDateTime(); break; case PaymentRequestResponseSerializer.Code: paymentResponse.Code = reader.ReadElementContentAsString(); break; default: SerializationHelper.SkipElement(reader); break; } } else { SerializationHelper.SkipNode(reader); } } }
/// <summary> /// /// </summary> /// <param name="reader"></param> /// <param name="paymentResponse"></param> internal static void Read(XmlReader reader, PaymentRequestResponse paymentResponse) { if (reader.IsEmptyElement) { XMLParserUtils.SkipNode(reader); return; } string rootElement = reader.Name; reader.ReadStartElement(); reader.MoveToContent(); while (!reader.EOF) { if (XMLParserUtils.IsEndElement(reader, rootElement)) { XMLParserUtils.SkipNode(reader); break; } if (reader.NodeType == XmlNodeType.Element) { switch (reader.Name) { case PaymentSerializer.Date: paymentResponse.RegistrationDate = reader.ReadElementContentAsDateTime(); break; case PaymentSerializer.Code: paymentResponse.Code = reader.ReadElementContentAsString(); break; case PaymentSerializer.TransactionCode: paymentResponse.TransactionCode = reader.ReadElementContentAsString(); break; default: XMLParserUtils.SkipElement(reader); break; } } else { XMLParserUtils.SkipNode(reader); } } }
public int PaymentRequest(string MerchantID, int Amount, string Description, string Email, string Mobile, string CallbackURL, out string Authority) { PaymentRequestRequest inValue = new PaymentRequestRequest(); inValue.Body = new PaymentRequestRequestBody(); inValue.Body.MerchantID = MerchantID; inValue.Body.Amount = Amount; inValue.Body.Description = Description; inValue.Body.Email = Email; inValue.Body.Mobile = Mobile; inValue.Body.CallbackURL = CallbackURL; PaymentRequestResponse retVal = ((PaymentGatewayImplementationServicePortType)(this)).PaymentRequest(inValue); Authority = retVal.Body.Authority; return(retVal.Body.Status); }
public PaymentRequestResponse GetPaymentRequestStatus(String id) { HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(baseURL + EndPoint_PaymentRequest + id + "/"); httpReq.Headers = Headers; httpReq.Method = "GET"; using (HttpWebResponse response = (HttpWebResponse)httpReq.GetResponse()) { using (StreamReader reader = new StreamReader(response.GetResponseStream())) { PaymentRequestResponse jobj = JsonConvert.DeserializeObject <PaymentRequestResponse>(reader.ReadToEnd()); reader.Close(); response.Close(); return(jobj); } } }
internal static void Read(XmlReader reader, PaymentRequestResponse paymentResponse) { if (reader == null) throw new ArgumentNullException("reader"); if (paymentResponse == null) throw new ArgumentNullException("paymentResponse"); if (reader.IsEmptyElement) { SerializationHelper.SkipNode(reader); return; } string rootElement = reader.Name; reader.ReadStartElement(); reader.MoveToContent(); while (!reader.EOF) { if (SerializationHelper.IsEndElement(reader, rootElement)) { SerializationHelper.SkipNode(reader); break; } if (reader.NodeType == XmlNodeType.Element) { switch (reader.Name) { case PaymentRequestResponseSerializer.Date: paymentResponse.RegistrationDate = reader.ReadElementContentAsDateTime(); break; case PaymentRequestResponseSerializer.Code: paymentResponse.Code = reader.ReadElementContentAsString(); break; default: SerializationHelper.SkipElement(reader); break; } } else { SerializationHelper.SkipNode(reader); } } }