Skip to content

Commit 1b64d86

Browse files
Finish
1 parent af5f78f commit 1b64d86

File tree

14 files changed

+263
-252
lines changed

14 files changed

+263
-252
lines changed

IngressosOnlineSolution/Domain/TIcket.PayMethod/MundiPaggClient.cs

+19-37
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,18 @@
1010
namespace Ticket.PayMethod
1111
{
1212
public class MundiPaggClient
13-
{
14-
public Action<PaymentMessage> OnCreated;
15-
public Action<PaymentMessage> OnError;
13+
{
1614
private GatewayServiceClient _serviceClient;
1715

1816
public MundiPaggClient()
1917
{
20-
var key = new Guid("5e62ba71-73d4-4ca0-8c03-26d1f78d6c71");
21-
const string link = "https://sandbox.mundipaggone.com";
18+
// var key = new Guid("5e62ba71-73d4-4ca0-8c03-26d1f78d6c71");
19+
// const string link = "https://sandbox.mundipaggone.com";
2220
// Creates the client that will send the transaction.
23-
_serviceClient = new GatewayServiceClient(key, new Uri(link));
21+
_serviceClient = new GatewayServiceClient();
2422
}
2523

26-
public void Pay(IOrder order, params ICreditCard[] cards)
24+
public IEnumerable<PaymentMessage> Pay(IOrder order, params ICreditCard[] cards)
2725
{
2826
var transictions = new Collection<CreditCardTransaction>(Generate(order, cards)
2927
.ToArray());
@@ -50,44 +48,28 @@ public void Pay(IOrder order, params ICreditCard[] cards)
5048
InstantBuyKey = creditCardTransaction.CreditCard.InstantBuyKey
5149

5250
});
51+
return feedBack.ToArray();
5352

54-
foreach (var msg in feedBack)
55-
{
56-
if (OnCreated != null)
57-
OnCreated(msg);
58-
}
59-
break;
60-
}
61-
case HttpStatusCode.InternalServerError:
62-
{
63-
InternalServerError();
64-
break;
6553
}
54+
6655
default:
6756
{
68-
Error(createSaleResponse, (int)httpResponse.HttpStatusCode, order);
69-
break;
57+
var payments = createSaleResponse.ErrorReport
58+
.ErrorItemCollection
59+
.Select(errorItem => new PaymentMessage()
60+
{
61+
Message = errorItem.Description,
62+
StatusCode = (int)httpResponse.HttpStatusCode,
63+
MessageCode = errorItem.ErrorCode,
64+
Email = order.UserEmail
65+
})
66+
.ToArray();
67+
return payments;
7068
}
7169
}
7270

7371
}
7472

75-
private void Error(CreateSaleResponse createSaleResponse, int statusCode, IOrder order)
76-
{
77-
if (OnError == null || createSaleResponse.ErrorReport == null) return;
78-
var payments =
79-
createSaleResponse.ErrorReport.ErrorItemCollection
80-
.Select(errorItem => new PaymentMessage()
81-
{
82-
Message = errorItem.Description,
83-
StatusCode = statusCode,
84-
MessageCode = errorItem.ErrorCode,
85-
Email = order.UserEmail
86-
}).ToArray();
87-
88-
foreach (var msg in payments)
89-
OnError(msg);
90-
}
9173

9274
private IEnumerable<CreditCardTransaction> Generate(IOrder order, ICreditCard[] cards)
9375
{
@@ -109,7 +91,7 @@ private IEnumerable<CreditCardTransaction> Generate(IOrder order, ICreditCard[]
10991
InstantBuyKey = card.InstantBuyKey.Value
11092
};
11193
yield return transaction;
112-
continue;
94+
continue;
11395
}
11496
transaction.CreditCard = card.Copiar<ICreditCard, CreditCard>();
11597
Contract.Assert(card.CreditCardBrand.HasValue);

IngressosOnlineSolution/Domain/Ticket.Core/PaymentMessageServer.cs

+6-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ internal class PaymentMessageServer : IObserver<PaymentMessage>
77
{
88
private SmtpClient _smtp;
99

10+
private const string DefaultFrom = "[email protected]";
11+
1012
private SmtpClient Smtp => _smtp ?? (_smtp = new SmtpClient("smtp.sendgrid.net"));
1113

1214
public void OnCompleted()
@@ -18,11 +20,12 @@ public void OnError(Exception erro)
1820
erro.LogAndThrow();
1921
}
2022

