VopSdk Develops SDK with a High Compulsory Wechat Public Number

Keywords: ASP.NET JSON SDK xml

1. Our Goals

Separation of basic and operational parameters.

High reuse and scalability.

Lightweight.

II. Achieving Goals

Separation of basic and operational parameters

Carefully analyze all interfaces and extract common parameters for each module interface.

A. Analysis of all interfaces for Wechat Public Number (excluding Wechat Payment)

(a) Access_Token is acquired using AppId and AppSecret.

b. Web authorization is relatively independent, but AppId and AppSecret are also used.

c. Other interfaces are all based on Access_Token to obtain data.

According to a, b and c, the basic parameters of SDK are AppId, AppSecret and Access_Token.

 

B. Analysis of Wechat Payment Interface

appid, mch_id, nonce_str, sign_type and sign are all the basic parameters of SDK.

There are two special parameters for refund, certificate path and certificate password.

 

Based on the results of A and B, we extract the basic parameters. The basic parameters can not be copied with the business parameters only by initialization or by calling a separate method. The business parameters are used to adjust and fix the method one-time.

 

High reuse and scalability

There are two kinds of calling methods GET and POST for common interface. For GET, the parameters are on the address. The parameters on the address include both basic parameters and business parameters; for POST, the business parameters are in JSON format; and the parameters are all json.

Payment interface input and output parameters are xml.

 

Each interface object can set its own request mode (GET or POST); each interface can set its own formatting mode (json or xml).

Public methods such as whether certificates are needed and certificate paths and certificate passwords can be set for refunds.

 

(3) Lightweight

Do not refer to third-party components.

 

 

The following is a sample test:

a. Delete menu

        public static void Test()
        {
            Vop.Api.Request.VopMobilePublicMenuDeleteRequest request = new Vop.Api.Request.VopMobilePublicMenuDeleteRequest();
            Vop.Api.IVopClient client = new Vop.Api.DefaultVopClient(Config.AppId, Config.AppSecret);
            string accessToken = Vop.Api.Util.AccessTokenContainer.GetAccessToken(Config.AppId, Config.AppSecret);
            var result = client.Execute(request, accessToken);
        }

 

b. Creating menus

        public static void Test()
        {
             string menuStr = "{\"button\":[{\"name\":\"Medical business\",\"sub_button\":[{\"type\":\"view\",\"name\":\"homepage\",\"url\":\"http://m.baidu.com\"}]}]}";

            Vop.Api.Request.VopMobilePublicMenuCreateRequest request = new Vop.Api.Request.VopMobilePublicMenuCreateRequest();
            request.SetBizModel(menuStr);
            Vop.Api.IVopClient client = new Vop.Api.DefaultVopClient(Config.AppId, Config.AppSecret);
            string accessToken = Vop.Api.Util.AccessTokenContainer.GetAccessToken(Config.AppId, Config.AppSecret);
            var result = client.Execute(request, accessToken);
        }

c. Sending messages

        public static void Test()
        {
            Vop.Api.Request.VopMobilePublicMessageCustomSendRequest request = new Vop.Api.Request.VopMobilePublicMessageCustomSendRequest();
            request.SetBizModel("{\"touser\":\"osDGfuNaaSJ1LbScpzpeRXF107L4\",\"msgtype\":\"text\",\"text\":{\"content\":\"Hello\"}}");
            Vop.Api.IVopClient client = new Vop.Api.DefaultVopClient(Config.AppId, Config.AppSecret);
            string accessToken = Vop.Api.Util.AccessTokenContainer.GetAccessToken(Config.AppId, Config.AppSecret);
            var result = client.Execute(request, accessToken);
        }

 

d. Getting User Information

        public static void Test()
        {
            Vop.Api.Request.VopMobilePublicUserInfoRequest request = new Vop.Api.Request.VopMobilePublicUserInfoRequest();
            request.SetBizModel("openid=osDGfuNaaSJ1LbScpzpeRXF107L4&lang=zh_CN");
            Vop.Api.IVopClient client = new Vop.Api.DefaultVopClient(Config.AppId, Config.AppSecret);
            string accessToken = Vop.Api.Util.AccessTokenContainer.GetAccessToken(Config.AppId, Config.AppSecret);
            var result = client.Execute(request, accessToken);
        }

 

e. Payment Interface

        public static void Test()
        {
            Vop.Api.IVopClient client = new Vop.Api.DefaultVopClient(Config.AppId, Config.AppSecret, "utf-8", Config.MchId, Config.MchSecret);
            Vop.Api.Request.VopTradeWapPayRequest request = new Vop.Api.Request.VopTradeWapPayRequest();
            request.SetBizModel(string.Format("<body>{0}</body><out_trade_no>{1}</out_trade_no><total_fee>{2}</total_fee><spbill_create_ip>{3}</spbill_create_ip><notify_url>{4}</notify_url><trade_type>{5}</trade_type><openid>{6}</openid>", "test", CreateOrderNo(), 1, "8.8.8.8", "http://wxcp.zlsoft.com/open/check", "JSAPI", "oskHZwpNB6K4eaGkL3m7bPdAZQ0Y"));
            var r1 = client.PayExecute(request);
        }
        private static string CreateOrderNo()
        {
            return string.Format("{0}{1:D4}", DateTime.Now.ToString("yyyyMMddHHmmss"), Math.Abs(BitConverter.ToInt32(Guid.NewGuid().ToByteArray(), 0)));
        }

 

 

. . . . . . .

 

Isn't it very simple? What if I want to add a new interface, So Easy.

For example, by adding an interface to send template messages, you only need to create two classes to complete:

Interface Request Object: VopMobile PublicMessageTemplateSendRequest.cs

Interface return entity: VopMobile PublicMessageTemplate SendResponse.cs

 

VopMobilePublicMessageTemplateSendRequest.cs

namespace Vop.Api.Request
{
    public class VopMobilePublicMessageTemplateSendRequest : VopPublicRequest<VopMobilePublicMessageTemplateSendResponse>, IVopRequest<VopMobilePublicMessageTemplateSendResponse>
    {
        public override string GetApiUrl()
        {
            this.apiUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={0}";
            return string.Format(this.apiUrl, GetAccessToken());
        }

        public override string GetApiMethod()
        {
            this.apiMethod = "POST";
            return this.apiMethod;
        }
    }
}

 

VopMobilePublicMessageTemplateSendResponse.cs

  [DataContract] public class VopMobilePublicMessageTemplateSendResponse : VopPublicResponse { } 

 

Okay, the new send template message is finished. Here is the test of the interface.

        public static void Test()
        {
            Vop.Api.Request.VopMobilePublicMessageTemplateSendRequest request = new Vop.Api.Request.VopMobilePublicMessageTemplateSendRequest();
            request.SetBizModel(Config.GetTestData("VopMobilePublicMessageTemplateSendTest"));
            Vop.Api.IVopClient client = new Vop.Api.DefaultVopClient(Config.AppId, Config.AppSecret);
            string accessToken = Vop.Api.Util.AccessTokenContainer.GetAccessToken(Config.AppId, Config.AppSecret);
            var result = client.Execute(request, accessToken);
        }

 

 

Want to know how to achieve it? Get the source code I don't give, you hit me ah, ah ha ha ha....

Project screenshots

 

Original address: http://www.cnblogs.com/deeround/p/6803960.html 

Source address: Respond to email, I will send.

Posted by samuraitux on Wed, 03 Jul 2019 18:04:13 -0700