/// <summary> /// Cancels the payment from the payment provider. This is often used when you need to call external services to handle the cancel process. /// </summary> /// <param name="payment">The payment.</param> /// <param name="status">The status.</param> /// <returns></returns> protected override bool CancelPaymentInternal(Payment payment, out string status) { var paymentMethod = payment.PaymentMethod; long accountNumber = Convert.ToInt64(paymentMethod.DynamicProperty <string>().AccountNumber.ToString()); var key = paymentMethod.DynamicProperty <string>().Key; int transactionNumber; if (!int.TryParse(payment.TransactionId, out transactionNumber)) { throw new FormatException(string.Format("Could not convert: {0} to an int.", payment.TransactionId)); } var preHash = accountNumber.ToString() + transactionNumber; var hash = Md5Computer.GetPreHash(preHash, key); var xml = GetPxOrderSoapClient(payment.PaymentMethod).Cancel2(accountNumber, transactionNumber, hash); var message = new PayExXmlMessage(xml); if (message.StatusCode && message.TransactionStatus == 4 && message.OriginalTransactionNumber.ToString() == payment.TransactionId) { payment.TransactionId = message.TransactionNumber.ToString(); payment.Save(); status = PaymentMessages.CancelSuccess; return(true); } status = PaymentMessages.CancelFailed + ": " + message.ErrorDescription; return(false); }
/// <summary> /// Acquires the payment from the payment provider. This is often used when you need to call external services to handle the acquire process. /// </summary> /// <param name="payment">The payment.</param> /// <param name="status">The status.</param> /// <returns></returns> protected override bool AcquirePaymentInternal(Payment payment, out string status) { var paymentMethod = payment.PaymentMethod; long accountNumber = Convert.ToInt64(paymentMethod.DynamicProperty <string>().AccountNumber.ToString()); var key = paymentMethod.DynamicProperty <string>().Key; int transactionNumber; if (!int.TryParse(payment.TransactionId, out transactionNumber)) { throw new Exception(string.Format("Could not convert: {0} to an int.", payment.TransactionId)); } var amount = Convert.ToInt32(payment.Amount * 100); var referenceId = payment.ReferenceId; var preHash = accountNumber.ToString() + transactionNumber + amount + referenceId; var hash = Md5Computer.GetPreHash(preHash, key); var xml = GetPxOrderSoapClient(payment.PaymentMethod).Capture3(accountNumber, transactionNumber, amount, referenceId, hash); var message = new PayExXmlMessage(xml); if (message.StatusCode && message.TransactionStatus == 6) { payment.TransactionId = message.TransactionNumber.ToString(); status = PaymentMessages.AcquireSuccess; return(true); } status = PaymentMessages.AcquireFailed + ": " + message.ErrorDescription; return(false); }
/// <summary> /// Refunds the payment from the payment provider. This is often used when you need to call external services to handle the refund process. /// </summary> /// <param name="payment">The payment.</param> /// <param name="status">The status.</param> /// <returns></returns> protected override bool RefundPaymentInternal(Payment payment, out string status) { var paymentMethod = payment.PaymentMethod; long accountNumber = Convert.ToInt64(paymentMethod.DynamicProperty <string>().AccountNumber.ToString()); string key = paymentMethod.DynamicProperty <string>().Key.ToString(); var amount = payment.Amount.ToCents(); var transactionRef = int.Parse(payment.TransactionId); var hashInput = accountNumber + "" + transactionRef + "" + amount + "" + payment.ReferenceId; var preHash = Md5Computer.GetPreHash(hashInput, key); var xml = GetPxOrderSoapClient(payment.PaymentMethod).Credit3(accountNumber, transactionRef, amount, payment.ReferenceId, preHash); var message = new PayExXmlMessage(xml); if (message.StatusCode && message.TransactionStatus == 2) { status = PaymentMessages.RefundSuccess; payment.TransactionId = message.TransactionNumber.ToString(); return(true); } status = PaymentMessages.RefundFailed + ": " + message.ErrorDescription; return(false); }
/// <summary> /// Processed the callback received from the payment provider. /// </summary> /// <param name="payment">The payment.</param> public override void ProcessCallback(Payment payment) { Guard.Against.PaymentNotPendingAuthorization(payment); Guard.Against.NoHttpContext(_webRuntimeInspector); Guard.Against.MissingParameterInResponse("orderRef"); var paymentMethod = payment.PaymentMethod; var acceptUrl = paymentMethod.DynamicProperty <string>().AcceptUrl; var cancelUrlForPaymentMethod = paymentMethod.DynamicProperty <string>().CancelUrl; long accountNumber = Convert.ToInt64(paymentMethod.DynamicProperty <string>().AccountNumber.ToString()); var key = paymentMethod.DynamicProperty <string>().Key; string orderReference = GetOrderReferenceFromRequest(); string hash = Md5Computer.GetPreHash(accountNumber + orderReference, key); PayExXmlMessage completeResponseMessage = CompletePayment(payment, accountNumber, orderReference, hash); Guard.Against.ResponseWasNotOk(completeResponseMessage, payment); Guard.Against.PaymentAlreadyCompleted(completeResponseMessage, payment); if (TransactionStatusNotAuthorized(completeResponseMessage)) { HandleDeclinedResponse(payment, cancelUrlForPaymentMethod); } else { HandleAuthorizedResponse(payment, acceptUrl, completeResponseMessage.TransactionNumber.ToString()); } }
public static void PaymentAlreadyCompleted(this Guard guard, PayExXmlMessage message, Payment payment) { if (message.AlreadyCompleted) { throw new InvalidOperationException(string.Format("The payment {0} has already been completed.", payment.PaymentId)); } }
public static void ResponseWasNotOk(this Guard guard, PayExXmlMessage message, Payment payment) { if (!message.StatusCode) { throw new ArgumentException(string.Format("Error when processing callback for payment {1}: {0}.", message.ErrorDescription, payment.PaymentId)); } }
private PayExXmlMessage CompletePayment(Payment payment, long accountNumber, string orderReference, string hash) { var xml = GetPxOrderSoapClient(payment.PaymentMethod).Complete(accountNumber, orderReference, hash); var message = new PayExXmlMessage(xml); return(message); }
public override Payment RequestPayment(PaymentRequest paymentRequest) { var paymentMethod = paymentRequest.PaymentMethod; string callBackUrl = paymentMethod.DynamicProperty <string>().CallbackUrl; string cancelUrlForPaymentMethod = paymentMethod.DynamicProperty <string>().CancelUrl; long accountNumber = Convert.ToInt64(paymentMethod.DynamicProperty <string>().AccountNumber.ToString()); string key = paymentMethod.DynamicProperty <string>().Key; if (paymentRequest.Payment == null) { paymentRequest.Payment = CreatePayment(paymentRequest); } var vatRate = Convert.ToInt32(paymentRequest.Payment.PurchaseOrder.OrderLines.First().VATRate * 100) * 100; var price = paymentRequest.Payment.Amount.ToCents(); const string purchaseOperation = "AUTHORIZATION"; var isoCode = paymentRequest.PurchaseOrder.BillingCurrency.ISOCode; var productNumber = paymentRequest.Payment.ReferenceId; var clientIpAddress = HttpContext.Current.Request.UserHostAddress; const string description = "Sum"; var returnUrl = _callbackUrl.GetCallbackUrl(callBackUrl, paymentRequest.Payment); const string creditcard = "CREDITCARD"; var clientLanguage = LocalizationContext.CurrentCultureCode; string cancelUrl = AbstractPageBuilder.GetAbsoluteUrl(cancelUrlForPaymentMethod); string value = accountNumber + purchaseOperation + price + "" + isoCode + vatRate + productNumber + productNumber + description + clientIpAddress + "" + "" + "" + returnUrl + creditcard + "" + cancelUrl + clientLanguage; var preHash = Md5Computer.GetPreHash(value, key); var pxOrderSoapClient = GetPxOrderSoapClient(paymentMethod); string xml = pxOrderSoapClient.Initialize7( accountNumber, purchaseOperation, price, "", isoCode, vatRate, productNumber, productNumber, description, clientIpAddress, "", "", "", returnUrl, creditcard, "", cancelUrl, clientLanguage, preHash ); var message = new PayExXmlMessage(xml); if (!message.StatusCode) { throw new Exception(string.Format("The webservice returned: {0}.", xml)); } // To avoid Thread being aborted exception allow the request to complete before redirecting. HttpContext.Current.Response.Redirect(message.RedirectUrl); return(paymentRequest.Payment); }
private bool TransactionStatusNotAuthorized(PayExXmlMessage message) { return(message.TransactionStatus != 3); }