21-
public async void OnNext(PaymentMessage message)
23+
public void OnNext(PaymentMessage message)
2224
{
25+
2326
if (message.Email == null) return;
24-
var mail = new MailMessage("[email protected]", message.Email) { Body = message.ToString() };
25-
await Smtp.SendMailAsync(mail);
27+
var mail = new MailMessage(DefaultFrom, message.Email) { Body = message.ToString() };
28+
Smtp.Send(mail);
2629
}
2730
}
2831
}

IngressosOnlineSolution/Domain/Ticket.Core/RequestCore.cs

+20-17
Original file line numberDiff line numberDiff line change
@@ -95,27 +95,32 @@ private void Buy(CardOfCredit card, Request request, bool saveCard)
9595
card.Subscribe(MessageServer);
9696

9797
var gateway = new MundiPaggClient();
98-
gateway.OnCreated += (msg) =>
99-
{
100-
if (saveCard)
101-
{
102-
_userFromRequest.InstantBuyKey = msg.InstantBuyKey;
103-
_context.SaveChange();
104-
}
105-
card.Notificar(msg);
106-
};
107-
gateway.OnError += card.Notificar;
10898
var priceInCents = (long)request.Total * 100;
109-
gateway.Pay(new Order(priceInCents,
110-
request.Id.ToString(), _userFromRequest.Email), card);
99+
var order = new Order(priceInCents,
100+
request.Id.ToString(), _userFromRequest.Email);
101+
102+
var msg = gateway.Pay(order, card);
103+
foreach (var message in msg)
104+
{
105+
if (saveCard && message.StatusCode == 201)
106+
_userFromRequest.InstantBuyKey = message.InstantBuyKey;
107+
108+
request.StatusId = message.StatusCode;
109+
card.Notificar(message);
110+
}
111+
var resultSave = _context.SaveChange();
112+
Contract.Assert(resultSave > 0);
111113
}
112114

113115
/// <exception cref="InvalidOperationException">Ocorre quando não há itens no pedido</exception>
114116
/// <returns></returns>
115117
private Request SaveRequest(IBuyOnClick model)
116118
{
117-
if (model == null || model.Itens.IsNullOrEmpty())
118-
throw new InvalidOperationException("Request need ticket(s)");
119+
var noItens = (model == null || model.Itens.IsNullOrEmpty());
120+
Contract.EnsuresOnThrow<InvalidOperationException>(noItens, "Request need ticket(s)");
121+
122+
_userFromRequest = _context.UsersInfo.FindOrDefault(model.UserId);
123+
Contract.EnsuresOnThrow<InvalidOperationException>(_userFromRequest == null, "Not found user selected");
119124

120125
Contract.EndContractBlock();
121126

@@ -131,9 +136,6 @@ private Request SaveRequest(IBuyOnClick model)
131136

132137
try
133138
{
134-
_userFromRequest = _context.UsersInfo.FindOrDefault(model.UserId);
135-
if (_userFromRequest == null)
136-
throw new InvalidOperationException("Not found user selected");
137139
request.User = _userFromRequest;
138140
_context.Requests.Add(request);
139141
var result = _context.SaveChange();
@@ -145,5 +147,6 @@ private Request SaveRequest(IBuyOnClick model)
145147
throw;
146148
}
147149
}
150+
148151
}
149152
}

IngressosOnlineSolution/Domain/Ticket.Core/ViewModel/RequestView.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ public RequestView(Request request)
1717
Description = requestView.Any()
1818
? requestView.Aggregate((c, r) => $"{c} <br/> {r}")
1919
: "No itens for this request";
20-
21-
Date = _request.Date.ToString("dd/MM/yyy HH:mm");
20+
StatusId = request.StatusId;
21+
Date = _request.Date.ToString("dd/MM/yyy HH:mm:ss");
2222
}
2323

2424
/// <summary>Gets a date</summary>
@@ -29,6 +29,7 @@ public RequestView(Request request)
2929

3030
public string Number => _request.Id.ToString();
3131

32+
public int StatusId { get; private set; }
3233

3334
public decimal Total => _request.Total;
3435
}

IngressosOnlineSolution/Domain/Ticket/PaymentMessage.cs

+5-12
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,20 @@ public class PaymentMessage
1010

1111
/// <summary>Gets and sends decription for message</summary>
1212

13-
public string Message { get; set; }
14-
13+
public string Message { get; set; }
14+
1515
/// <summary>Gets and sends Message code</summary>
1616
public int MessageCode { get; set; }
1717

1818
/// <summary>Gets and sends email from user relatead</summary>
19-
public string Email { get; set; }
20-
19+
public string Email { get; set; }
20+
2121
public Guid InstantBuyKey { get; set; }
2222

