public virtual async Task HandleEventAsync(WeChatPayRefundEto eventData) { // Todo: Handle errors and rollback using (_currentTenant.Change(eventData.TenantId)) { var payment = await _paymentRepository.GetAsync(eventData.PaymentId); var paymentRecord = await _paymentRecordRepository.GetByPaymentId(eventData.PaymentId); var refundRecordId = _guidGenerator.Create(); var dict = await RequestWeChatPayRefundAsync(payment, paymentRecord, eventData, refundRecordId.ToString()); var externalTradingCode = dict.GetOrDefault("refund_id"); eventData.Refund.SetExternalTradingCode(externalTradingCode); await _refundRepository.UpdateAsync(eventData.Refund, true); if (dict.GetOrDefault("result_code") != "SUCCESS") { await _paymentManager.RollbackRefundAsync(payment, eventData.Refund); } } }
private async Task <Dictionary <string, string> > RequestWeChatPayRefundAsync(Payment payment, PaymentRecord paymentRecord, WeChatPayRefundEto eventData, string outRefundNo) { var refundAmount = eventData.Refund.RefundAmount; var result = await _serviceProviderPayService.RefundAsync( appId : payment.GetProperty <string>("appid"), mchId : payment.PayeeAccount, subAppId : null, subMchId : null, transactionId : paymentRecord.TransactionId, outTradeNo : payment.Id.ToString("N"), outRefundNo : outRefundNo, totalFee : paymentRecord.TotalFee, refundFee : _weChatPayFeeConverter.ConvertToWeChatPayFee(refundAmount), refundFeeType : null, refundDesc : eventData.Refund.DisplayReason, refundAccount : null, notifyUrl : null ); var dict = new Dictionary <string, string>(result.SelectSingleNode("xml").ToDictionary() ?? throw new NullReferenceException()); if (dict.GetOrDefault("return_code") != "SUCCESS") { throw new RefundFailedException(dict.GetOrDefault("return_code"), dict.GetOrDefault("return_msg")); } await CreateWeChatPayRefundRecordEntitiesAsync(payment, dict); return(dict); }