【移動應用開發(fā)技術】Android中怎么實現微信支付功能_第1頁
【移動應用開發(fā)技術】Android中怎么實現微信支付功能_第2頁
【移動應用開發(fā)技術】Android中怎么實現微信支付功能_第3頁
【移動應用開發(fā)技術】Android中怎么實現微信支付功能_第4頁
【移動應用開發(fā)技術】Android中怎么實現微信支付功能_第5頁
已閱讀5頁,還剩6頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

【移動應用開發(fā)技術】Android中怎么實現微信支付功能

Android中怎么實現微信支付功能,相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。1.去微信開放平臺申請微信支付服務,綁定自己的應用這里具體不多講,但是一定要申請完成,將會得到是三個參數//appid

微信分配的公眾賬號ID

public

static

final

String

APP_ID

=

"";

//商戶號

微信分配的公眾賬號ID

public

static

final

String

MCH_ID

=

"";

//

API密鑰,在商戶平臺設置

public

static

final

String

API_KEY=

"";**坑點提示:在微信開發(fā)平臺設置包名和簽名。這里的包名一定要和你自己的包名一樣,就是manifest中的package,簽名一定要和你用官方app生成的一樣(/zh_CN/htmledition/res/dev/download/sdk/Gen_Signature_Android.apk)。微信會根據你的填寫的包名,然后對你的keystore進行一種算法,生成你的簽名。包名和簽名一定要和微信開放平臺的相同。不過這里需要注意的是,如果你發(fā)布的正式版本,需要用官方app重新生成簽名,然后在開放平臺重新設置sign,因為測試版本的keystore與正式版的keystore不一樣??傊?,就是你用的keystore生成的sign要和微信開放平臺的時刻保持一致。**2.準備工作做好了,接下來就是開發(fā)了,先下載微信的jar包,導入。微信支付分為三個步驟①.生成prepayId@Override

protected

Map<String,

String>

doInBackground(String...

params)

{

//

TODO

Auto-generated

method

stub

String

url=String.format(params[0]);

String

entity=getProductArgs();

Log.e("Simon",">>>>"+entity);

byte[]

buf=Util.httpPost(url,

entity);

String

content

=

new

String(buf);

Log.e("orion",

""+content);

Map<String,String>

xml=decodeXml(content);

return

xml;

}②.生成簽名參數private

void

genPayReq()

{

req.appId

=

Constants.APP_ID;

req.partnerId

=

Constants.MCH_ID;

if

(resultunifiedorder!=null)

{

req.prepayId

=

resultunifiedorder.get("prepay_id");

req.packageValue

=

"prepay_id="+resultunifiedorder.get("prepay_id");

}

else

{

Toast.makeText(MainActivity.this,

"prepayid為空",

Toast.LENGTH_SHORT).show();

}

req.nonceStr

=

getNonceStr();

req.timeStamp

=

String.valueOf(genTimeStamp());

List<NameValuePair>

signParams

=

new

LinkedList<NameValuePair>();

signParams.add(new

BasicNameValuePair("appid",

req.appId));

signParams.add(new

BasicNameValuePair("noncestr",

req.nonceStr));

signParams.add(new

BasicNameValuePair("package",

req.packageValue));

signParams.add(new

BasicNameValuePair("partnerid",

req.partnerId));

signParams.add(new

BasicNameValuePair("prepayid",

req.prepayId));

signParams.add(new

BasicNameValuePair("timestamp",

req.timeStamp));

req.sign

=

genAppSign(signParams);

sb.append("sign\n"+req.sign+"\n\n");

textView.setText(sb.toString());

Log.e("Simon",

""+signParams.toString());

}③.調起支付/*

*

調起微信支付

*/

private

void

sendPayReq()

{

msgApi.registerApp(Constants.APP_ID);

msgApi.sendReq(req);

Log.i(">>>>>",

req.partnerId);

}下面給出完整代碼

package

com.alpha.live;

import

java.io.StringReader;

import

java.util.HashMap;

import

java.util.LinkedList;

import

java.util.List;

import