2323
/// <summary>Returns a string that represents the current object.</summary>
2424
/// <returns>A string that represents the current object.</returns>
2525
/// <filterpriority>2</filterpriority>
26-
public override string ToString()
27-
{
28-
var text = new StringBuilder();
29-
if (StatusCode != 200)
30-
text.AppendLine($"ErroCode:{MessageCode} </br> ");
31-
text.AppendLine(Message);
26+
public override string ToString() => $"ErroCode:{MessageCode} <br /> {Message}";
3227

33-
return text.ToString();
34-
}
3528
}
3629
}

IngressosOnlineSolution/Domain/Ticket/Request.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ public class Request
1616

1717
public virtual UserInfo User { get; set; }
1818

19+
public int StatusId { get; set; } = 204;
20+
1921
/// <summary>Gets and sends total all itens</summary>
2022
public decimal Total => Itens.Sum(i => (i.PriceForRequest * i.NumberOfItens));
2123

@@ -38,6 +40,6 @@ public Request(params RequestItem[] itens)
3840
Itens = itens;
3941
}
4042

41-
43+
4244
}
4345
}

IngressosOnlineSolution/Test/Ticket.Core.UnitTest/App.config

+8-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@
33
<connectionStrings>
44
<add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;Initial Catalog=TicketsNow;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False" />
55
</connectionStrings>
6+
7+
<appSettings>
8+
<add key="GatewayService.MerchantKey" value="5e62ba71-73d4-4ca0-8c03-26d1f78d6c71" />
9+
<add key="GatewayService.HostUri" value="https://sandbox.mundipaggone.com" />
10+
</appSettings>
11+
612
<system.net>
713
<mailSettings>
8-
<smtp from="[email protected]">
9-
<network host="" password="MundiPagg123" userName="[email protected]" port="587" />
14+
<smtp from="[email protected]">
15+
<network host="" password="Mundi123Pagg" userName="[email protected]" port="587" />
1016
</smtp>
1117
</mailSettings>
1218
</system.net>

IngressosOnlineSolution/Views/Ticket.Views.HttpApi/Web.config

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<system.web>
2222
<compilation debug="true" targetFramework="4.6" />
2323
<httpRuntime targetFramework="4.6" />
24+
<globalization culture="pt-br" uiCulture="pt-br" enableClientBasedCulture="true" />
2425
</system.web>
2526
<system.webServer>
2627
<handlers>
@@ -80,8 +81,8 @@
8081
</runtime>
8182
<system.net>
8283
<mailSettings>
83-
<smtp from="[email protected]">
84-
<network host="" password="MundiPagg123" userName="[email protected]" port="587" />
84+
<smtp from="[email protected]">
85+
<network host="" password="Mundi123Pagg" userName="[email protected]" port="587" />
8586
</smtp>
8687
</mailSettings>
8788
</system.net>

IngressosOnlineSolution/Views/Ticket.Views.Website/MyRequest.html

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<th>
1818
{{rsx.label.total}}
1919
</th>
20-
20+
<th>Status</th>
2121
</tr>
2222
</thead>
2323
<tbody>
@@ -34,6 +34,9 @@
3434
<td>
3535
{{r.total}}
3636
</td>
37+
<td>
38+
{{r.statusId}}
39+
</td>
3740

3841
</tr>
3942
</tbody>

IngressosOnlineSolution/Views/Ticket.Views.Website/Scripts/oauth.js

+9
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,15 @@ var OAuth;
134134
else
135135
return alert("Internal error: " + error_1.message);
136136
}
137+
case 400:
138+
if (request.responseJSON) {
139+
if (request.responseJSON.message)
140+
return win.alert(request.responseJSON.message);
141+
else
142+
return win.alert(request.responseJSON);
143+
}
144+
return win.alert(request);
145+
break;
137146
default:
138147
var erros = request.responseJSON ? request.responseJSON.modelState : [""];
139148
if (!erros || !erros[""] || erros[""].length === 0)

IngressosOnlineSolution/Views/Ticket.Views.Website/Scripts/oauth.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,15 @@
172172
else
173173
return alert(`Internal error: ${error.message}`);
174174
}
175+
case 400:
176+
if (request.responseJSON) {
177+
if (request.responseJSON.message)
178+
return win.alert(request.responseJSON.message);
179+
else
180+
return win.alert(request.responseJSON);
181+
}
182+
return win.alert(request);
183+
break;
175184
default:
176185
const erros = request.responseJSON ? request.responseJSON.modelState : [""];
177186

@@ -210,7 +219,7 @@
210219
}
211220
}
212221

213-
222+
214223
export class AppStart {
215224
run(): void {
216225
const modulo = win.angular.module("oauth", ["ngRoute"]);

0 commit comments

Comments
 (0)