Alipay interface (just finished, should be the best of the present)

Source: Internet
Author: User
Tags foreach format bool datetime log tostring urlencode log4net
Interface

Alipay's interface call is very inconvenient, just do a package, realize the virtual transaction and physical transactions.
There are three projects in the solution as well as NDoc generated documents, simple sequence diagrams: Commonalipay, encapsulated Alipay interface.
Testali,asp.net's Test project
Testcommonalipay,nunit's Test project.
Call Method:
1. Introduction of CommonAliPay.dll
2, the implementation of Alipay service interface method call Way:
AliPay ap = new AliPay ();
string key = "";//fill in your key
String partner = "";/fill in your own partner
StandardGoods bp = new StandardGoods ("Trade_create_by_buyer", partner, Key, "MD5", "Card 2", Guid.NewGuid (). ToString (), 2.551m, 1, "hao_ding2000@yahoo.com.cn", "hao_ding2000@yahoo.com.cn"
, "EMS", 25.00m, "Buyer_pay", "1");
Bp. Notify_url = "http://203.86.79.185/ali/notify.aspx";
Ap. Createstandardtrade ("Https://www.alipay.com/cooperate/gateway.do", BP, this); The above is the universal calling method.
The following are the only ways to support virtual goods:
string key = "";//fill in your key
String partner = "";/fill in your own partner
AliPay ap = new AliPay ();
Digitalgoods bp = new Digitalgoods ("Create_digital_goods_trade_p", partner, Key, "MD5", "Card 2", Guid.NewGuid (). ToString (), 2.551m, 1, "hao_ding2000@yahoo.com.cn", "hao_ding2000@yahoo.com.cn");
Bp. Notify_url = "http://203.86.79.185/ali/notify.aspx";
Ap. Createdigitaltrade ("Https://www.alipay.com/cooperate/gateway.do", BP, this); 3, the implementation of Alipay notification interface method calls (support virtual and in-kind):
protected void Page_Load (object sender, EventArgs e)
{

string key = "";//fill in your key
String partner = "";/fill in your own partner
AliPay ap = new AliPay ();
String Notifyid = request.form["notify_id"];
Verify v = new Verify ("Notify_verify", partner, Notifyid);
Ap. Waitsellersendgoods+=new Alipay.processnotifyeventhandler (ap_waitsellersendgoods);
Ap. Waitbuyerpay + = new Alipay.processnotifyeventhandler (Ap_waitbuyerpay);
Ap. Processnotify (This, "https://www.alipay.com/cooperate/gateway.do", Key,v, "utf-8");
}

void Ap_waitbuyerpay (object sender, Notifyeventargs e)
{
Add your own processing logic
Log4net.log.Error ("Wait buyer pay fire");
}

  
    private void Ap_waitsellersendgoods (object sender, Notifyeventargs e)
    {
       //Add your own processing logic
         Log4net.log.Error ("Waitsellersendgoods Fire"); The transaction status of the
   } Alipay is defined as an event of similar name.
Partial source code resolution:
1, parse Forms collection to the Notifyeventargs class, because the data for this class is used to do md5sign, so all value types cannot have initial values, such as 0 of Int. So use the nullable model.
   Private Notifyeventargs parsenotify (NameValueCollection NV, object obj)
         {
            propertyinfo[] Propertyinfos = obj. GetType (). GetProperties (BindingFlags.Public | BindingFlags.Instance);

foreach (PropertyInfo pi in Propertyinfos)
{
String v = NV. Get (pi. Name.tolower ());
if (v!= null)
{
if (pi. PropertyType = = typeof (String)
{

Pi. SetValue (obj, V, null);

                    }
                     else if (pi. PropertyType = = typeof (int?))
                     {
                         Pi. SetValue (obj, int. Parse (v), NULL);
                    }
                     else if (pi. PropertyType = = typeof (decimal))
                     {

                         Pi. SetValue (obj, decimal. Parse (v), NULL);
                    }
                     else if (pi. PropertyType = = typeof (DateTime))
                     {

Pi. SetValue (obj, DateTime.Parse (v), NULL);
}
else if (pi. PropertyType = = typeof (bool))
{

Pi. SetValue (obj, bool. Parse (v), NULL);
}
Else
{
Transformation failure throws an exception
Pi. SetValue (obj, V, null);
}
}

}
Return (Notifyeventargs) obj;

}
2. Get sorted parameters from type
/**////<summary>
Get the sorted parameters
</summary>
<param name= "obj" ></param>
<returns></returns>
Private sortedlist<string,string> GetParam (object obj)
{

propertyinfo[] Propertyinfos = obj. GetType (). GetProperties (bindingflags.public| BindingFlags.Instance);
sortedlist<string, string> sortedlist = new sortedlist<string, string> ( Stringcomparer.currentcultureignorecase);
foreach (PropertyInfo pi in Propertyinfos)
{

if (pi. GetValue (obj, null)!= null)
{
if (pi. Name = = "Sign" | | Pi. Name = = "Sign_type")
{
Continue
}
Sortedlist.add (pi. Name.tolower (), Pi. GetValue (obj, null). ToString ());

}
}
return sortedlist;

}3, generating parameters from SortedList
private String Geturlparam (sortedlist<string, string> sortedlist,bool isencode)
{
StringBuilder param = new StringBuilder ();
StringBuilder Encodeparam = new StringBuilder ();
if (Isencode = = False)
{

foreach (keyvaluepair<string, string> kvp in SortedList)
{
String t = String. Format (' {0}={1} ', kvp. Key, kvp. Value);
Param. Append (t + "&");
}
return param. ToString (). TrimEnd (' & ');
}
Else
{
foreach (keyvaluepair<string, string> kvp in SortedList)
{
String et = string. Format ("{0}={1}", Httputility.urlencode (KVP). Key), Httputility.urlencode (kvp. Value));
Encodeparam.append (et + "&");
}
Return encodeparam.tostring (). TrimEnd (' & ');
}

}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.