调用支付宝接口,完成付款功能
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??支付寶接口調用,完成付款
? ? ? 先到支付寶下載sdk,下載SDK,看看demo,可以看看支付寶開發文檔,Demo中,Base64.java、Result.java、Rsa.java這3個類不需要動,直接放到自己的項目中就行,在支付寶平臺注冊商家帳號,申請項目:? 申請要上傳你的apk或者產品說明文檔,我用的是產品說明文檔,里面要包括產品說明(最好有產品截圖)、接口使用場景、資費說明。
? ? ? ? ? ? 申請成功后按文檔上說明,用openssl做幾個密鑰,上傳你做的公鑰。然后配置。這里要注意的是,String PRIVATE=“”;這里要用PKCS8格式的私鑰。 ?配置成功即可用Demo測試啦!
? ? ? ? 支付寶結果返回是同步將結果返回給客戶,異步返回給商家的服務器,其實很簡單,下面是我調支付寶的程序,完成了支付寶接口的調用!
先在配置文件中添加?<!-- alipay sdk begin -->
 ? ? ? ? <activity
 ? ? ? ? ? ? android:name="com.alipay.sdk.app.H5PayActivity"
 ? ? ? ? ? ? android:configChanges="orientation|keyboardHidden|navigation|screenSize"
 ? ? ? ? ? ? android:exported="false"
 ? ? ? ? ? ? android:screenOrientation="behind"
 ? ? ? ? ? ? android:windowSoftInputMode="adjustResize|stateHidden" >
 ? ? ? ? </activity>
 ? ? ? ? <!-- alipay sdk end -->
?
然后是程序:
?
/******************** 增加內容支付寶 **********************/// 商戶PIDpublic static final String PARTNER = "2088221156661213";// 商戶收款賬號public static final String SELLER = "shenboshi_sjz@sina.com";// 商戶私鑰,pkcs8格式public static final String RSA_PRIVATE = "****";// 支付寶公鑰public static final String RSA_PUBLIC = "***";private static final int SDK_PAY_FLAG = 1;private static final int SDK_CHECK_FLAG = 2;private Handler mHandler = new Handler() {public void handleMessage(Message msg) {switch (msg.what) {case SDK_PAY_FLAG: {PayResult payResult = new PayResult((String) msg.obj);/*** 同步返回的結果必須放置到服務端進行驗證(驗證的規則請看https://doc.open.alipay.com/doc2/* detail.htm?spm=0.0.0.0.xdvAU6&treeId=59&articleId=103665&* docType=1) 建議商戶依賴異步通知*/String resultInfo = payResult.getResult();// 同步返回需要驗證的信息String resultStatus = payResult.getResultStatus();// 判斷resultStatus 為“9000”則代表支付成功,具體狀態碼代表含義可參考接口文檔if (TextUtils.equals(resultStatus, "9000")) {post();Toast.makeText(YuYue2.this, "支付成功", Toast.LENGTH_SHORT).show();Intent intent = new Intent(YuYue2.this,ShenQingChengGong.class);startActivity(intent);} else {// 判斷resultStatus 為非"9000"則代表可能支付失敗// "8000"代表支付結果因為支付渠道原因或者系統原因還在等待支付結果確認,最終交易是否成功以服務端異步通知為準(小概率狀態)if (TextUtils.equals(resultStatus, "8000")) {Toast.makeText(YuYue2.this, "支付結果確認中",Toast.LENGTH_SHORT).show();} else {// 其他值就可以判斷為支付失敗,包括用戶主動取消支付,或者系統返回的錯誤Toast.makeText(YuYue2.this, "支付失敗", Toast.LENGTH_SHORT).show();Intent intent = new Intent(YuYue2.this,ShenQingShiBai.class);startActivity(intent);}}break;}case SDK_CHECK_FLAG: {Toast.makeText(YuYue2.this, "檢查結果為:" + msg.obj,Toast.LENGTH_SHORT).show();break;}default:break;}}?
?
?
public void pay(View v) {if (TextUtils.isEmpty(PARTNER) || TextUtils.isEmpty(RSA_PRIVATE)|| TextUtils.isEmpty(SELLER)) {new AlertDialog.Builder(this).setTitle("警告").setMessage("需要配置PARTNER | RSA_PRIVATE| SELLER").setPositiveButton("確定",new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialoginterface, int i) {//finish();}}).show();return;}String orderInfo = getOrderInfo("診費", "+的診費", "0.01");/*** 特別注意,這里的簽名邏輯需要放在服務端,切勿將私鑰泄露在代碼中!*/String sign = sign(orderInfo);try {/*** 僅需對sign 做URL編碼*/sign = URLEncoder.encode(sign, "UTF-8");} catch (UnsupportedEncodingException e) {e.printStackTrace();}/*** 完整的符合支付寶參數規范的訂單信息*/final String payInfo = orderInfo + "&sign=\"" + sign + "\"&"+ getSignType();Runnable payRunnable = new Runnable() {@Overridepublic void run() {// 構造PayTask 對象PayTask alipay = new PayTask(YuYue2.this);// 調用支付接口,獲取支付結果String result = alipay.pay(payInfo, true);Message msg = new Message();msg.what = SDK_PAY_FLAG;msg.obj = result;mHandler.sendMessage(msg);}};// 必須異步調用Thread payThread = new Thread(payRunnable);// Toast.makeText(YuYue2.this, "hehe", 0).show();payThread.start();}/******************** 增加內容支付寶 **********************//*** create the order info. 創建訂單信息* */private String getOrderInfo(String subject, String body, String price) {// 簽約合作者身份IDString orderInfo = "partner=" + "\"" + PARTNER + "\"";// 簽約賣家支付寶賬號orderInfo += "&seller_id=" + "\"" + SELLER + "\"";// 商戶網站唯一訂單號orderInfo += "&out_trade_no=" + "\"" + getOutTradeNo() + "\"";// 商品名稱orderInfo += "&subject=" + "\"" + subject + "\"";// 商品詳情orderInfo += "&body=" + "\"" + body + "\"";// 商品金額orderInfo += "&total_fee=" + "\"" + price + "\"";// 服務器異步通知頁面路徑// 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;}/*** get the out_trade_no for an order. 生成商戶訂單號,該值在商戶端應保持唯一(可自定義格式規范)* */private String getOutTradeNo() {SimpleDateFormat format = new SimpleDateFormat("MMddHHmmss",Locale.getDefault());Date date = new Date();String key = format.format(date);Random r = new Random();key = key + r.nextInt();key = key.substring(0, 15);return key;}/*** sign the order info. 對訂單信息進行簽名* * @param content* 待簽名訂單信息*/private String sign(String content) {return SignUtils.sign(content, RSA_PRIVATE);}/*** get the sign type we use. 獲取簽名方式* */private String getSignType() {return "sign_type=\"RSA\"";}/******************************************/?
?
?
?
?
?
?
總結
以上是生活随笔為你收集整理的调用支付宝接口,完成付款功能的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 迭代器的工作原理
 - 下一篇: 古典文学--本经阴符七术