java.util.Map;

import

java.util.Random;

import

org.apache.http.NameValuePair;

import

org.apache.http.message.BasicNameValuePair;

import

org.xmlpull.v1.XmlPullParser;

import

com.tencent.mm.sdk.modelpay.PayReq;

import

com.tencent.mm.sdk.openapi.IWXAPI;

import

com.tencent.mm.sdk.openapi.WXAPIFactory;

import

android.app.Activity;

import

android.app.AlertDialog;

import

android.app.ProgressDialog;

import

android.os.AsyncTask;

import

android.os.Bundle;

import

android.util.Log;

import

android.util.Xml;

import

android.view.View;

import

android.view.View.OnClickListener;

import

android.widget.Button;

import

android.widget.TextView;

import

android.widget.Toast;

/**

*

Created

by

Simon

on

2016/12/2.

*/

public

class

MainActivity

extends

Activity

implements

OnClickListener

{

private

Button

submitButton;

private

Button

confirmButton;

private

TextView

textView;

private

StringBuffer

sb;

private

Map<String,String>

resultunifiedorder;

private

PayReq

req;

private

final

IWXAPI

msgApi

=

WXAPIFactory.createWXAPI(this,

null);

@Override

protected

void

onCreate(Bundle

savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

submitButton=(Button)

findViewById(R.id.bt_submit_order);

confirmButton=(Button)

findViewById(R.id.bt_corfirm);

textView=(TextView)

findViewById(R.id.tv_prepay_id);

submitButton.setOnClickListener(this);

confirmButton.setOnClickListener(this);

sb=new

StringBuffer();

req=new

PayReq();

}

@Override

public

void

onClick(View

v)

{

//

TODO

Auto-generated

method

stub

switch

(v.getId())

{

case

R.id.bt_submit_order:

String

urlString="/pay/unifiedorder";

PrePayIdAsyncTask

prePayIdAsyncTask=new

PrePayIdAsyncTask();

prePayIdAsyncTask.execute(urlString);

//生成prepayId

break;

case

R.id.bt_corfirm:

genPayReq();//生成簽名參數

sendPayReq();//調起支付

break;

default:

break;

}

}

/*

*

調起微信支付

*/

private

void

sendPayReq()

{

msgApi.registerApp(Constants.APP_ID);

msgApi.sendReq(req);

Log.i(">>>>>",

req.partnerId);

}

private

long

genTimeStamp()

{

return

System.currentTimeMillis()

/

1000;

}

private

void

genPayReq()

{

req.appId

=

Constants.APP_ID;

req.partnerId

=

Constants.MCH_ID;

if

(resultunifiedorder!=null)

{

req.prepayId

=

resultunifiedorder.get("prepay_id");

req.packageValue

=

"prepay_id="+resultunifiedorder.get("prepay_id");

}

else

{

Toast.makeText(MainActivity.this,

"prepayid為空",

Toast.LENGTH_SHORT).show();

}

req.nonceStr

=

getNonceStr();

req.timeStamp

=

String.valueOf(genTimeStamp());

List<NameValuePair>

signParams

=

new

LinkedList<NameValuePair>();

signParams.add(new

BasicNameValuePair("appid",

req.appId));

signParams.add(new

BasicNameValuePair("noncestr",

req.nonceStr));

signParams.add(new

BasicNameValuePair("package",

req.packageValue));

signParams.add(new

BasicNameValuePair("partnerid",

req.partnerId));

signParams.add(new

BasicNameValuePair("prepayid",

req.prepayId));

signParams.add(new

BasicNameValuePair("timestamp",

req.timeStamp));

req.sign

=

genAppSign(signParams);

sb.append("sign\n"+req.sign+"\n\n");

textView.setText(sb.toString());

Log.e("Simon",

""+signParams.toString());

}

private

String

genAppSign(List<NameValuePair>

params)

{

StringBuilder

sb

=

new

StringBuilder();

for

(int

i

=

0;

i

<

params.size();

i++)

{

sb.append(params.get(i).getName());

sb.append('=');

sb.append(params.get(i).getValue());

sb.append('&');

}

sb.append("key=");

sb.append(Constants.API_KEY);

this.sb.append("sign

str\n"+sb.toString()+"\n\n");

String

appSign

=

MD5.getMessageDigest(sb.toString().getBytes());

Log.e("Simon",""+appSign);

return

appSign;

}

private

class

PrePayIdAsyncTask

extends

AsyncTask<String,Void,

Map<String,

String>>

{

private

ProgressDialog

dialog;

@Override

protected

void

onPreExecute()

{

//

TODO

Auto-generated

method

stub

super.onPreExecute();

dialog

=

ProgressDialog.show(MainActivity.this,

"提示",

"正在提交訂單");

}

@Override

protected

Map<String,

String>

doInBackground(String...

params)

{

//

TODO

Auto-generated

method

stub

String

url=String.format(params[0]);

String

entity=getProductArgs();

Log.e("Simon",">>>>"+entity);

byte[]

buf=Util.httpPost(url,

entity);

String

content

=

new

String(buf);

Log.e("orion",

""+content);

Map<String,String>

xml=decodeXml(content);

return

xml;

}

@Override

protected

void

onPostExecute(Map<String,

String>

result)

{

//

TODO

Auto-generated

method

stub

super.onPostExecute(result);

if

(dialog

!=

null)

{

dialog.dismiss();

}

sb.append("prepay_id\n"+result.get("prepay_id")+"\n\n");

textView.setText(sb.toString());

resultunifiedorder=result;

}

}

public

Map<String,String>

decodeXml(String

content)

{

try

{

Map<String,

String>

xml

=

new

HashMap<String,

String>();

XmlPullParser

parser

=

Xml.newPullParser();

parser.setInput(new

StringReader(content));

int

event

=

parser.getEventType();

while

(event

!=

XmlPullParser.END_DOCUMENT)

{

String

nodeName=parser.getName();

switch

(event)

{

case

XmlPullParser.START_DOCUMENT:

break;

case

XmlPullParser.START_TAG:

if("xml".equals(nodeName)==false){

//實例化student對象

xml.put(nodeName,parser.nextText());

}

break;

case

XmlPullParser.END_TAG:

break;

}

event

=

parser.next();

}

return

xml;

}

catch

(Exception

e)

{

Log.e("Simon",""+e.toString());

}

return

null;

}

private

String

getProductArgs()

{

//

TODO

Auto-generated

method

stub

StringBuffer

xml=new

StringBuffer();

try

{

String

nonceStr=getNonceStr();

xml.append("<xml>");

List<NameValuePair>

packageParams=new

LinkedList<NameValuePair>();

packageParams.add(new

BasicNameValuePair("appid",Constants.APP_ID));

packageParams.add(new

BasicNameValuePair("body",

"APP

pay

test"));

packageParams.add(new

BasicNameValuePair("mch_id",

Constants.MCH_ID));

packageParams.add(new

BasicNameValuePair("nonce_str",

nonceStr));

packageParams.add(new

BasicNameValuePair("notify_url",

""));//寫你們的回調地址

packageParams.add(new

BasicNameValuePair("out_trade_no",genOutTradNo()));

packageParams.add(new

BasicNameValuePair("total_fee",

"1"));

packageParams.add(new

BasicNameValuePair("trade_type",

"APP"));

String

sign=getPackageSign(packageParams);

packageParams.add(new

BasicNameValuePair("sign",

sign));

String

xmlString=toXml(packageParams);

return

xmlString;

}

catch

(Exception

e)

{

//

TODO:

handle

exception

return

null;

}

}

//生成訂單號,測試用,在客戶端生成

private

String

genOutTradNo()

{

Random

random

=

new

Random();

//

return

"dasgfsdg1234";

//訂單號寫死的話只能支付一次,第二次不能生成訂單

return

MD5.getMessageDigest(String.valueOf(random.nextInt(10000)).getBytes());

}

//生成隨機號,防重發(fā)

private

String

getNonceStr()

{

//

TODO

Auto-generated

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論