PageRenderTime 29ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/yueai/src/main/java/com/yueai/activity/BuyServiceActivity.java

https://bitbucket.org/juzhiwiscom/datelove
Java | 414 lines | 338 code | 37 blank | 39 comment | 42 complexity | 284d86d8b04d13b9a00a22dab0a66e86 MD5 | raw file
  1. package com.yueai.activity;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Handler;
  5. import android.text.TextUtils;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.LinearLayout;
  9. import android.widget.TextView;
  10. import com.alibaba.fastjson.JSON;
  11. import com.library.utils.DialogUtil;
  12. import com.library.utils.PayUtils;
  13. import com.library.utils.TimeUtil;
  14. import com.library.utils.ToastUtil;
  15. import com.library.utils.Util;
  16. import com.library.utils.VersionInfoUtil;
  17. import com.yueai.R;
  18. import com.yueai.base.BaseTitleActivity;
  19. import com.yueai.bean.PayWay;
  20. import com.yueai.bean.User;
  21. import com.yueai.constant.IConfigConstant;
  22. import com.yueai.constant.IUrlConstant;
  23. import com.yueai.event.UpdatePayInfoEvent;
  24. import com.yueai.payUtil.IabHelper;
  25. import com.yueai.payUtil.IabResult;
  26. import com.yueai.payUtil.Purchase;
  27. import com.yueai.utils.AppsflyerUtils;
  28. import com.yueai.utils.CommonRequestUtil;
  29. import com.yueai.xml.PlatformInfoXml;
  30. import com.yueai.xml.UserInfoXml;
  31. import com.zhy.http.okhttp.OkHttpUtils;
  32. import com.zhy.http.okhttp.callback.Callback;
  33. import com.zhy.http.okhttp.callback.StringCallback;
  34. import org.greenrobot.eventbus.EventBus;
  35. import java.util.HashMap;
  36. import java.util.Map;
  37. import butterknife.BindView;
  38. import okhttp3.Call;
  39. import okhttp3.Response;
  40. /**
  41. * 支付页面
  42. * Modified by zhangdroid on 2017/03/09.
  43. */
  44. public class BuyServiceActivity extends BaseTitleActivity implements View.OnClickListener {
  45. @BindView(R.id.ll_month)
  46. LinearLayout ll_month;
  47. @BindView(R.id.ll_bean)
  48. LinearLayout ll_bean;
  49. @BindView(R.id.month1)
  50. Button month1;
  51. @BindView(R.id.month3)
  52. Button month3;
  53. @BindView(R.id.month12)
  54. Button month12;
  55. @BindView(R.id.month1_payall)
  56. TextView month1_payall;
  57. @BindView(R.id.month3_payall)
  58. TextView month3_payall;
  59. @BindView(R.id.month12_payall)
  60. TextView month12_payall;
  61. @BindView(R.id.bean1)
  62. Button bean1;
  63. @BindView(R.id.bean2)
  64. Button bean2;
  65. @BindView(R.id.bean3)
  66. Button bean3;
  67. @BindView(R.id.beans)
  68. TextView beans;// 豆币数
  69. private String[] skus = {IConfigConstant.SKU_BEAN300, IConfigConstant.SKU_BEAN600, IConfigConstant.SKU_BEAN1500,
  70. IConfigConstant.SKU_MONTH1, IConfigConstant.SKU_MONTH3, IConfigConstant.SKU_MONTH12};
  71. private static final String INTENT_KEY_TYPE = "intent_key_type";
  72. private static final String INTENT_KEY_FROM_TAG = "intent_key_from_tag";
  73. private static final String INTENT_KEY_SERVICE_WAY = "intent_key_service_way";
  74. public static final String INTENT_FROM_INTERCEPT = "intent_key_intercept";
  75. public static final String INTENT_FROM_BEAN = "intent_key_bean";
  76. public static final String INTENT_FROM_MONTH = "intent_key_month";
  77. private IabHelper mHelper;
  78. private static final int RC_REQUEST = 10001;
  79. private boolean iap_is_ok = false;
  80. @Override
  81. protected int getLayoutResId() {
  82. return R.layout.activity_buy_service;
  83. }
  84. @Override
  85. protected String getCenterTitle() {
  86. return getString(R.string.recharge);
  87. }
  88. /**
  89. * 跳转支付页面
  90. *
  91. * @param from Activity对象
  92. * @param sourceTag 支付入口
  93. * @param serviceWay 支付类型
  94. */
  95. public static void toBuyServiceActivity(Activity from, String type, String sourceTag, String serviceWay) {
  96. Map<String, String> map = new HashMap<String, String>();
  97. map.put(INTENT_KEY_TYPE, type);
  98. map.put(INTENT_KEY_FROM_TAG, sourceTag);
  99. map.put(INTENT_KEY_SERVICE_WAY, serviceWay);
  100. Util.gotoActivity(from, BuyServiceActivity.class, false, map);
  101. }
  102. @Override
  103. protected void initViewsAndVariables() {
  104. String type = INTENT_FROM_INTERCEPT;
  105. String fromTag = "";
  106. String serviceWay = "-1";
  107. Intent intent = getIntent();
  108. if (intent != null) {
  109. type = intent.getStringExtra(INTENT_KEY_TYPE);
  110. fromTag = intent.getStringExtra(INTENT_KEY_FROM_TAG);
  111. serviceWay = intent.getStringExtra(INTENT_KEY_SERVICE_WAY);
  112. }
  113. switch (type) {
  114. case INTENT_FROM_INTERCEPT:// 支付拦截
  115. ll_bean.setVisibility(View.VISIBLE);
  116. ll_month.setVisibility(View.VISIBLE);
  117. beans.setVisibility(View.GONE);
  118. break;
  119. case INTENT_FROM_BEAN:// 豆币
  120. ll_bean.setVisibility(View.VISIBLE);
  121. ll_month.setVisibility(View.GONE);
  122. beans.setVisibility(View.VISIBLE);
  123. beans.setText(getString(R.string.beancounts, String.valueOf(UserInfoXml.getBeanCount())));
  124. break;
  125. case INTENT_FROM_MONTH:// 包月
  126. ll_bean.setVisibility(View.GONE);
  127. ll_month.setVisibility(View.VISIBLE);
  128. beans.setVisibility(View.GONE);
  129. break;
  130. }
  131. // 支付统计和支付方式
  132. getPayWay(fromTag, serviceWay);
  133. mHelper = new IabHelper(BuyServiceActivity.this, IConfigConstant.GOOGLE_APP_KEY);
  134. mHelper.enableDebugLogging(false);
  135. mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
  136. public void onIabSetupFinished(IabResult result) {
  137. if (!result.isSuccess()) {
  138. return;
  139. }
  140. iap_is_ok = true;
  141. }
  142. });
  143. }
  144. private void getPayWay(String fromTag, String serviceWay) {
  145. OkHttpUtils.post()
  146. .url(IUrlConstant.URL_PAY_WAY)
  147. .addParams("platformInfo", PlatformInfoXml.getPlatformJsonString())
  148. .addHeader("token", PlatformInfoXml.getToken())
  149. .addParams("fromTag", fromTag)
  150. .addParams("serviceWay", serviceWay)
  151. .build()
  152. .execute(new Callback<PayWay>() {
  153. @Override
  154. public PayWay parseNetworkResponse(Response response, int id) throws Exception {
  155. String resultJson = response.body().string();
  156. if (!TextUtils.isEmpty(resultJson)) {
  157. return JSON.parseObject(resultJson, PayWay.class);
  158. }
  159. return null;
  160. }
  161. @Override
  162. public void onError(Call call, Exception e, int id) {
  163. }
  164. @Override
  165. public void onResponse(PayWay response, int id) {
  166. }
  167. });
  168. }
  169. @Override
  170. protected void addListeners() {
  171. month1.setOnClickListener(this);
  172. month3.setOnClickListener(this);
  173. month12.setOnClickListener(this);
  174. bean1.setOnClickListener(this);
  175. bean2.setOnClickListener(this);
  176. bean3.setOnClickListener(this);
  177. }
  178. public void onClick(View view) {
  179. switch (view.getId()) {
  180. case R.id.bean1:
  181. // payTest(PlatformInfoXml.getPlatformInfo().getFid() + "101", "1");
  182. AppsflyerUtils.click300Bean();
  183. iapHandler.sendEmptyMessage(0);
  184. break;
  185. case R.id.bean2:
  186. AppsflyerUtils.click600Bean();
  187. iapHandler.sendEmptyMessage(1);
  188. break;
  189. case R.id.bean3:
  190. AppsflyerUtils.click1500Bean();
  191. iapHandler.sendEmptyMessage(2);
  192. break;
  193. case R.id.month1:
  194. AppsflyerUtils.click1Month();
  195. iapHandler.sendEmptyMessage(3);
  196. break;
  197. case R.id.month3:
  198. AppsflyerUtils.click3Month();
  199. iapHandler.sendEmptyMessage(4);
  200. break;
  201. case R.id.month12:
  202. AppsflyerUtils.clickOneYear();
  203. iapHandler.sendEmptyMessage(5);
  204. break;
  205. }
  206. }
  207. /**
  208. * 根据SKU购买商品
  209. *
  210. * @param sku 商品对应的SKU
  211. */
  212. private void purchase(String sku) {
  213. if (VersionInfoUtil.checkAPKExist(BuyServiceActivity.this, "com.android.vending") && mHelper != null && iap_is_ok) {
  214. mHelper.handleActivityResult(mHelper.mRequestCode, Activity.RESULT_CANCELED, null);
  215. // mHelper.launchPurchaseFlow(BuyServiceActivity.this, sku, RC_REQUEST, mPurchaseFinishedListener);
  216. mHelper.launchSubscriptionPurchaseFlow(BuyServiceActivity.this, sku, RC_REQUEST, mPurchaseFinishedListener);
  217. } else {
  218. showMessage(getString(R.string.pay_tip_message));
  219. }
  220. }
  221. Handler iapHandler = new Handler() {
  222. public void handleMessage(android.os.Message msg) {
  223. switch (msg.what) {
  224. case 0:
  225. purchase(skus[0]);
  226. break;
  227. case 1:
  228. purchase(skus[1]);
  229. break;
  230. case 2:
  231. purchase(skus[2]);
  232. break;
  233. case 3:
  234. purchase(skus[3]);
  235. break;
  236. case 4:
  237. purchase(skus[4]);
  238. break;
  239. case 5:
  240. purchase(skus[5]);
  241. break;
  242. }
  243. }
  244. };
  245. IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
  246. public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
  247. if (result.isFailure()) {
  248. return;
  249. }
  250. if (purchase.getSku().equals(skus[0])) {
  251. pay(purchase, PlatformInfoXml.getPlatformInfo().getFid() + "101", "2");
  252. mHelper.consumeAsync(purchase, mConsumeFinishedListener);
  253. } else if (purchase.getSku().equals(skus[1])) {
  254. pay(purchase, PlatformInfoXml.getPlatformInfo().getFid() + "102", "2");
  255. mHelper.consumeAsync(purchase, mConsumeFinishedListener);
  256. } else if (purchase.getSku().equals(skus[2])) {
  257. pay(purchase, PlatformInfoXml.getPlatformInfo().getFid() + "103", "2");
  258. mHelper.consumeAsync(purchase, mConsumeFinishedListener);
  259. } else if (purchase.getSku().equals(skus[3])) {
  260. pay(purchase, PlatformInfoXml.getPlatformInfo().getFid() + "002", "1");
  261. mHelper.consumeAsync(purchase, mConsumeFinishedListener);
  262. } else if (purchase.getSku().equals(skus[4])) {
  263. pay(purchase, PlatformInfoXml.getPlatformInfo().getFid() + "001", "1");
  264. mHelper.consumeAsync(purchase, mConsumeFinishedListener);
  265. } else if (purchase.getSku().equals(skus[5])) {
  266. pay(purchase, PlatformInfoXml.getPlatformInfo().getFid() + "003", "1");
  267. mHelper.consumeAsync(purchase, mConsumeFinishedListener);
  268. }
  269. }
  270. };
  271. // Called when consumption is complete
  272. IabHelper.OnConsumeFinishedListener mConsumeFinishedListener = new IabHelper.OnConsumeFinishedListener() {
  273. public void onConsumeFinished(Purchase purchase, IabResult result) {
  274. if (result.isSuccess()) {
  275. updateUserInfo();
  276. if (purchase.getSku().equals(skus[0])) {
  277. showMessage(getString(R.string.pay_bean300_success));
  278. AppsflyerUtils.purchase300Bean();
  279. } else if (purchase.getSku().equals(skus[1])) {
  280. showMessage(getString(R.string.pay_bean600_success));
  281. AppsflyerUtils.purchase600Bean();
  282. } else if (purchase.getSku().equals(skus[2])) {
  283. showMessage(getString(R.string.pay_bean1500_success));
  284. AppsflyerUtils.purchase1500Bean();
  285. } else if (purchase.getSku().equals(skus[3])) {
  286. showMessage(getString(R.string.pay_month1_success));
  287. AppsflyerUtils.purchase1Month();
  288. } else if (purchase.getSku().equals(skus[4])) {
  289. showMessage(getString(R.string.pay_month3_success));
  290. AppsflyerUtils.purchase3Month();
  291. } else if (purchase.getSku().equals(skus[5])) {
  292. showMessage(getString(R.string.pay_month12_success));
  293. AppsflyerUtils.purchaseOneYear();
  294. }
  295. }
  296. }
  297. };
  298. private void updateUserInfo() {
  299. CommonRequestUtil.loadUserInfo(new CommonRequestUtil.OnLoadUserInfoListener() {
  300. @Override
  301. public void onSuccess(User user) {
  302. // 更新用户支付信息
  303. EventBus.getDefault().post(new UpdatePayInfoEvent());
  304. }
  305. @Override
  306. public void onFail() {
  307. }
  308. });
  309. }
  310. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  311. if (!mHelper.handleActivityResult(requestCode, resultCode, data)) {
  312. super.onActivityResult(requestCode, resultCode, data);
  313. }
  314. }
  315. @Override
  316. public void onDestroy() {
  317. super.onDestroy();
  318. if (mHelper != null)
  319. mHelper.dispose();
  320. mHelper = null;
  321. }
  322. private void showMessage(String message) {
  323. DialogUtil.showSingleBtnDialog(getSupportFragmentManager(), getString(R.string.pay_tip), message, getString(R.string.sure), true, null);
  324. }
  325. private void pay(final Purchase purchase, final String serviceid, final String payType) {
  326. OkHttpUtils.post()
  327. .url(IUrlConstant.GOOGLE_PAY)
  328. .addParams("platformInfo", PlatformInfoXml.getPlatformJsonString())
  329. .addParams("token", PlatformInfoXml.getToken())
  330. .addParams("transactionId", purchase.getOrderId())
  331. .addParams("payTime", purchase.getPurchaseTime() + "")
  332. .addParams("clientTime", TimeUtil.getCurrentTime())
  333. .addParams("userGoogleId", " ")
  334. .addParams("payType", payType)
  335. .addParams("googleId", purchase.getPackageName())
  336. .addParams("serviceId", serviceid)
  337. .addParams("userId", UserInfoXml.getUID())
  338. .addParams("transactionToken", PayUtils.encryptPay(PlatformInfoXml.getPlatformInfo().getPid(), purchase.getOrderId()))
  339. //后台验证支付和续费使用 具体key 跟后台协商
  340. .addParams("purchaseToken", purchase.getToken())
  341. .addParams("packageName", purchase.getPackageName())
  342. .addParams("productId", purchase.getSku())
  343. .build()
  344. .execute(new PayCallback());
  345. }
  346. /* private void payTest(final String serviceid, final String payType) {
  347. OkHttpUtils.post()
  348. .url(IUrlConstant.GOOGLE_PAY)
  349. .addParams("platformInfo", PlatformInfoXml.getPlatformJsonString())
  350. .addParams("token", PlatformInfoXml.getToken())
  351. .addParams("transactionId", "GPA.1234-5678-9012-34567")
  352. .addParams("payTime", System.currentTimeMillis() + "")
  353. .addParams("clientTime", TimeUtil.getCurrentTime())
  354. .addParams("userGoogleId", "123")
  355. .addParams("googleId", "123")
  356. .addParams("payType", payType)
  357. .addParams("serviceId", serviceid)
  358. .addParams("userId", UserInfoXml.getUID())
  359. .addParams("transactionToken", PayUtils.encryptPay(PlatformInfoXml.getPlatformInfo().getPid(), "GPA.1234-5678-9012-34567"))
  360. .build()
  361. .execute(new PayCallback());
  362. }*/
  363. public class PayCallback extends StringCallback {
  364. @Override
  365. public void onError(Call call, Exception e, int id) {
  366. ToastUtil.showShortToast(BuyServiceActivity.this, getString(R.string.pay_fail) + e.toString());
  367. }
  368. @Override
  369. public void onResponse(String response, int id) {
  370. if (!TextUtils.isEmpty(response) && "success".equals(response)) {
  371. ToastUtil.showShortToast(BuyServiceActivity.this, getString(R.string.pay_success));
  372. } else {
  373. ToastUtil.showShortToast(BuyServiceActivity.this, getString(R.string.pay_fail));
  374. }
  375. }
  376. }
  377. }