public SignatureType[] getSignatureType() { SignatureType HST = new SignatureType(); SignatureType[] MST = { HST }; IDType HIT = new IDType(); HIT.Value = MS.Emp.Ruc; PartyIdentificationType HPIT = new PartyIdentificationType(); PartyIdentificationType[] MPIT = { HPIT }; MPIT[0].ID = HIT; NameType1 HNT1 = new NameType1(); PartyNameType HPNT = new PartyNameType(); PartyNameType[] MPNT = { HPNT }; MPNT[0] = HPNT; HNT1.Value = $"<![CDATA[{MS.Emp.RazonSocial}]]>"; MPNT[0].Name = HNT1; ExternalReferenceType HERT = new ExternalReferenceType(); AttachmentType HAT_C = new AttachmentType(); URIType HURIT = new URIType(); HURIT.Value = ""; //HURIT.Value = $"#signature{MS.Emp.Ruc}"; HURIT.Value = DocumentName.Substring(12); HERT.URI = HURIT; HAT_C.ExternalReference = HERT; PartyType HPT = new PartyType(); HPT = null; HPT = new PartyType(); HPT.PartyIdentification = MPIT; HPT.PartyName = MPNT; HIT = null; HIT = new IDType(); HIT.Value = ""; //HIT.Value = $"IDSign{MS.Emp.Ruc}"; HIT.Value = DocumentName.Substring(12); MST[0].ID = HIT; MST[0].DigitalSignatureAttachment = HAT_C; MST[0].SignatoryParty = HPT; return(MST); }
public Message AddMessage(string apiKey, string senderUri, string recipientUri, string senderAccountId, double amount, string comments, string messageType, string securityPin, double latitude, double longitude, string recipientFirstName, string recipientLastName, string recipientImageUri) { User sender = null; URIType recipientUriType = GetURIType(recipientUri); URIType senderUriType = GetURIType(senderUri); if (recipientUriType == URIType.MobileNumber) { recipientUri = _formattingServices.RemoveFormattingFromMobileNumber(recipientUri); } if (senderUriType == URIType.MobileNumber) { senderUri = _formattingServices.RemoveFormattingFromMobileNumber(senderUri); } if (!(messageType.ToUpper() == "PAYMENT" || messageType.ToUpper() == "PAYMENTREQUEST")) { throw new ArgumentException(String.Format("Invalid Message Type. Message Type must be Payment or PaymentRequest")); } MessageType type = MessageType.Payment; if (messageType.ToUpper() == "PAYMENT") { type = MessageType.Payment; } if (messageType.ToUpper() == "PAYMENTREQUEST") { type = MessageType.PaymentRequest; } try { sender = _userServices.GetUser(senderUri); } catch (Exception ex) { _logger.Log(LogLevel.Debug, String.Format("Exception getting Sender {0}. {1}", senderUri, ex.Message)); throw new ArgumentException(String.Format("Sender {0} Not Found", senderUri)); } if (sender == null) { var message = String.Format("Unable to find sender {0}", senderUri); _logger.Log(LogLevel.Debug, message); throw new Exception(message); } if (!sender.SetupSecurityPin || !(sender.SecurityPin.Equals(_securityServices.Encrypt(securityPin)))) { var message = String.Format("Invalid Security Pin"); _logger.Log(LogLevel.Info, message); _logger.Log(LogLevel.Info, String.Format("{0} - {1}", sender.SecurityPin, _securityServices.Encrypt(securityPin))); throw new Exception(message); } if (recipientUriType == URIType.MobileNumber && _validationService.AreMobileNumbersEqual(sender.MobileNumber, recipientUri)) { var message = String.Format("Sender and Recipient are the same"); _logger.Log(LogLevel.Debug, message); throw new InvalidOperationException(message); } PaymentAccount senderAccount = null; try { senderAccount = GetAccount(sender, senderAccountId); } catch (Exception ex) { var message = String.Format("Exception getting Sender Account {0}. {1}", senderAccountId, ex.Message); _logger.Log(LogLevel.Debug, message); throw new Exception(message); } if (senderAccount == null) { var message = String.Format("The senderAccountId is invalid."); _logger.Log(LogLevel.Debug, message); throw new ArgumentException(message); } //TODO: validate application in request Application application = null; try { application = _applicationServices.GetApplication(apiKey);; } catch (Exception ex) { _logger.Log(LogLevel.Debug, String.Format("Exception getting application {0}. {1}", apiKey, ex.Message)); throw ex; } if (application == null) { var message = String.Format("Application {0} was not found.", apiKey); _logger.Log(LogLevel.Debug, String.Format("Exception getting application {0}. {1}", apiKey, message)); throw new InvalidOperationException(message); } Domain.User recipient = null; if (recipientUriType == URIType.MECode) { var meCode = _context.MECodes .FirstOrDefault(m => m.MeCode.Equals(recipientUri)); if (meCode == null) { throw new ArgumentException("MECode not found."); } recipient = meCode.User; } if (recipientUriType == URIType.EmailAddress) { recipient = _userServices.FindUserByEmailAddress(recipientUri); } if (recipientUriType == URIType.MobileNumber) { recipient = _userServices.FindUserByMobileNumber(recipientUri); } if (recipientUriType == URIType.FacebookAccount) { recipient = _userServices.GetUser(recipientUri); } //TODO: confirm recipient is valid??? //TODO: confirm amount is within payment limits //TODO: try to add message Domain.Message messageItem; _logger.Log(LogLevel.Info, "Adding Message"); if (sender == null || senderAccount == null) { _logger.Log(LogLevel.Info, "Sender or Sender Account Not Set"); throw new Exception("Sender or Sender Account Not Set"); } try { MessageStatus messageStatus = MessageStatus.Submitted; messageItem = _context.Messages.Add(new Message() { Amount = amount, Application = application, ApiKey = application.ApiKey, Comments = comments, CreateDate = System.DateTime.Now, Id = Guid.NewGuid(), MessageStatus = MessageStatus.Pending, MessageStatusValue = (int)messageStatus, MessageType = type, MessageTypeValue = (int)type, RecipientUri = recipientUri, SenderUri = senderUri, Sender = sender, SenderId = sender.UserId, SenderAccount = senderAccount, SenderAccountId = senderAccount.Id, Latitude = latitude, Longitude = longitude, recipientFirstName = (recipient != null && !String.IsNullOrEmpty(recipient.FirstName) ? recipient.FirstName : recipientFirstName), recipientLastName = (recipient != null && !String.IsNullOrEmpty(recipient.LastName) ? recipient.LastName : recipientLastName), recipientImageUri = recipientImageUri }); _context.SaveChanges(); } catch (Exception ex) { var message = String.Format("Exception adding message. {0}", ex.Message); throw new Exception(message); } return(messageItem); }
// GET /api/{userId}/PayStreamMessages public HttpResponseMessage <List <MessageModels.MessageResponse> > Get(string userId) { DomainServices.MessageServices messageServices = new DomainServices.MessageServices(_ctx); var user = GetUser(userId); if (user == null) { var message = new HttpResponseMessage <List <MessageModels.MessageResponse> >(HttpStatusCode.NotFound); message.ReasonPhrase = String.Format("User {0} specified in the request not found.", userId); return(message); } var messages = _ctx.Messages .Where(m => m.SenderId == user.UserId || m.RecipientId.Value == user.UserId) .OrderByDescending(m => m.CreateDate) .ToList <Message>(); List <MessageModels.MessageResponse> messageResponse = new List <MessageModels.MessageResponse>(); URIType senderUriType = URIType.MECode; URIType recipientUriType = URIType.MECode; string senderName = ""; string recipientName = ""; try { foreach (var message in messages) { senderUriType = URIType.MECode; recipientUriType = URIType.MECode; senderUriType = messageServices.GetURIType(message.SenderUri); recipientUriType = messageServices.GetURIType(message.RecipientUri); if (!String.IsNullOrEmpty(message.Sender.FirstName) || !String.IsNullOrEmpty(message.Sender.LastName)) { senderName = message.Sender.FirstName + " " + message.Sender.LastName; } else if (!String.IsNullOrEmpty(message.senderFirstName) || !String.IsNullOrEmpty(message.senderLastName)) { senderName = message.senderFirstName + " " + message.Sender.LastName; } else { senderName = (senderUriType == URIType.MobileNumber ? _formattingServices.FormatMobileNumber(message.SenderUri) : message.SenderUri); } if (message.Recipient != null && (!String.IsNullOrEmpty(message.Recipient.FirstName) || !String.IsNullOrEmpty(message.Recipient.LastName))) { recipientName = message.Recipient.FirstName + " " + message.Recipient.LastName; } else if (!String.IsNullOrEmpty(message.recipientFirstName) || !String.IsNullOrEmpty(message.recipientLastName)) { recipientName = message.recipientFirstName + " " + message.recipientLastName; } else { recipientName = (recipientUriType == URIType.MobileNumber ? _formattingServices.FormatMobileNumber(message.RecipientUri) : message.RecipientUri); } _logger.Log(LogLevel.Error, String.Format("URI Formats {0} {1}", senderUriType, recipientUriType)); String direction = "In"; if (message.SenderId.Equals(user.UserId)) { direction = "Out"; } messageResponse.Add(new MessageModels.MessageResponse() { amount = message.Amount, comments = message.Comments, createDate = message.CreateDate.ToString("ddd MMM dd HH:mm:ss zzz yyyy"), Id = message.Id, //lastUpdatedDate = m.LastUpdatedDate.ToString("ddd MMM dd HH:mm:ss zzz yyyy"), messageStatus = message.MessageStatus.ToString(), messageType = message.MessageType.ToString(), recipientUri = (recipientUriType == URIType.MobileNumber ? _formattingServices.FormatMobileNumber(message.RecipientUri) : message.RecipientUri), senderUri = (senderUriType == URIType.MobileNumber ? _formattingServices.FormatMobileNumber(message.SenderUri) : message.SenderUri), direction = direction, latitude = message.Latitude, longitutde = message.Longitude, senderName = senderName, senderImageUri = message.senderImageUri, recipientName = recipientName, recipientImageUri = message.recipientImageUri }); _logger.Log(LogLevel.Error, String.Format("Added MEssage {0} {1}", senderUriType, recipientUriType)); } } catch (Exception ex) { _logger.Log(LogLevel.Error, String.Format("Unhandled Exception formatting response. {0}", ex.Message)); } if (messageResponse == null) { var message = new HttpResponseMessage <List <MessageModels.MessageResponse> >(HttpStatusCode.InternalServerError); message.ReasonPhrase = String.Format("Unhandled exception get message for {0}", userId); return(message); } return(new HttpResponseMessage <List <MessageModels.MessageResponse> >(messageResponse, HttpStatusCode.OK)); }
public bool Process(Message message) { _formattingService = new FormattingServices(); _transactionBatchService = new TransactionBatchService(_ctx, _logger); _validationService = new ValidationService(_logger); _smsService = new SMSService(_ctx); _emailService = new EmailService(_ctx); _userService = new UserService(_ctx); _messageService = new MessageServices(_ctx); string fromAddress = "*****@*****.**"; URIType recipientType = _messageService.GetURIType(message.RecipientUri); _logger.Log(LogLevel.Info, String.Format("Processing Payment Message to {0}", message.RecipientUri)); _logger.Log(LogLevel.Info, String.Format("URI Type {0}", recipientType)); string smsMessage; string emailSubject; string emailBody; var sender = message.Sender; var recipient = _userService.GetUser(message.RecipientUri); message.Recipient = recipient; var senderName = _userService.GetSenderName(sender); var recipientName = message.RecipientUri; //check to see if recipient uri is mobile #, email address, or ME code //Validate Payment //Batch Transacations _logger.Log(LogLevel.Info, String.Format("Batching Transactions for message {0}", message.Id)); try { _transactionBatchService.BatchTransactions(message); } catch (Exception ex) { _logger.Log(LogLevel.Error, String.Format("Unable to process message {0}. {1}", message.Id, ex.Message)); throw ex; } //Attempt to assign payment to Payee if (recipient != null) { recipientName = recipient.UserName; if (!String.IsNullOrEmpty(recipient.SenderName)) { recipientName = recipient.SenderName; } else if (!String.IsNullOrEmpty(recipient.MobileNumber)) { recipientName = _formattingService.FormatMobileNumber(recipient.MobileNumber); } //Send out SMS Message to recipient if (!String.IsNullOrEmpty(recipient.MobileNumber)) { _logger.Log(LogLevel.Info, String.Format("Send SMS to Recipient")); smsMessage = String.Format(_recipientSMSMessage, message.Amount, senderName, _mobileWebSiteUrl); _smsService.SendSMS(message.ApiKey, recipient.MobileNumber, smsMessage); } //Send SMS Message to sender if (!String.IsNullOrEmpty(sender.MobileNumber)) { _logger.Log(LogLevel.Info, String.Format("Send SMS to Sender")); smsMessage = String.Format(_senderSMSMessage, message.Amount, recipientName, _mobileWebSiteUrl); _smsService.SendSMS(message.ApiKey, sender.MobileNumber, smsMessage); } //Send confirmation email to sender if (!String.IsNullOrEmpty(sender.EmailAddress)) { _logger.Log(LogLevel.Info, String.Format("Sending Email Confirmation to Sender")); emailSubject = String.Format(_senderConfirmationEmailSubject, recipientName); emailBody = String.Format(_senderConfirmationEmailBody, recipientName, message.Amount, _mobileWebSiteUrl); _emailService.SendEmail(message.ApiKey, fromAddress, sender.EmailAddress, emailSubject, emailBody); } //Send confirmation email to recipient if (!String.IsNullOrEmpty(recipient.EmailAddress)) { _logger.Log(LogLevel.Info, String.Format("Sending Email Confirmation to Recipient")); emailSubject = String.Format(_recipientConfirmationEmailSubject, senderName, message.Amount); //Payment Registered Recipient //first_name //last_name //rec_amount //rec_sender //rec_sender_photo_url //rec_datetime formatted dddd, MMMM dd(rd) at hh:mm tt //rec_comments //app_user //link_registration - empty _emailService.SendEmail(recipient.EmailAddress, emailSubject, _paymentReceivedRecipientRegisteredTemplate, new List <KeyValuePair <string, string> >() { new KeyValuePair <string, string>("first_name", recipient.FirstName), new KeyValuePair <string, string>("last_name", recipient.LastName), new KeyValuePair <string, string>("rec_amount", String.Format("{0:C}", message.Amount)), new KeyValuePair <string, string>("rec_sender", senderName), new KeyValuePair <string, string>("rec_sender_photo_url", ""), new KeyValuePair <string, string>("rec_datetime", message.CreateDate.ToString("dddd, MMMM dd at hh:mm tt")), new KeyValuePair <string, string>("rec_comments", message.Comments), new KeyValuePair <string, string>("link_registration", ""), new KeyValuePair <string, string>("app_user", "false") }); } if (recipient.DeviceToken.Length > 0) { _logger.Log(LogLevel.Info, String.Format("Sending iOS Push Notification to Recipient")); // We need to know the number of pending requests that the user must take action on for the application badge # // The badge number is the number of PaymentRequests in the Messages database with the Status of (1 - Pending) // If we are processing a payment, we simply add 1 to the number in this list. This will allow the user to // Be notified of money received, but it will not stick on the application until the users looks at it. Simplyt // Opening the application is sufficient var numPending = _ctx.Messages.Where(p => p.MessageTypeValue.Equals((int)Domain.MessageType.PaymentRequest) && p.MessageStatusValue.Equals((int)Domain.MessageStatus.Pending)); _logger.Log(LogLevel.Info, String.Format("iOS Push Notification Num Pending: {0}", numPending.Count())); NotificationPayload payload = null; String notification; // Send a mobile push notification if (message.MessageType == Domain.MessageType.Payment) { notification = String.Format(_recipientWasPaidNotification, senderName, message.Amount); payload = new NotificationPayload(recipient.DeviceToken, notification, numPending.Count() + 1); payload.AddCustom("nType", "recPCNF"); } else if (message.MessageType == Domain.MessageType.PaymentRequest) { notification = String.Format(_recipientRequestNotification, senderName, message.Amount); payload = new NotificationPayload(recipient.DeviceToken, notification, numPending.Count()); payload.AddCustom("nType", "recPRQ"); } /* * Payment Notification Types: * Payment Request [recPRQ] * - Recipient receives notification that takes them to the * paystream detail view about that payment request * Payment Confirmation [recPCNF] * - Recipient receices notification that takes them to the paysteam detail view about the payment request */ payload.AddCustom("tID", message.Id); var notificationList = new List <NotificationPayload>() { payload }; List <string> result; try { var push = new PushNotification(true, @"C:\APNS\DevKey\aps_developer_identity.p12", "KKreap1566"); result = push.SendToApple(notificationList); // You are done! } catch (Exception ex) { _logger.Log(LogLevel.Fatal, String.Format("Exception sending iOS push notification. {0}", ex.Message)); var exception = ex.InnerException; while (exception != null) { _logger.Log(LogLevel.Fatal, String.Format("Exception sending iOS push notification. {0}", exception.Message)); } } } if (recipient.FacebookUser != null) { //Send Facebook Message // I don't think we can do this through the server. Nice try though. // We should, however, publish something to the user's page that says sender sent payment } } else { _logger.Log(LogLevel.Info, String.Format("Send SMS to Payee not found")); var link = String.Format("{0}{1}", _mobileWebSiteUrl, message.Id.ToString()); //Send out SMS message to sender if (!String.IsNullOrEmpty(sender.MobileNumber)) { _logger.Log(LogLevel.Info, String.Format("Send SMS to Sender (Recipient is not an registered user).")); smsMessage = String.Format(_senderSMSMessageRecipientNotRegistered, message.Amount, message.RecipientUri, link); _smsService.SendSMS(message.ApiKey, sender.MobileNumber, smsMessage); } if (!String.IsNullOrEmpty(sender.EmailAddress)) { emailSubject = String.Format(_senderConfirmationEmailSubjectRecipientNotRegistered, message.RecipientUri); emailBody = String.Format(_senderConfirmationEmailBodyRecipientNotRegistered, message.Amount, message.RecipientUri); //Send confirmation email to sender _logger.Log(LogLevel.Info, String.Format("Send Email to Sender (Recipient is not an registered user).")); _emailService.SendEmail(message.ApiKey, fromAddress, sender.EmailAddress, emailSubject, emailBody); } if (recipientType == URIType.MobileNumber) { //Send out SMS message to recipient _logger.Log(LogLevel.Info, String.Format("Send SMS to Recipient (Recipient is not an registered user).")); smsMessage = String.Format(_recipientSMSMessageRecipientNotRegistered, senderName, message.Amount, link); _smsService.SendSMS(message.ApiKey, message.RecipientUri, smsMessage); } emailSubject = String.Format(_recipientConfirmationEmailSubject, senderName, message.Amount); //Payment Registered Recipient //first_name //last_name //rec_amount //rec_sender //rec_sender_photo_url //rec_datetime formatted DayOfWeek, MM dd(rd) at hh:mm:tt //rec_comments //app_user //link_registration - empty if (recipientType == URIType.EmailAddress) { //Send confirmation email to recipient _logger.Log(LogLevel.Info, String.Format("Send Email to Recipient (Recipient is not an registered user).")); _emailService.SendEmail(message.RecipientUri, emailSubject, _paymentReceivedRecipientNotRegisteredTemplate, new List <KeyValuePair <string, string> >() { new KeyValuePair <string, string>("first_name", ""), new KeyValuePair <string, string>("last_name", ""), new KeyValuePair <string, string>("rec_amount", String.Format("{0:C}", message.Amount)), new KeyValuePair <string, string>("rec_sender", senderName), new KeyValuePair <string, string>("rec_sender_photo_url", ""), new KeyValuePair <string, string>("rec_datetime", message.CreateDate.ToString("MM, dd yyyy hh:mm tt")), new KeyValuePair <string, string>("rec_comments", message.Comments), new KeyValuePair <string, string>("link_registration", link), new KeyValuePair <string, string>("app_user", "false") }); } } _logger.Log(LogLevel.Info, String.Format("Updating Payment")); message.MessageStatus = MessageStatus.Pending; message.LastUpdatedDate = System.DateTime.Now; _ctx.SaveChanges(); return(true); }
public bool Process(Message message) { _logger.Log(LogLevel.Info, String.Format("Processing Request Message to {0}", message.RecipientUri)); //Validate Payment Request //Batch Transacation WithDrawal URIType recipientType = _messageService.GetURIType(message.RecipientUri); _logger.Log(LogLevel.Info, String.Format("URI Type {0}", recipientType)); //Attempt to find the recipient of the request var sender = message.Sender; _logger.Log(LogLevel.Info, String.Format("Getting Recipient")); var recipient = _userService.GetUser(message.RecipientUri); message.Recipient = recipient; string smsMessage; string emailSubject; string emailBody; _logger.Log(LogLevel.Info, String.Format("Getting Sender")); var senderName = _userService.GetSenderName(sender); _logger.Log(LogLevel.Info, String.Format("Retrieved Sender Name {0}", senderName)); var recipientName = message.RecipientUri; if (recipient != null) { _logger.Log(LogLevel.Debug, String.Format("Recipient Found")); //if the recipient has a mobile #; send SMS if (!String.IsNullOrEmpty(recipient.MobileNumber)) { _logger.Log(LogLevel.Info, String.Format("Send SMS to Recipient")); smsMessage = String.Format(_recipientSMSMessage, message.Amount, senderName); _smsService.SendSMS(message.ApiKey, recipient.MobileNumber, smsMessage); } //if the recipient has an email address; send an email if (!String.IsNullOrEmpty(recipient.EmailAddress)) { _logger.Log(LogLevel.Info, String.Format("Send Email to Recipient")); emailSubject = String.Format(_recipientConfirmationEmailSubject, message.Amount, senderName); emailBody = String.Format(_recipientConfirmationEmailBody, message.Amount, senderName); _emailService.SendEmail(message.ApiKey, _fromAddress, recipient.EmailAddress, emailSubject, emailBody); } //if the recipient has a device token; send a push notification if (!String.IsNullOrEmpty(recipient.DeviceToken)) { _logger.Log(LogLevel.Info, String.Format("Send Push Notification to Recipient")); } //if the recipient has a linked facebook account; send a facebook message if (recipient.FacebookUser != null) { _logger.Log(LogLevel.Info, String.Format("Send Facebook Message to Recipient")); } } else { //if recipient Uri Type is Mobile Number, Send SMS if (recipientType == URIType.MobileNumber) { _logger.Log(LogLevel.Info, String.Format("Send SMS Message to Recipient")); smsMessage = String.Format(_recipientSMSMessage, message.Amount, senderName); _smsService.SendSMS(message.ApiKey, message.RecipientUri, smsMessage); } //if recipient Uri Type is email address, Send Email if (recipientType == URIType.EmailAddress) { _logger.Log(LogLevel.Info, String.Format("Send Emaili Message to Recipient")); emailSubject = String.Format(_recipientConfirmationEmailSubject, message.Amount, senderName); emailBody = String.Format(_recipientConfirmationEmailBody, message.Amount, senderName); _emailService.SendEmail(message.ApiKey, _fromAddress, message.RecipientUri, emailSubject, emailBody); } //if recipient Uri Type is facebook count, send a facebook message if (recipientType == URIType.FacebookAccount) { _logger.Log(LogLevel.Info, String.Format("Send Facebook Message to Recipient")); } } //if sender has mobile #, send confirmation email to sender if (!String.IsNullOrEmpty(sender.MobileNumber)) { _logger.Log(LogLevel.Info, String.Format("Send SMS to Recipient")); smsMessage = String.Format(_senderSMSMessage, message.Amount, recipientName); _smsService.SendSMS(message.ApiKey, sender.MobileNumber, smsMessage); } //if sender has email address; send an email if (!String.IsNullOrEmpty(sender.EmailAddress)) { _logger.Log(LogLevel.Info, String.Format("Send Email to Recipient")); emailSubject = String.Format(_senderConfirmationEmailSubject, recipientName); emailBody = String.Format(_senderConfirmationEmailBody, message.Amount, recipientName); _emailService.SendEmail(message.ApiKey, _fromAddress, sender.EmailAddress, emailSubject, emailBody); } //if sender has a device token; send a push notification if (!String.IsNullOrEmpty(sender.DeviceToken)) { _logger.Log(LogLevel.Info, String.Format("Send Push Notification to Recipient")); } //if sender has a linked facebook account; send a facebook message if (sender.FacebookUser != null) { _logger.Log(LogLevel.Info, String.Format("Send Facebook Message to Recipient")); } //Update Payment Status _logger.Log(LogLevel.Info, String.Format("Updating Payment Request")); message.MessageStatus = MessageStatus.Pending; message.LastUpdatedDate = System.DateTime.Now; _ctx.SaveChanges(); return(true); }