您好,登錄后才能下訂單哦!
開發環境Visual Studio 2015,Xamarin 3.11.1537,Xamarin Android5.1.7.12
下載支付寶移動支付的SDK(http://doc.open.alipay.com/doc2/detail?treeId=54&articleId=103419&docType=1),找到Andriud的SDK,其中有一個alipaySDK-20151014.jar(這個版本,以后肯定會變化),這是我們使用的重點。
在Visual Studio中創建一個綁定庫文件
把支付寶的alipaySDK-20151014.jar添加到Jars文件夾下,先中alipaySDK-20151014.jar,打開屬性窗口,設置生成操作為EmbeddedJar,庫文件搞定。
Android中使用綁定庫文件
在Visual Studio中創建一個BlankApp應用,然后在打開項目屬性,Android Manifest選項卡,在Required permissions中選擇ACCESS_WIFI_STATE和INTERNET權限,添加綁定庫生成的dll到當前項目的引用中。
添加簽名生成類
using System; using System.Text; using System.IO; using System.Security.Cryptography; namespace App1 { class SignatureUtils { /// <summary> /// 簽名 /// </summary> /// <param name="content">需要簽名的內容</param> /// <param name="privateKey">私鑰</param> /// <returns></returns> public static string Sign(string content, string privateKey) { byte[] Data = Encoding.GetEncoding("utf-8").GetBytes(content); RSACryptoServiceProvider rsa = DecodePemPrivateKey(privateKey); SHA1 sh = new SHA1CryptoServiceProvider(); byte[] signData = rsa.SignData(Data, sh); sh.Clear(); return Convert.ToBase64String(signData); } /// <summary> /// 對prikey進行處理,C#API默認的prikey是xml格式的,所以需要進行處理 /// </summary> /// <param name="pemstr"></param> /// <returns></returns> private static RSACryptoServiceProvider DecodePemPrivateKey(String pemstr) { byte[] pkcs8privatekey; pkcs8privatekey = Convert.FromBase64String(pemstr); if (pkcs8privatekey != null) { RSACryptoServiceProvider rsa = DecodePrivateKeyInfo(pkcs8privatekey); return rsa; } else return null; } /// <summary> /// 轉換prikey /// </summary> /// <param name="pkcs8"></param> /// <returns></returns> private static RSACryptoServiceProvider DecodePrivateKeyInfo(byte[] pkcs8) { byte[] SeqOID = { 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01, 0x05, 0x00 }; byte[] seq = new byte[15]; MemoryStream mem = new MemoryStream(pkcs8); int lenstream = (int)mem.Length; BinaryReader binr = new BinaryReader(mem); byte bt = 0; ushort twobytes = 0; try { twobytes = binr.ReadUInt16(); if (twobytes == 0x8130) binr.ReadByte(); else if (twobytes == 0x8230) binr.ReadInt16(); else return null; bt = binr.ReadByte(); if (bt != 0x02) return null; twobytes = binr.ReadUInt16(); if (twobytes != 0x0001) return null; seq = binr.ReadBytes(15); if (!CompareBytearrays(seq, SeqOID)) return null; bt = binr.ReadByte(); if (bt != 0x04) return null; bt = binr.ReadByte(); if (bt == 0x81) binr.ReadByte(); else if (bt == 0x82) binr.ReadUInt16(); byte[] rsaprivkey = binr.ReadBytes((int)(lenstream - mem.Position)); RSACryptoServiceProvider rsacsp = DecodeRSAPrivateKey(rsaprivkey); return rsacsp; } catch (Exception) { return null; } finally { binr.Close(); } } private static bool CompareBytearrays(byte[] a, byte[] b) { if (a.Length != b.Length) return false; int i = 0; foreach (byte c in a) { if (c != b[i]) return false; i++; } return true; } /// <summary> /// 處理私鑰 /// </summary> /// <param name="privkey"></param> /// <returns></returns> private static RSACryptoServiceProvider DecodeRSAPrivateKey(byte[] privkey) { byte[] MODULUS, E, D, P, Q, DP, DQ, IQ; MemoryStream mem = new MemoryStream(privkey); BinaryReader binr = new BinaryReader(mem); byte bt = 0; ushort twobytes = 0; int elems = 0; try { twobytes = binr.ReadUInt16(); if (twobytes == 0x8130) binr.ReadByte(); else if (twobytes == 0x8230) binr.ReadInt16(); else return null; twobytes = binr.ReadUInt16(); if (twobytes != 0x0102) return null; bt = binr.ReadByte(); if (bt != 0x00) return null; elems = GetIntegerSize(binr); MODULUS = binr.ReadBytes(elems); elems = GetIntegerSize(binr); E = binr.ReadBytes(elems); elems = GetIntegerSize(binr); D = binr.ReadBytes(elems); elems = GetIntegerSize(binr); P = binr.ReadBytes(elems); elems = GetIntegerSize(binr); Q = binr.ReadBytes(elems); elems = GetIntegerSize(binr); DP = binr.ReadBytes(elems); elems = GetIntegerSize(binr); DQ = binr.ReadBytes(elems); elems = GetIntegerSize(binr); IQ = binr.ReadBytes(elems); CspParameters cspParams = new CspParameters(); cspParams.Flags = CspProviderFlags.UseMachineKeyStore; //System.Security.Cryptography.RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(1024, cspParams); RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(1024); RSAParameters RSAparams = new RSAParameters(); RSAparams.Modulus = MODULUS; RSAparams.Exponent = E; RSAparams.D = D; RSAparams.P = P; RSAparams.Q = Q; RSAparams.DP = DP; RSAparams.DQ = DQ; RSAparams.InverseQ = IQ; RSA.ImportParameters(RSAparams); return RSA; } catch (Exception) { return null; } finally { binr.Close(); } } /// <summary> /// 獲取binr的長度 /// </summary> /// <param name="binr"></param> /// <returns></returns> private static int GetIntegerSize(BinaryReader binr) { byte bt = 0; byte lowbyte = 0x00; byte highbyte = 0x00; int count = 0; bt = binr.ReadByte(); if (bt != 0x02) return 0; bt = binr.ReadByte(); if (bt == 0x81) count = binr.ReadByte(); else if (bt == 0x82) { highbyte = binr.ReadByte(); lowbyte = binr.ReadByte(); byte[] modint = { lowbyte, highbyte, 0x00, 0x00 }; count = BitConverter.ToInt32(modint, 0); } else { count = bt; } while (binr.ReadByte() == 0x00) { count -= 1; } binr.BaseStream.Seek(-1, SeekOrigin.Current); return count; } } }
在主程序中添加如下代碼:
using System; using Android.App; using Android.Widget; using Android.OS; using Java.Net; namespace App1 { [Activity(Label = "App1", MainLauncher = true)] public class MainActivity : Activity { // 合作商戶ID。用簽約支付寶賬號登錄ms.alipay.com后,在賬戶信息頁面獲取。 public static string PARTNER = "替換你支付寶申請的partner"; // 商戶收款的支付寶賬號 public static string SELLER = "替換你支付寶申請的seller"; //商戶私密 string RSA_PRIVATE = "替換你支付寶申請的private_key"; ; protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); SetContentView(Resource.Layout.Main); Button button = FindViewById<Button>(Resource.Id.MyButton); button.Click += HandleClick; } void HandleClick(object sender, EventArgs e) { System.Threading.Thread the = new System.Threading.Thread(Pay); the.Start(); } void Pay() { var con = getOrderInfo("test", "testbody"); var sign = SignatureUtils.Sign(con, RSA_PRIVATE); sign = URLEncoder.Encode(sign, "utf-8"); con += "&sign=\"" + sign + "\"&" + MySignType; Com.Alipay.Sdk.App.PayTask pa = new Com.Alipay.Sdk.App.PayTask(this); var result = pa.Pay(con); //調用結果查看result中是否返回是90000,如果是,則成功 } #region 組合 public String getOrderInfo(String subject, String body) { // 簽約合作者身份ID String orderInfo = "partner=" + "\"" + PARTNER + "\""; // 簽約賣家支付寶賬號 orderInfo += "&seller_id=" + "\"" + SELLER + "\""; // 商戶網站唯一訂單號 orderInfo += "&out_trade_no=" + "\"DJ" + DateTime.Now.ToString("yyyyMMddhhmmss") + "\""; // 商品名稱 orderInfo += "&subject=" + "\"" + subject + "\""; // 商品詳情 orderInfo += "&body=" + "\"" + body + "\""; // 商品金額 orderInfo += "&total_fee=" + "\"" + 0.01 + "\""; // 服務器異步通知頁面路徑 orderInfo += "¬ify_url=" + "\"" + "http://notify.msp.hk/notify.htm" + "\""; // 服務接口名稱, 固定值
orderInfo += "&service=\"mobile.securitypay.pay\"";
// 支付類型, 固定值
orderInfo += "&payment_type=\"1\"";
// 參數編碼, 固定值
orderInfo += "&_input_charset=\"utf-8\"";
// 設置未付款交易的超時時間
// 默認30分鐘,一旦超時,該筆交易就會自動被關閉。
// 取值范圍:1m~15d。
// m-分鐘,h-小時,d-天,1c-當天(無論交易何時創建,都在0點關閉)。
// 該參數數值不接受小數點,如1.5h,可轉換為90m。
orderInfo += "&it_b_pay=\"30m\"";
// extern_token為經過快登授權獲取到的alipay_open_id,帶上此參數用戶將使用授權的賬戶進行支付
// orderInfo += "&extern_token=" + "\"" + extern_token + "\"";
// 支付寶處理完請求后,當前頁面跳轉到商戶指定頁面的路徑,可空
orderInfo += "&return_url=\"m.alipay.com\"";
// 調用銀行卡支付,需配置此參數,參與簽名, 固定值 (需要簽約《無線銀行卡快捷支付》才能使用)
// orderInfo += "&paymethod=\"expressGateway\"";
return orderInfo;
}
public String MySignType
{
get
{
return "sign_type=\"RSA\"";
}
}
#endregion
}
}
源代碼見附件
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。