Backend for the Teso project written in 2022
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
4.6 KiB

3 months ago
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<ActionResult> 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<string, object> user = new Dictionary<string, object>
{
{ "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<string, string>()
{
{ "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<string, string>()
{
{ "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();
}
}
}