public async Task CancelOrder(string orderID) { dynamic postData = new WxPayData(_configuration.APIKey); postData.appid = _configuration.AppID; postData.mch_id = _configuration.MechantID; postData.out_trade_no = orderID; postData.Sign(); HttpContent postContent = new ByteArrayContent(postData.ToByteArray()); var response = await _httpClient.PostAsync("closeorder", postContent); if (!response.IsSuccessStatusCode) { throw new WxPayResponseInvalidException(); } var resContent = await response.Content.ReadAsStringAsync(); dynamic resData = WxPayData.FromXml(resContent, _configuration.APIKey); if (resData.VerifySignature()) { if (resData.return_code != "SUCCESS") { throw new WxPayBusinessException(resData.return_code, resData.return_msg); } else if (resData.result_code != "SUCCESS") { throw new WxPayBusinessException(resData.err_code, resData.err_code_des); } } else { throw new WxPaySignatureInvalidException("PlaceOrder return error"); } }
public async Task <OrderStatus> QueryOrder(string transactionID, string orderID) { dynamic postData = new WxPayData(_configuration.APIKey); postData.appid = _configuration.AppID; postData.mch_id = _configuration.MechantID; postData.transaction_id = transactionID; postData.out_trade_no = orderID; postData.Sign(); //System.Console.WriteLine($"postData: {postData.ToXml()}"); HttpContent postContent = new ByteArrayContent(postData.ToByteArray()); var response = await _httpClient.PostAsync("orderquery", postContent); if (!response.IsSuccessStatusCode) { throw new WxPayResponseInvalidException(); } var resContent = await response.Content.ReadAsStringAsync(); //System.Console.WriteLine($"resContent:{resContent}"); var resData = WxPayData.FromXml(resContent, _configuration.APIKey); if (resData.VerifySignature()) { return(OrderStatus.FromWxPayData(resData)); } else { throw new WxPaySignatureInvalidException("QueryOrder signature invalid"); } }
public async Task <OrderReceipt> PlaceOrder(string appID, string mechantID, string deviceInfo, string description, IEnumerable <GoodsDetails> details, string attachNote, string orderID, string currency, int totalAmount, string clientIP, DateTime?validFrom, DateTime?expireAt, string couponTag, string callbackUrl, WxPayApiType apiType, string productID, WxPayPaymentLimit paymentLimit, string userOpenID) { dynamic postData = new WxPayData(_configuration.APIKey); postData.appid = appID; postData.mch_id = mechantID; postData.device_info = deviceInfo; postData.body = description; postData.detail = SerializeGoodsDetails(details); postData.attach = attachNote; postData.out_trade_no = orderID; postData.fee_type = currency; postData.total_fee = totalAmount; postData.spbill_create_ip = clientIP; postData.time_start = ConvertToEast8TimeString(validFrom); postData.time_expire = ConvertToEast8TimeString(expireAt); postData.goods_tag = couponTag; postData.notify_url = callbackUrl; postData.trade_type = apiType.ToPostDataString(); postData.product_id = productID; postData.limit_pay = paymentLimit.ToPostDataString(); postData.openid = userOpenID; postData.Sign(); HttpContent postContent = new ByteArrayContent(postData.ToByteArray()); var response = await _httpClient.PostAsync("unifiedorder", postContent); if (!response.IsSuccessStatusCode) { throw new WxPayResponseInvalidException(); } var resContent = await response.Content.ReadAsStringAsync(); var resData = WxPayData.FromXml(resContent, _configuration.APIKey); if (resData.IsReturnedValid) { if (resData.VerifySignature()) { return(OrderReceipt.FromWxPayData(resData)); } else { throw new WxPaySignatureInvalidException("PlaceOrder return error"); } } else { throw new WxPayResponseInvalidException(resData.ReturnedError); } }
public async Task <RefundStatus> Refund(string transactionID, string orderID, string refundID, int totalAmount, int refundAmount, string currency) { throw new NotImplementedException(); dynamic postData = new WxPayData(_configuration.APIKey); postData.appid = _configuration.AppID; postData.mch_id = _configuration.MechantID; postData.out_trade_no = orderID; postData.transaction_id = transactionID; postData.out_refund_no = refundID; postData.total_fee = totalAmount; postData.refund_fee = refundAmount; postData.refund_fee_type = currency; postData.op_user_id = _configuration.MechantID; postData.Sign(); HttpContent postContent = new ByteArrayContent(postData.ToByteArray()); var response = await _secureHttpClient.PostAsync("refund", postContent); if (!response.IsSuccessStatusCode) { throw new WxPayResponseInvalidException(); } var resContent = await response.Content.ReadAsStringAsync(); dynamic resData = WxPayData.FromXml(resContent, _configuration.APIKey); if (resData.VerifySignature()) { if (resData.return_code != "SUCCESS") { throw new WxPayBusinessException(resData.return_code, resData.return_msg); } else if (resData.result_code != "SUCCESS") { throw new WxPayBusinessException(resData.err_code, resData.err_code_des); } else { return(RefundStatus.FromWxPayData(resData)); } } else { throw new WxPaySignatureInvalidException("PlaceOrder return error"); } }
public async Task <ExchangeRate> GetExchangeRate(string currency, DateTime date) { dynamic postData = new WxPayData(_configuration.APIKey); postData.appid = _configuration.AppID; postData.mch_id = _configuration.MechantID; postData.fee_type = currency; postData.date = ConvertToEast8TimeString(date).Substring(0, 8); postData.Sign(null); HttpContent postContent = new ByteArrayContent(postData.ToByteArray()); var response = await _httpClient.PostAsync("queryexchagerate", postContent); if (!response.IsSuccessStatusCode) { throw new WxPayResponseInvalidException(); } var resContent = await response.Content.ReadAsStringAsync(); dynamic resData = WxPayData.FromXml(resContent, _configuration.APIKey); if (resData.IsReturnedValid) { if (resData.VerifySignature()) { return(new ExchangeRate() { Currency = resData.fee_type, Date = DateTime.ParseExact(resData.rate_time, "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture), Rate = Decimal.Parse(resData.rate) / 100000000 }); } else { throw new WxPaySignatureInvalidException(); } } else { throw new WxPayResponseInvalidException(resData.ReturnedError); } }