public static PayRecord Create(DataSource ds, long user, string openId, string title, string provider, Money money, int type = 0, string targetId = null, PaymentType payType = PaymentType.Pay) { DateTime now = DateTime.Now; PayRecord pr = new PayRecord() { Id = NewId(now, user), PayType = payType, PayId = string.Empty, UserId = user, OpenId = openId, Title = title, Provider = provider, Money = money, Type = type, TargetId = targetId, Status = PayStatus.Paying, CreationDate = now }; if (pr.Insert(ds) == DataStatus.Success) { return(pr); } return(null); }
protected virtual bool OnNotify(PayProvider provider, bool success, PaymentResult result) { if (result.Type == PaymentType.Pay) { PayResult presult = (PayResult)result; if (!string.IsNullOrEmpty(presult.TradeNo)) { DataSource.Begin(); try { M.PayRecord pr = M.PayRecord.GetById(DataSource, presult.TradeNo, presult.Type); if (pr == null) { throw new ArgumentException("订单号错误!"); } if (pr.Status >= PayStatus.Paying && pr.Status < PayStatus.PaySuccess) { PayStatus status = pr.Status; pr.PayId = presult.PayTradeNo; if (provider.IsOnlinePay) { pr.Money = presult.TotalFee; } if (success) { pr.Status = PayStatus.PaySuccess; } else { pr.Status = PayStatus.PayFailed; } if (pr.UpdateStatus(DataSource, status) == DataStatus.Success) { if (success && pr.Status == PayStatus.PaySuccess) { if (!OnModifyMoney(provider, pr.PayType, pr.UserId, presult.TotalFee, pr.Id, pr.Title, pr.Type, pr.TargetId)) { throw new ArgumentException("充值失败!"); } } } else { throw new ArgumentException("更细订单失败!"); } } else { if (pr.Status != PayStatus.PaySuccess) { throw new ArgumentException("订单号错误!"); } } DataSource.Commit(); return(true); } catch (Exception) { DataSource.Rollback(); //OnNotifyError(provider, ex.Message); } } //else //{ // OnNotifyError(provider, "订单号为空!"); //} } else { RefundResult rresult = (RefundResult)result; if (!string.IsNullOrEmpty(rresult.BatchNo)) { if (rresult.SuccessNum == 1 && rresult.Results.Count == 1) { RefundInfo info = rresult.Results[0]; DataSource.Begin(); try { M.PayRecord pr = M.PayRecord.GetById(DataSource, rresult.BatchNo, rresult.Type); if (pr == null) { throw new ArgumentException("批次号错误!"); } if (pr.PayId != info.PayTradeNo) { throw new ArgumentException("订单号错误!"); } if (pr.Status == PayStatus.RefundNotifying) { if (provider.IsOnlinePay) { pr.Money = info.TotalFee; } if (success && info.Status) { pr.Status = PayStatus.RefundSuccess; } else { pr.Status = PayStatus.RefundFailed; } if (pr.UpdateStatus(DataSource, PayStatus.RefundNotifying) == DataStatus.Success) { if (success && info.Status && pr.Status == PayStatus.RefundSuccess) { if (!OnModifyMoney(provider, pr.PayType, pr.UserId, info.TotalFee, pr.Id, pr.Title, pr.Type, pr.TargetId)) { throw new ArgumentException("退款失败!"); } } } else { throw new ArgumentException("更新订单失败!"); } } else { if (pr.Status != PayStatus.RefundSuccess) { throw new ArgumentException("批次号错误!"); } } DataSource.Commit(); return(true); } catch (Exception) { DataSource.Rollback(); //OnNotifyError(provider, ex.Message); } } //else //{ // OnNotifyError(provider, "退款数量错误!"); //} } //else //{ // OnNotifyError(provider, "批次号为空!"); //} } return(false); }
protected virtual void OnCallback(PayProvider provider, bool success, PaymentResult result) { #region Pay if (result.Type == PaymentType.Pay) { PayResult presult = (PayResult)result; if (!string.IsNullOrEmpty(presult.TradeNo)) { DataSource.Begin(); try { M.PayRecord pr = M.PayRecord.GetById(DataSource, presult.TradeNo, result.Type); if (pr == null) { throw new ArgumentException("订单号错误!"); } if (pr.Status >= PayStatus.Paying && pr.Status < PayStatus.PaySuccess) { PayStatus status = pr.Status; pr.PayId = presult.PayTradeNo; if (provider.IsOnlinePay) { pr.Money = presult.TotalFee; } if (success) { if (provider.IsNeedNotify) { pr.Status = PayStatus.PayNotifying; } else { pr.Status = PayStatus.PaySuccess; } } else { if (provider.IsOnlinePay && !provider.IsNeedNotify) { pr.Status = PayStatus.PayFailed; } else { if (!provider.IsOnlinePay) { throw new Exception("余额不足!"); } } } if (pr.UpdateStatus(DataSource, status) == DataStatus.Success) { if (success && pr.Status == PayStatus.PaySuccess) { if (!OnModifyMoney(provider, pr.PayType, pr.UserId, presult.TotalFee, pr.Id, pr.Title, pr.Type, pr.TargetId)) { throw new ArgumentException("充值失败!"); } } } else { throw new ArgumentException("更新订单失败!"); } } DataSource.Commit(); } catch (Exception ex) { DataSource.Rollback(); OnError(ex.Message); } } else { OnError("订单号为空!"); } } #endregion #region Refund else if (result.Type == PaymentType.Refund) { //RefundResult presult = (RefundResult)result; //if (!string.IsNullOrEmpty(presult.BatchNo)) //{ // DataSource.Begin(); // try // { // M.PayRecord pr = M.PayRecord.GetById(DataSource, presult.BatchNo, result.Type); // if (pr == null) // throw new ArgumentException("订单号错误!"); // if (pr.Status == PayStatus.RefundNotifying) // { // PayStatus status = pr.Status; // if (provider.IsOnlinePay) // pr.Money = presult.TotalFee; // if (success) // { // if (provider.IsNeedNotify) // pr.Status = PayStatus.PayNotifying; // else // pr.Status = PayStatus.PaySuccess; // } // else // { // if (provider.IsOnlinePay && !provider.IsNeedNotify) // { // pr.Status = PayStatus.PayFailed; // } // else // { // if (!provider.IsOnlinePay) // throw new Exception("余额不足!"); // } // } // if (pr.UpdateStatus(DataSource, status) == DataStatus.Success) // { // if (success && pr.Status == PayStatus.PaySuccess) // { // if (!OnModifyMoney(provider, pr.PayType, pr.UserId, presult.TotalFee, pr.Id, pr.Title, pr.Type, pr.TargetId)) // throw new ArgumentException("充值失败!"); // } // } // else // { // throw new ArgumentException("更新订单失败!"); // } // } // DataSource.Commit(); // } // catch (Exception ex) // { // DataSource.Rollback(); // OnError(ex.Message); // } //} //else //{ // OnError("订单号为空!"); //} } #endregion else { throw new ArgumentException("支付类型错误!"); } }