Java tutorial
/** * This file was auto-generated by the Titanium Module SDK helper for Android * Appcelerator Titanium Mobile * Copyright (c) 2009-2010 by Appcelerator, Inc. All Rights Reserved. * Licensed under the terms of the Apache Public License * Please see the LICENSE included with this distribution for details. * */ package com.mamashai.pingxx; import org.appcelerator.kroll.KrollModule; import org.appcelerator.kroll.annotations.Kroll; import org.appcelerator.titanium.util.*; import org.appcelerator.titanium.TiApplication; import org.appcelerator.kroll.common.Log; import org.appcelerator.kroll.common.TiConfig; import java.util.HashMap; import java.io.IOException; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.content.ComponentName; import android.content.Intent; import android.os.AsyncTask; import android.os.Bundle; import com.pingplusplus.android.PaymentActivity; import com.pingplusplus.android.PingppLog; import com.squareup.okhttp.MediaType; import com.squareup.okhttp.OkHttpClient; import com.squareup.okhttp.Request; import com.squareup.okhttp.RequestBody; import com.squareup.okhttp.Response; @Kroll.module(name = "PingxxAndroid", id = "com.mamashai.pingxx") public class PingxxAndroidModule extends KrollModule implements TiActivityResultHandler { // Standard Debugging variables private static final String LCAT = "PingxxAndroidModule"; private static final boolean DBG = TiConfig.LOGD; private static final int REQUEST_CODE_PAYMENT = 1; private static TiApplication _app; // You can define constants with @Kroll.constant, for example: // @Kroll.constant public static final String EXTERNAL_NAME = value; public class PingActivityHandler implements TiActivityResultHandler { public void onResult(Activity activity, int requestCode, int resultCode, Intent data) { } public void onError(Activity activity, int requestCode, Exception e) { } } public PingxxAndroidModule() { super(); } @Kroll.onAppCreate public static void onAppCreate(TiApplication app) { Log.d(LCAT, "inside onAppCreate"); _app = app; } @Kroll.method public void pay(Object arg) { HashMap<String, String> kd = (HashMap<String, String>) arg; OkHttpClient client = new OkHttpClient(); /* RequestBody formBody = new FormEncodingBuilder() .add("order_no", kd.get("order_no")) .add("amount", kd.get("amount")) .add("channel", kd.get("channel")) .build(); Request request = new Request.Builder().url(url).build(); Response response = client.newCall(request).execute(); */ String url = kd.get("url") + "?order_no=" + kd.get("order_no") + "&amount=" + kd.get("amount") + "&channel=" + kd.get("channel"); Log.d(LCAT, "url :" + url); Request request = new Request.Builder().url(url).build(); try { Response response = client.newCall(request).execute(); if (response.code() >= 200 && response.code() <= 300) { String data = response.body().string(); Log.d(LCAT, "data:" + data); Intent intent = new Intent(); String packageName = _app.getAppCurrentActivity().getPackageName(); ComponentName componentName = new ComponentName(packageName, packageName + ".wxapi.WXPayEntryActivity"); intent.setComponent(componentName); intent.putExtra(PaymentActivity.EXTRA_CHARGE, data); TiActivitySupport support = (TiActivitySupport) TiApplication.getAppCurrentActivity(); support.launchActivityForResult(intent, REQUEST_CODE_PAYMENT, this); Log.d(LCAT, "after launch Activity"); } else { HashMap<String, Object> event = new HashMap<String, Object>(); event.put("code", "error"); event.put("text", "charge" + response); fireEvent("ping_paid", event); Log.d(LCAT, "request error"); } } catch (Exception e) { } } public void onResult(Activity activity, int requestCode, int resultCode, Intent data) { //?? if (requestCode == REQUEST_CODE_PAYMENT) { if (resultCode == Activity.RESULT_OK) { String result = data.getExtras().getString("pay_result"); /* ? * "success" - payment succeed * "fail" - payment failed * "cancel" - user canceld * "invalid" - payment plugin not installed */ String errorMsg = data.getExtras().getString("error_msg"); // ? String extraMsg = data.getExtras().getString("extra_msg"); // ? HashMap<String, Object> event = new HashMap<String, Object>(); event.put("code", result); event.put("text", errorMsg); event.put("extra_text", extraMsg); fireEvent("ping_paid", event); } } } public void onError(Activity activity, int requestCode, Exception e) { } }