微信支付接入(Android/IOS(swift)/Java後臺)
阿新 • • 發佈:2019-02-10
-
建立 包名.wxapi.WXPayEntryActivity 檔案,保證內容如下(WechartUtils下面會給出):
import android.app.Activity; import android.os.Bundle; import android.util.Log; import com.tencent.mm.opensdk.constants.ConstantsAPI; import com.tencent.mm.opensdk.modelbase.BaseReq; import com.tencent.mm.opensdk.modelbase.BaseResp; import com.tencent.mm.opensdk.modelpay.PayResp; import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler; public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler { public static final String APP_ID = "";//填入自己的appid public static Back authBack; public interface Back { public void onFiled(String errorMsg); public void onSuccess(String code, String state); } public static void registAuthBack(Back back) { authBack = back; } public static void unregistAuthBack() { authBack = null; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); WechartUtils.getInstance(this, APP_ID) .handleIntent(this, this); } @Override public void onReq(BaseReq baseReq) { Log.e("-----1", "baseReq=" + baseReq); } @Override public void onResp(BaseResp baseResp) { switch (baseResp.getType()) { case ConstantsAPI.COMMAND_PAY_BY_WX: if (authBack != null) { // 0:成功; -1:錯誤; -2:使用者取消; PayResp payResp = (PayResp) baseResp; if (payResp.errCode == 0) { authBack.onSuccess(String.valueOf(payResp.errCode), payResp.prepayId); } else { authBack.onFiled(baseResp.errStr); } } break; } finish(); } @Override protected void onDestroy() { super.onDestroy(); WechartUtils.getInstance(this, APP_ID) .unregisterApp(this); } }
-
建立WechartUtils檔案用於公共處理支付
import android.app.Activity; import android.util.Log; import com.tencent.mm.opensdk.modelmsg.SendAuth; import com.tencent.mm.opensdk.modelpay.PayReq; import com.tencent.mm.opensdk.openapi.IWXAPI; import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler; import com.tencent.mm.opensdk.openapi.WXAPIFactory; import java.util.concurrent.ConcurrentHashMap; public class WechartUtils { private static final WechartUtils wechartUtils = new WechartUtils(); private static final ConcurrentHashMap<String, IWXAPI> apis = new ConcurrentHashMap<>(); private WechartUtils() { } public static WechartUtils getInstance(Activity activity, String appId) { if (activity != null && !apis.containsKey(activity.getLocalClassName())) { IWXAPI api = WXAPIFactory.createWXAPI(activity, appId, true); api.registerApp(appId); apis.put(activity.getLocalClassName(), api); } return wechartUtils; } public WechartUtils handleIntent(Activity activity , IWXAPIEventHandler handler) { apis.get(activity.getLocalClassName()) .handleIntent(activity.getIntent(), handler); return this; } public void pay(Activity activity, String appId, String partnerId, String packageName , String prepayId, String nonceStr, String timeStamp, String sign) { if (activity == null) { return; } PayReq req = new PayReq(); req.appId = appId;//appid req.partnerId = partnerId;//微信支付分配的商戶號 req.packageValue = packageName;//暫填寫固定值Sign=WXPay req.prepayId = prepayId;//微信返回的支付交易會話ID req.nonceStr = nonceStr;//隨機字串 req.timeStamp = timeStamp;//時間戳 req.sign = sign;//簽名 IWXAPI api = apis.get(activity.getLocalClassName()); if (api != null) { api.sendReq(req); } } public void login(Activity activity) { if (activity == null) { return; } SendAuth.Req req = new SendAuth.Req(); req.scope = "snsapi_userinfo"; req.state = String.valueOf(System.currentTimeMillis()); IWXAPI api = apis.get(activity.getLocalClassName()); if (api != null) { api.sendReq(req); } } public void unregisterApp(Activity activity) { if (activity == null) { return; } IWXAPI api = apis.get(activity.getLocalClassName()); if (api != null) { api.unregisterApp(); apis.remove(activity.getLocalClassName()); } } }
-
在AndroidManifest.xml檔案內註冊activity
<activity android:name=".wxapi.WXPayEntryActivity" android:exported="true" android:label="@string/app_name" />
-
建立好工具檔案後可以開始準備開發了,這裡貼出主要使用虛擬碼。
public class DemoActivity extends Activity { private WechartUtils wechartUtils; @Override protected void onCreate(Bundle savedInstanceState) { //初始化工具 wechartUtils = WechartUtils.getInstance(this, WXPayEntryActivity.APP_ID); //註冊微信支付回撥 WXPayEntryActivity.registAuthBack(new WXPayEntryActivity.Back() { @Override public void onFiled(String errorMsg) { //支付失敗回撥 } @Override public void onSuccess(String code, String state) { //失敗成功回撥 } }); } //開始微信支付方法 private void startWechatPay() { //去伺服器獲取簽名信息 具體返回的資訊可檢視後面的後臺開發 WXResponse response = getWXResponse(); //呼叫wechartUtils的pay方法調起微信支付。 wechartUtils.pay(this, response.appid, response.partnerid , response.packageName, response.prepayid, response.noncestr , response.timestamp, response.sign); } @Override protected void onDestroy() { //取消註冊 wechartUtils.unregisterApp(this); WXPayEntryActivity.unregistAuthBack(); super.onDestroy(); } }
-