WXPayEntryActivity.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package com.wsj.Imgdt3;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.util.Log;
  7. import android.widget.Toast;
  8. import com.tencent.mm.opensdk.constants.ConstantsAPI;
  9. import com.tencent.mm.opensdk.modelbase.BaseReq;
  10. import com.tencent.mm.opensdk.modelbase.BaseResp;
  11. import com.tencent.mm.opensdk.openapi.IWXAPI;
  12. import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
  13. import com.tencent.mm.opensdk.openapi.WXAPIFactory;
  14. import com.unity3d.player.UnityPlayer;
  15. public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler{
  16. public static String GameObjectName;
  17. public static String CallBackFuncName;
  18. private static final String TAG = "WXPayEntryActivity";
  19. private IWXAPI api;
  20. public static String wxAPPID = "wxd9772f42f126413f"; // 在调用微信支付处,设置该变量值
  21. @Override
  22. public void onCreate(Bundle savedInstanceState)
  23. {
  24. super.onCreate(savedInstanceState);
  25. // setContentView(R.layout.wx_pay_result);
  26. //
  27. api = WXAPIFactory.createWXAPI(this, wxAPPID);
  28. api.handleIntent(getIntent(), this);
  29. //UnityPlayer.UnitySendMessage(GameObjectName,CallBackFuncName,"微信API_ING");
  30. }
  31. @Override
  32. protected void onNewIntent(Intent intent)
  33. {
  34. super.onNewIntent(intent);
  35. setIntent(intent);
  36. api.handleIntent(intent, this);
  37. }
  38. @Override
  39. public void onReq(BaseReq req)
  40. {
  41. }
  42. /** 微信返回支付结果,会调用该函数*/
  43. @Override
  44. public void onResp(BaseResp resp)
  45. {
  46. showText("onPayFinish, errCode = " + resp.errCode);
  47. if (resp.errCode != 0 && resp.errCode != -2)
  48. {
  49. showToast(this, "微信支付失败, errCode:" + resp.errCode);
  50. }
  51. if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX)
  52. {
  53. // AlertDialog.Builder builder = new AlertDialog.Builder(this);
  54. // builder.setTitle("提示");
  55. // builder.setMessage("微信支付结果:" + resp.errCode);
  56. // builder.show();
  57. if(resp.errCode == 0)
  58. {
  59. // PaySuccess(this); // 可在此处,添加应用自己的支付结果统计相关逻辑
  60. showToast(this, "微信支付成功");
  61. }
  62. else if(resp.errCode == -2) showToast(this, "用户取消支付");
  63. else showToast(this, "支付失败,其他异常情形" );
  64. }
  65. showToast(this, GameObjectName + "," + CallBackFuncName );
  66. UnityPlayer.UnitySendMessage(GameObjectName, CallBackFuncName, "" + resp.errCode);
  67. this.finish();
  68. }
  69. /** 输出log信息 */
  70. public static void showText(final String info)
  71. {
  72. Log.d(TAG, info);
  73. }
  74. /** 输出Toast消息 */
  75. private void showToast(Context context, final String info)
  76. {
  77. Toast.makeText(context, info, Toast.LENGTH_SHORT).show();
  78. Log.d(TAG, info);
  79. }
  80. }