1. 程式人生 > >app微信支付的集成步驟

app微信支付的集成步驟

with rom str chat clas singletop new intent res


1.引用地址

//微信支付
compile ‘com.tencent.mm.opensdk:wechat-sdk-android-with-mta:+‘


2.註冊
    private IWXAPI api;
    api = WXAPIFactory.createWXAPI(this, appid);


3. 發起請求  (結果是後臺返回的)

PayReq req = new PayReq();
req.appId = "wxa9f8f372188a6867";
req.partnerId = "1487600712";
req.packageValue = "Sign=WXPay";

req.prepayId = "wx201710032153051e158cdeb50210343037";
req.nonceStr = "6md852kc4uik1m3bzr4sl3v5lr1ohh4d";
req.timeStamp = "1507038785";
req.sign = "0D684FF4D47E0E265E33FD67B3AE4CA7";
api.sendReq(req);

4.回調結果:

1.要在項目包名下新建wxapi包,
2.WXPayEntryActivity必須是這個類,實現這個接口IWXAPIEventHandler

3. 在清單文件中註冊
<activity
android:name=".wxapi.WXPayEntryActivity"
android:exported="true"
android:launchMode="singleTop"/>

//回調的頁面
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";

private IWXAPI api;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pay_result);

api = WXAPIFactory.createWXAPI(this,appid);
api.handleIntent(getIntent(), this);
}

@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
api.handleIntent(intent, this);
}

@Override
public void onReq(BaseReq req) {
}

@Override
public void onResp(BaseResp resp) {

if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("提示");
builder.setMessage(getString(R.string.pay_result_callback_msg, String.valueOf(resp.errCode)));
builder.show();
}
}
}

app微信支付的集成步驟