/// <summary> /// 查询支付状态 /// </summary> /// <param name="postData"></param> /// <returns></returns> public object Query([FromBody] NullableSortedDict <string, object> postData) { try { using (var db = new MainDB()) { string outTradeNo = postData.GetValue <string>("outTradeNo"); string sign = postData.GetValue <string>("sign"); var tran = db.Transaction.FirstOrDefault(m => m.OutTradeNo == outTradeNo); if (tran == null) { throw new Exception("无效的outTradeNo"); } var wallet = db.Wallet.FirstOrDefault(m => m.id == tran.WalletId); var signResult = Helper.Sign(postData, wallet.Secret); if (signResult != sign) { throw new Exception("签名校验失败"); } return(Helper.SignResult(new { outTradeNo = outTradeNo, payedAmount = tran.PayedAmount, status = (int)tran.Status, cyptoCoinTrans = Newtonsoft.Json.JsonConvert.SerializeObject((from m in db.CyptoCoinTran where m.TransactionId == tran.id select new { cyptoCoinTransId = m.CyptoCoinTransId, confirmations = m.Confirmations, payedAmount = m.PayedAmount, payTime = m.PayTime // 接收到款项的时间 }).ToArray()) }, wallet.Secret)); } } catch (Exception ex) { return(new { status = "error", errMsg = ex.Message }); } }
/// <summary> /// 发起btc支付交易 /// </summary> /// <returns></returns> public object Pay([FromBody] NullableSortedDict <string, object> postData) { using (var db = new MainDB()) using (Way.Lib.CLog log = new Way.Lib.CLog("Pay")) { try { double?amount = postData.GetValue <double?>("amount"); string account = postData.GetValue <string>("account"); string sign = postData.GetValue <string>("sign"); string outTradeNo = postData.GetValue <string>("outTradeNo"); string notifyUrl = postData.GetValue <string>("notifyUrl"); string currency = postData.GetValue <string>("currency"); if (amount <= 0) { throw new Exception("金额无效"); } var wallet = db.Wallet.FirstOrDefault(m => m.Account == account); if (wallet == null) { throw new Exception($"账户“{account}”不存在"); } log.LogJson(postData); currency = currency.ToUpper(); var signResult = Helper.Sign(postData, wallet.Secret); if (signResult != sign) { throw new Exception("签名校验失败"); } var newTran = new DBModels.Transaction { Amount = amount, NotifyUrl = notifyUrl, OutTradeNo = outTradeNo }; //根据币种创建ICyptoCoinClient var client = Activator.CreateInstance(typeof(ApiController).Assembly.GetType($"Cailutong.CyptoCoinGateway.CyptoCoinPlatform.Impls.{currency}.{currency}_Client")) as CyptoCoinPlatform.ICyptoCoinClient; if (client == null) { throw new Exception("不支持" + currency); } //填充交易里面的付款地址 transactionGetCyptoCoinAddress(db, account, currency, client, newTran); newTran.WalletId = wallet.id; db.Insert(newTran); return(Helper.SignResult(new { status = "success", outTradeNo = outTradeNo, targetAddress = newTran.CyptoCoinAddress }, wallet.Secret)); } catch (Exception ex) { log.Log(ex.ToString()); return(new { status = "error", errMsg = ex.Message }); } } }