/**
* VenueBookingFragment.java[v 1.0.0]
* class:com.bdyjy.fragment,VenueBookingFragment
* �ܺ� create at 2016-4-5 ����2:50:41
*/
package com.bdyjy.fragment;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Map;
import java.util.Random;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.Fragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import com.alibaba.fastjson.JSON;
import com.alipay.sdk.app.PayTask;
import com.bdyjy.R;
import com.bdyjy.activity.MainActivity;
import com.bdyjy.constants.HandlerOrder;
import com.bdyjy.entity.venue.CommitResultBean;
import com.bdyjy.util.OkHttpUtils;
import com.bdyjy.util.SPUtils;
import com.bdyjy.util.alipay.PayResult;
import com.bdyjy.util.alipay.SignUtils;
/**
* com.bdyjy.fragment.VenueBookingFragment
*
* @author �ܺ�<br/>
* create at 2016-4-5 ����2:50:41
*/
public class VenueBookingPayFragment extends Fragment
{
MainActivity ctx;
private Handler handler = null;
String[] timeIds = new String[2];// ʱ���id
String roomId = null;// ����id
String appDate = null;
private TextView tv_venue_name, tv_booking_date, tv_time_1, tv_time_2,
tv_fee;
private void initHandler()
{
handler = new Handler(ctx.getMainLooper())
{
@Override
public void handleMessage(Message msg)
{
switch (msg.what)
{
case HandlerOrder.TOAST:
Toast.makeText(ctx, toastMsg, Toast.LENGTH_LONG).show();
break;
case HandlerOrder.PROCESSBAR_SHOW:
ctx.showRoundProcessDialog();
break;
case HandlerOrder.PROCESSBAR_HIDE:
ctx.hideRoundProcessDialog();
break;
case HandlerOrder.SDK_PAY_FLAG:
{
PayResult payResult = new PayResult((String) msg.obj);
/**
* ͬ�����صĽ��������õ�����˽�����֤����֤�Ĺ����뿴https://doc.open.alipay.com/
* doc2/ detail.htm?spm=0.0.0.0.xdvAU6&treeId=59&articleId=
* 103665& docType=1) �����̻������첽֪ͨ
*/
String resultInfo = payResult.getResult();// ͬ��������Ҫ��֤����Ϣ
// ������Ҫ����֧�������֤������
// TODO
System.out.println(resultInfo);
String resultStatus = payResult.getResultStatus();
// �ж�resultStatus Ϊ��9000�������֧���ɹ�������״̬�������ɲο��ӿ��ĵ�
if (TextUtils.equals(resultStatus, "9000"))
{
Toast.makeText(ctx, "֧���ɹ�", Toast.LENGTH_SHORT).show();
} else
{
// �ж�resultStatus Ϊ��"9000"��������֧��ʧ��
// "8000"����֧�������Ϊ֧������ԭ�����ϵͳԭ���ڵȴ�֧�����ȷ�ϣ����ս����Ƿ�ɹ��Է�����첽֪ͨΪ��С����״̬��
if (TextUtils.equals(resultStatus, "8000"))
{
Toast.makeText(ctx, "֧�����ȷ����", Toast.LENGTH_SHORT)
.show();
} else
{
// ����ֵ�Ϳ����ж�Ϊ֧��ʧ�ܣ������û�����ȡ��֧��������ϵͳ���صĴ���
Toast.makeText(ctx, "֧��ʧ��", Toast.LENGTH_SHORT)
.show();
}
}
break;
}
}
}
};
}
// // ���������
// private Dialog mDialog;
//
// public void showRoundProcessDialog()
// {
// mDialog = new AlertDialog.Builder(ctx).create();
// mDialog.setCancelable(false);
// // mDialog.setOnKeyListener(keyListener);
// mDialog.show();
// // ע��˴�Ҫ����show֮�� ����ᱨ�쳣
// mDialog.setContentView(R.layout.loading_process_dialog_anim);
// }
//
// public void hideRoundProcessDialog()
// {
// if (null != mDialog && mDialog.isShowing())
// mDialog.hide();
// }
/**
*
*/
public VenueBookingPayFragment(MainActivity ctx)
{
this.ctx = ctx;
initHandler();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.venue_booking_pay, null);
tv_venue_name = (TextView) view.findViewById(R.id.tv_venue_name);
tv_booking_date = (TextView) view.findViewById(R.id.tv_booking_date);
tv_time_1 = (TextView) view.findViewById(R.id.tv_time_1);
tv_time_2 = (TextView) view.findViewById(R.id.tv_time_2);
tv_fee = (TextView) view.findViewById(R.id.tv_fee);
TextView tv = (TextView) view.findViewById(R.id.tv_title);
tv.setText("����Ԥ��-֧��");
view.findViewById(R.id.tv_back).setOnClickListener(
new OnClickListener()
{
@Override
public void onClick(View v)
{
showDialog();
}
});
// ���ط��ؼ������ؼ�û������
// view.findViewById(R.id.tv_back).setVisibility(View.INVISIBLE);
view.findViewById(R.id.ll_my).setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
toastMsg = "��ȡ����֧����������ڡ��ҵĶ������鿴������ж�����¼";
handler.sendEmptyMessage(HandlerOrder.TOAST);
ctx.jumpToVenueBookingMyFregment();
}
});
view.findViewById(R.id.btn_pay_now).setOnClickListener(
new OnClickListener()
{
@Override
public void onClick(View v)
{
// ֧���ύ
toastMsg = "��δ��֧ͨ���������ڴ�";
handler.sendEmptyMessage(HandlerOrder.TOAST);
// ���������֧������֧������
// TODO
// String venue_name = tv_venue_name.getText().toString();
// String date = tv_booking_date.getText().toString();
// String time_1 = tv_time_1.getText().toString();
// String time_2 = tv_time_2.getText().toString();
// String fee = tv_fee.getText().toString();
//
// if (fee.contains("��"))
// {
// fee = fee.substring(1);
// }
//
// // �������Լ�ʱ��Σ����м۸�
// pay(venue_name + "_" + date, time_1 + ";" + time_2, fee);
}
});
initData(view);
return view;
}
private void showDialog()
{
// ����һ��AlertDialog.Builder����
AlertDialog.Builder builder = new AlertDialog.Builder(ctx).setMessage(
"֧����δ��ɣ���Ҫ������ҳô").setCancelable(false);
builder.setPositiveButton("ȷ��",
new android.content.DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
ctx.jumpToFirstPageFregment();
}
});
builder.setNegativeButton("ȡ��", null);
builder.create().show();
}
private void initData(View view)
{
// ��ʼ�����棺��
Map<String, String> data = MainActivity.venueBookingResultMap;
String venueName = null;
String[] timeStrs = new String[2];
String fee = null;
int i = 0;
for (String key : data.keySet())
{
String value = data.get(key);
// 2016-04-06&1834B71BABF74EA9ABC72647CC185DE4&20:30 - 21:00
appDate = key.split("&")[0];
String timeStr = key.split("&")[2];
timeStrs[i] = timeStr;
timeIds[i] = key.split("&")[1];
i++;
// value: �����_3&30.00&A77387170AC340DF9EF81D493B54DD40
venueName = value.split("&")[0].split("_")[0];
fee = value.split("&")[1];
roomId = value.split("&")[2];
}
// ��ʼ��ֵ������
TextView tv_venue_name = (TextView) view
.findViewById(R.id.tv_venue_name);
tv_venue_name.setText(venueName);
TextView tv_booking_date = (TextView) view
.findViewById(R.id.tv_booking_date);
tv_booking_date.setText(appDate);
// ����ʱ��
TextView tv_time_1 = (TextView) view.findViewById(R.id.tv_time_1);
TextView tv_time_2 = (TextView) view.findViewById(R.id.tv_time_2);
if (timeStrs[0] != null && timeStrs[0].length() > 0)
{
tv_time_1.setText(timeStrs[0]);
} else
{
view.findViewById(R.id.ll_time_1).setVisibility(View.GONE);
}
if (timeStrs[1] != null && timeStrs[1].length() > 0)
{
tv_time_2.setText(timeStrs[1]);
} else
{
view.findViewById(R.id.ll_time_2).setVisibility(View.GONE);
}
TextView tv_fee = (TextView) view.findViewById(R.id.tv_fee);
tv_fee.setText("��" + fee);
// ��ʼ���������֮������δ��ɶ���
String order_data_from = MainActivity.order_data_from;
if (order_data_from.equals("VenueBookingMyFragment"))// ���ҵĶ�����������Ļ�������Ҫ������
{
} else if (order_data_from.equals("VenueBookingFragment"))// �ӳ���ѡ��ҳ������Ļ���Ҫ�����ɶ���
{
commitBooking();
}
}
String toastMsg;
/**
* �̣߳��ύԤ��
*
* @param id
*/
private void commitBooking()
{
new Thread()
{
@Override
public void run()
{
// ����get����
String res = null;
// ��sharePreference��ȡ��֮ǰ�洢�IJ���
String token = (String) SPUtils.get(ctx, "token", "");
String singnature = (String) SPUtils.get(ctx, "singnature", "");
String st = (String) SPUtils.get(ctx, "st", "");
StringBuffer timeStr = new StringBuffer();
if (timeIds[0] != null && timeIds[0].length() > 0)
{
timeStr.append("&timeIds[0]=" + timeIds[0]);
}
if (timeIds[1] != null && timeIds[1].length() > 0)
{
timeStr.append("&timeIds[1]=" + timeIds[1]);
}
String httpStr = "/admin/appointmenrecord/addSave.do?roomId="
+ roomId + timeStr.toString() + "&appDate=" + appDate
+ "&token=" + token + "&singnature=" + singnature
+ "&st=" + st;
System.out.println(httpStr);
try
{
handler.sendEmptyMessage(HandlerOrder.PROCESSBAR_SHOW);
res = OkHttpUtils.getInstance().doGet(ctx, httpStr);
handler.sendEmptyMessage(HandlerOrder.PROCESSBAR_HIDE);
System.out.println(res);
} catch (Exception e)
{
e.printStackTrace();
toastMsg = "��ȡ����ʧ��";
handler.sendEmptyMessage(HandlerOrder.TOAST);
return;
}
CommitResultBean bean = JSON.parseObject(res,
CommitResultBean.class);
// ����ֵ������JSON��ʽ�����ݣ���Ҫ���������
if (res.trim().length() == 0)
{
toastMsg = "��ȡ����ʧ��";
handler.sendEmptyMessage(HandlerOrder.TOAST);
return;
}
String app_result_key = bean.getApp_result_key();
if ("0".equals(app_result_key))// ���������������ŵ�����,
// ��ô��Ҫ������ȡ�����ŵı��⣬���ݣ���ʾ�ڽ�����
{
// handler.sendEmptyMessage(HandlerOrder.UPDATE_LISTVIEW);
toastMsg = "���������ɣ���֧����";
handler.sendEmptyMessage(HandlerOrder.TOAST);
} else
{
toastMsg = bean.getSystem_result_message_key();
handler.sendEmptyMessage(HandlerOrder.TOAST);
}
}
}.start();
}
// �����Ǽ���֧������ع��ܴ���
// �̻�PID
public static final String PARTNER = "2088511831644162";
// �̻��տ��˺�
public static final String SELLER = "fso@pkusz.edu.cn";
// �̻�˽Կ��pkcs8��ʽ
public static final String RSA_PRIVATE = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAMUq65KcuJZjXnqB"
+ "7GQ7hV2VTZfAiKcynD0TLYHIozoL6A3HnnA5X+dbsJk8OyqBKe6ljWzu1F/nljye"
+ "fS2CT4xPxim1wnUpPT0aMnFifqTokIlgpKsMaoNtrAUJOGsxJgB3yTXJgS28TetN"
+ "M5y5T9k6YPKrpvOTEqpz5XbwZLPvAgMBAAECgYBkrFiigMFDkLNyDBrexwF+2jhH"
+ "Tvi3noDmMn+e8ApgHio7tvxwQut0H34ZkAaJ2m/FueRvhbcMzRwKEcbK0Om6TbTH"
+ "T5zOUkuwTLIjgrEj6oRyGDPR+fgsFd7NyrwNwawDw5t4n3xtHThlhig9j3+MtGR8"
+ "qj4dabNhbkwqpoBxoQJBAPYDqNQ8pO507ZPyxmJ4DtwDmDL1YRbTU/d08JJRlbWl"
+ "CeD0jwIapvnvu0hlzlghCeu/ctCq+/aZOcjrEpB6TD8CQQDNK7Gxsz20KzNkO31G"
+ "oel2U+HGCEKEa18+rtL3dR/n9JAMWhPbOsMu/WIlhKYqD+DZfx5qlq5BJbrsb8cc"
+ "eGxRAkAwMgFWKtdI2+FbNZmF2u1WTuH3QbkWhqoRdXhXweHpZv6rcl291MjXeA6h"
+ "nUgyXa6a37O3i3e3FMeMQ/ksErlzAkAmJxjtHZK83Ue6POHH77X1XhXe+Iw+CaV1"
+ "h5cNk0cyoCTlVx7YJq5DH7VPmsI3kipS57OlSI3w6W5AJ20UW0RxAkBIHSSQmC1K"
+ "pfjLMEZYnkYrVT5tzjmrtch94dG128gqa5TNVUDmHQe+PkhggrSZmtNSzo0Bzy9p"
+ "CX5b5Avm+6qY";
// ֧������Կ
public static final String RSA_PUBLIC = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDFKuuSnLiWY156gexkO4VdlU2X"
+ "wIinMpw9Ey2ByKM6C+gNx55wOV/nW7CZPDsqgSnupY1s7tRf55Y8nn0tgk+MT8Yp"
+ "tcJ1KT09GjJxYn6k6JCJYKSrDGqDbawFCThrMSYAd8k1yYEtvE3rTTOcuU/ZOmDy"
+ "q6bzkxKqc+V28GSz7wIDAQAB";
/**
* call alipay sdk pay. ����SDK֧��
*
*/
public void pay(String goodsTitle, String description, String price)
{
if (TextUtils.isEmpty(PARTNER) || TextUtils.isEmpty(RSA_PRIVATE)
|| TextUtils.isEmpty(SELLER))
{
toastMsg = "ϵͳ��������";
handler.sendEmptyMessage(HandlerOrder.TOAST);
return;
}
// String orderInfo = getOrderInfo("���Ե���Ʒ", "�ò�����Ʒ����ϸ����", "0.01");
String orderInfo = getOrderInfo(goodsTitle, description, price);
/**
* �ر�ע�⣬�����ǩ������Ҫ���ڷ���ˣ�����˽Կй¶�ڴ����У�
*/
String sign = sign(orderInfo);
try
{
/**
* �����sign ��URL����
*/
sign = URLEncoder.encode(sign, "UTF-8");
} catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
/**
* �����ķ���֧���������淶�Ķ�����Ϣ
*/
final String payInfo = orderInfo + "&sign=\"" + sign + "\"&"
+ getSignType();
Runnable payRunnable = new Runnable()
{
@Override
public void run()
{
// ����PayTask ����
PayTask alipay = new PayTask(ctx);
// ����֧���ӿڣ���ȡ֧�����
String result = alipay.pay(payInfo, true);
Message msg = new Message();
msg.what = HandlerOrder.SDK_PAY_FLAG;
msg.obj = result;
handler.sendMessage(msg);
}
};
// �����첽����
Thread payThread = new Thread(payRunnable);
payThread.start();
}
/**
* create the order info. ����������Ϣ
*
*/
private String getOrderInfo(String subject, String body, String price)
{
// ǩԼ���������ID
String orderInfo = "partner=" + "\"" + PARTNER + "\"";
// ǩԼ����֧�����˺�
orderInfo += "&seller_id=" + "\"" + SELLER + "\"";
// �̻���վΨһ������
orderInfo += "&out_trade_no=" + "\"" + getOutTradeNo() + "\"";
// ��Ʒ����
orderInfo += "&subject=" + "\"" + subject + "\"";
// ��Ʒ����
orderInfo += "&body=" + "\"" + body + "\"";
// ��Ʒ���
orderInfo += "&total_fee=" + "\"" + price + "\"";
// �������첽֪ͨҳ��·��
orderInfo += "¬ify_url=" + "\"" + "http://notify.msp.hk/notify.htm"
+ "\"";
// ����ӿ����ƣ� �̶�ֵ
orderInfo += "&service=\"mobile.securitypay.pay\"";
// ֧�����ͣ� �̶�ֵ
orderInfo += "&payment_type=\"1\"";
// �������룬 �̶�ֵ
orderInfo += "&_input_charset=\"utf-8\"";
// ����δ����ij�ʱʱ��
// Ĭ��30���ӣ�һ����ʱ���ñʽ��ͻ��Զ����رա�
// ȡֵ��Χ��1m��15d��
// m-���ӣ�h-Сʱ��d-�죬1c-���죨���۽���ʱ����������0��رգ���
// �ò�����ֵ������С���㣬��1.5h����ת��Ϊ90m��
orderInfo += "&it_b_pay=\"30m\"";
// extern_tokenΪ���������Ȩ��ȡ����alipay_open_id,���ϴ˲����û���ʹ����Ȩ���˻�����֧��
// orderInfo += "&extern_token=" + "\"" + extern_token + "\"";
// ֧���������������ǰҳ����ת���̻�ָ��ҳ���·�����ɿ�
orderInfo += "&return_url=\"m.alipay.com\"";
// �������п�֧���������ô˲���������ǩ���� �̶�ֵ ����ҪǩԼ���������п����֧��������ʹ�ã�
// orderInfo += "&paymethod=\"expressGateway\"";
return orderInfo;
}
/**
* get the out_trade_no for an order. �����̻������ţ���ֵ���̻���Ӧ����Ψһ�����Զ����ʽ�淶��
*
*/
private String getOutTradeNo()
{
SimpleDateFormat format = new SimpleDateFormat("MMddHHmmss",
Locale.getDefault());
Date date = new Date();
String key = format.format(date);
Random r = new Random();
key = key + r.nextInt();
key = key.substring(0, 15);
return key;
}
/**
* sign the order info. �Զ�����Ϣ����ǩ��
*
* @param content
* ��ǩ��������Ϣ
*/
private String sign(String content)
{
return SignUtils.sign(content, RSA_PRIVATE);
}
/**
* get the sign type we use. ��ȡǩ����ʽ
*
*/
private String getSignType()
{
return "sign_type=\"RSA\"";
}
}