想用c# 的wcf处理支付宝的异步通知,但是不知道如何定义接口里的接收参数和返回值

想用c# 的wcf处理支付宝的异步通知,但是不知道如何定义接口里的接收参数和返回值

问题描述:

[OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = MerchantServiceUri.AsynchConfirmAlipayRecharge, BodyStyle = WebMessageBodyStyle.Bare,
            RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]

string AcceptAlipayNotify(Stream stream);

上面是一个普通的wcf接口定义,我有三个地方不太懂

1、支付宝的异步通知虽然是post方法,但是参数都是在url里面的,例如:

https: //商家网站通知地址?voucher_detail_list=[{"amount":"0.20","merchantContribute":"0.00","name":"5折券","otherContribute":"0.20","type":"ALIPAY_DISCOUNT_VOUCHER","voucherId":"2016101200073002586200003BQ4"}]&fund_bill_list=[{"amount":"0.80","fundChannel":"ALIPAYACCOUNT"},{"amount":"0.20","fundChannel":"MDISCOUNT"}]&subject=PC网站支付交易&trade_no=2016101221001004580200203978&gmt_create=2016-10-12 21:36:12&notify_type=trade_status_sync&total_amount=1.00&out_trade_no=mobile_rdm862016-10-12213600&invoice_amount=0.80&seller_id=2088201909970555&notify_time=2016-10-12 21:41:23&trade_status=TRADE_SUCCESS&gmt_payment=2016-10-12 21:37:19&receipt_amount=0.80&passback_params=passback_params123&buyer_id=2088102114562585&app_id=2016092101248425&notify_id=7676a2e1e4e737cff30015c4b7b55e3kh6& sign_type=RSA2&buyer_pay_amount=0.80&sign=***&point_amount=0.00

我应该如何获取这些参数?

2、如果获取了这些参数,如何解析成键值对的模式。 

3、返回值应该如何处理。支付宝要求返回sucess,不能带任何其他字符,ResponseFormat = WebMessageFormat.Json这里感觉就不太对。是否应该用stream来作为返回值?

这样就好了。没啥问题,记得采纳一下

1、支付宝异步通知,好像并没说是post请求方式。而且就算是post请求方式,参数拼接在url后面也是可以的。

2、

//获取所有参数,存入map中
public Dictionary<string, string> GetRequestPost()
    {
        int i = 0;
        Dictionary<string, string> sArray = new Dictionary<string, string>();
        NameValueCollection coll;
        coll = Request.Form;
        String[] requestItem = coll.AllKeys;
        for (i = 0; i < requestItem.Length; i++)
        {
            sArray.Add(requestItem[i], Request.Form[requestItem[i]]);
        }
        return sArray;
 
    }

3、Response.Write("success");     //返回给支付宝消息,成功

可以参考:https://blog.csdn.net/lwpoor123/article/details/79020664

第3点仍然有疑问,我需要知道在wcf里如何返回。

wcf中,你直接return “success”;就好了

private Stream GetJsonStream(string str)
{
MemoryStream ms = new MemoryStream();
StreamWriter sw = new StreamWriter(ms);
sw.AutoFlush = true;
sw.Write(str);
ms.Position = 0;
System.ServiceModel.Web.WebOperationContext.Current.OutgoingResponse.ContentType = "application/json";
return ms;
}

 

你之前方法不行,因为wcf会自动给结果加上"",后面的方法正确,另外还有一个问题盼请教下。

我要在wcf里面获取post过来的json格式的body,但是总报错

这是服务端的定义

[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/GetBody", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
void GetBody(Stream stream);

这是客户端的程序

var client = new RestClient("http://127.0.0.1");
var request = new RestRequest("/GetBody", Method.POST);
request.AddParameter("application/json", "{\"PortalType\":\"Merchant\"}", ParameterType.RequestBody);
var response = client.Execute(restRequest);

错误如下:"StatusCode: BadRequest, Content-Type: text/html, Content-Length: 2897)"

如果客户端的请求换成下面的,就可以正常获得body字符串

var client = new RestClient("http://127.0.0.1");
var request = new RestRequest("/GetBody", Method.POST);
//request.AddParameter("application/json", "{\"PortalType\":\"Merchant\"}", ParameterType.RequestBody);
requst.AddParameter("PortalType", "Merchant");
var response = client.Execute(restRequest);

找了很多资料,都没解决。