using FirebaseAdmin.Messaging; using Google.Apis.Auth.OAuth2; using Google.Cloud.Firestore; using Grpc.Auth; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Teso_API.Models; namespace Teso_API.Controllers { [AllowAnonymous, Route("payment")] [ApiController] public class PaymentController : ControllerBase { private readonly TESOContext _context; private FirestoreDb db; public PaymentController(TESOContext context) { _context = context; db = new FirestoreDbBuilder { ProjectId = ServerLocation.credentials.project_id, ChannelCredentials = GoogleCredential.FromJson(JsonConvert.SerializeObject(ServerLocation.credentials)).ToChannelCredentials(), }.Build(); } [HttpGet("{filter}")] public async Task Get(string orderID) { CoinPurchase purchase = await _context.CoinPurchases.AsQueryable().Where(o => o.OrderId == orderID).FirstOrDefaultAsync(); if (purchase.Status == 1) { DocumentReference docRef = db.Collection(ServerLocation.user_notifications).Document(purchase.Customerid).Collection(purchase.Customerid).Document(); Dictionary user = new Dictionary { { "notificationType", "coinpurchase" }, { "timestamp", new DateTimeOffset(DateTimeOffset.UtcNow.DateTime).ToUnixTimeMilliseconds()}, { "message", "You have successfully purchased "+ purchase.Coins + " silver coins which have been credit to your account"}, { "coinID", orderID }, }; await docRef.SetAsync(user); string registrationToken = await _context.UserAuths.AsQueryable().Where(u => u.UserGUID == purchase.Customerid) .Select(t => t.DeviceToken).FirstOrDefaultAsync(); if (!String.IsNullOrEmpty(registrationToken)) { var message = new Message() { Token = registrationToken, Data = new Dictionary() { { "notificationType", "coinpurchase" }, { "timestamp", new DateTimeOffset(DateTimeOffset.UtcNow.DateTime).ToUnixTimeMilliseconds().ToString()}, { "message", "You have successfully purchased "+ purchase.Coins + " silver coins which have been credit to your account"}, { "orderID", orderID }, }, Notification = new Notification { Title = "Teso", Body = "You have successfully purchased " + purchase.Coins + " silver coins which have been credit to your account" }, }; var response = await FirebaseMessaging.DefaultInstance.SendAsync(message); } } else if (purchase.Status == 2 || purchase.Status == 3) { string registrationToken = await _context.UserAuths.AsQueryable().Where(u => u.UserGUID == purchase.Customerid) .Select(t => t.DeviceToken).FirstOrDefaultAsync(); if (!String.IsNullOrEmpty(registrationToken)) { var message = new Message() { Token = registrationToken, Data = new Dictionary() { { "notificationType", "coinpurchase" }, { "timestamp", new DateTimeOffset(DateTimeOffset.UtcNow.DateTime).ToUnixTimeMilliseconds().ToString()}, { "message", "Your request to purchase "+ purchase.Coins + " Silver Coins failed"}, { "orderID", orderID }, }, Notification = new Notification { Title = "Teso", Body = "Your request to purchase " + purchase.Coins + " failed" }, }; var response = await FirebaseMessaging.DefaultInstance.SendAsync(message); } } return Ok(); } } }