Android 集成微信支付和支付宝
来源:互联网 发布:linux 显示执行过程 编辑:程序博客网 时间:2024/05/21 20:26
最近比较闲,公司项目更换后台,于是自己来研究微信支付和支付宝支付,把自己学习的过程写下来,以备以后查看。
注:要集成微信支付和支付宝功能,必须要有以下几个配置信息,而这写信息需要公司去微信支付和支付宝开放平台申请并提供给开发者,当然自己也可以去申请,这里作者用的是公司提供的,这里不纠结这些过程。获得这些信息以后
将配置信息放到一个静态类中,以共统一使用,但是处于安全考虑,微信与支付宝推荐这些数据放到服务器,这里作者把他们都放在前端,整个过程都是前端处理,实际开发尽量预处理订单生成放到后端处理。 1.首先需要导入微信jar包,从开放平台可以下载到,加入到libs目录即可
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public
class
ParameterConfig {
public
static
final
String GANHOST =
"http://101.226.197.11"
; //服务器地址ip(根据自己替换)
/**
* 微信
*/
<span style=
"white-space:pre"
> </span>
//appid
public
static
final
String WX_APP_ID =
""
;
// 自己填写自己项目的
// 商户号
public
static
final
String WX_MCH_ID =
""
;
// 自己填写自己项目的
// API密钥,在商户平台设置
public
static
final
String WX_API_KEY =
""
;
// 自己填写自己项目的
//服务器回调接口
public
static
final
String WX_notifyUrl = GANHOST+
"/service/orderComplete"
;
// 用于微信支付成功的回调(按自己需求填写)
/**
* 支付宝
*/
// 商户PID
public
static
final
String PARTNER =
""
;
//自己填写自己项目的
// 商户收款账号
public
static
final
String SELLER =
""
;
//自己填写自己项目的
// 商户私钥,pkcs8格式
public
static
final
String RSA_PRIVATE =
""
;
//自己填写自己项目的
public
static
final
String aliPay_notifyURL = GANHOST+
"/service/alipay/orderComplete"
;
//支付宝支付成功的回调
}
1.微信支付的集成前提条件
1.首先需要导入微信jar包,从开放平台可以下载到,加入到libs目录即可
2.配置manifest
1
a.用户权限
1
2
3
<uses-permission android:name=
"android.permission.INTERNET"
>
<uses-permission android:name=
"android.permission.MODIFY_AUDIO_SETTINGS"
>
<uses-permission android:name=
"android.permission.WRITE_EXTERNAL_STORAGE"
> </uses-permission></uses-permission></uses-permission>
b.activity配置,这里com.gan.mypay改成自己的包名(如果自己包名与src下的package 名不一样,这里要的是在manifest中配置的名称,同样需要在src建立以自己包 名为路劲的package,一定确保有这个activity)这个activity是微信支付结果要回调的activty
<activity
android:name="com.gan.mypay.wxapi.WXPayEntryActivity"
android:exported="true"
android:launchMode="singleTop"/>1.首先需要导入微信jar包,从开放平台可以下载到,加入到libs目录即可
android:name="com.gan.mypay.wxapi.WXPayEntryActivity"
android:exported="true"
android:launchMode="singleTop"/>
2.支付宝支付的集成前提条件
1.首先需要导入微信jar包,从开放平台可以下载到,加入到libs目录即可
2.配置manifest
a.用户权限
<activity
android:name="com.alipay.sdk.app.H5PayActivity"
android:configChanges="orientation|keyboardHidden|navigation"
android:exported="false"
android:screenOrientation="behind" >
<activity
android:name="com.alipay.sdk.auth.AuthActivity"
android:configChanges="orientation|keyboardHidden|navigation"
android:exported="false"
android:screenOrientation="behind" >
1
2
3
<uses-permission android:name=
"android.permission.INTERNET"
>
<uses-permission android:name=
"android.permission.MODIFY_AUDIO_SETTINGS"
>
<uses-permission android:name=
"android.permission.WRITE_EXTERNAL_STORAGE"
></uses-permission></uses-permission></uses-permission>
b.activity配置这里必须要这么配置
<activity
android:name="com.alipay.sdk.app.H5PayActivity"
android:configChanges="orientation|keyboardHidden|navigation"
android:exported="false"
android:screenOrientation="behind" >
<activity
android:name="com.alipay.sdk.auth.AuthActivity"
android:configChanges="orientation|keyboardHidden|navigation"
android:exported="false"
android:screenOrientation="behind" >
3.代码集成
1.首先要有个商品页面MainActivity,用来手机收集商品信息,这里需要后台交互生成订单,我偷懒就直接在页面生成了假订单
MainActivity.java(这里用了xutils的注入)
WXpayUtil.java AlipayUtil.java WXPayEntryActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
@ContentView
(R.layout.activity_main)
public
class
MainActivity
extends
Activity {
private
Goods goods;
private
String username;
private
String mobile;
private
String adress;
private
int
count;
@ViewInject
(R.id.product_ordsubmit_username)
private
TextView usernameTV;
@ViewInject
(R.id.product_ordsubmit_phone)
private
TextView phoneTV;
@ViewInject
(R.id.product_ordsubmit_adress)
private
TextView adressTV;
@ViewInject
(R.id.product_ordsubmit_desc)
private
TextView descTV;
@ViewInject
(R.id.product_ordsubmit_price)
private
TextView priceTV;
@ViewInject
(R.id.product_ordsubmit_intg)
private
TextView intgTV;
@ViewInject
(R.id.product_ordsubmit_count1)
private
TextView countTV1;
@ViewInject
(R.id.product_ordsubmit_count)
private
TextView countTV;
@ViewInject
(R.id.product_ordsubmit_intgtotal1)
private
TextView intgtotal1TV;
@ViewInject
(R.id.product_ordsubmit_intgtotal2)
private
TextView intgtotal2TV;
@ViewInject
(R.id.product_ordsubmit_pricetotal1)
private
TextView pricetotal1TV;
@ViewInject
(R.id.product_ordsubmit_pricetotal2)
private
TextView pricetotal2TV;
@ViewInject
(R.id.product_ordsubmit_counttotal)
private
TextView counttotalTV;
@ViewInject
(R.id.product_ordsubmit_ok)
private
Button okBtn;
@ViewInject
(R.id.product_ordsubmit_say_et)
private
TextView sayEt;
@ViewInject
(R.id.product_ordsubmit_img)
private
ImageView img;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
ViewUtils.inject(
this
);
goods =
new
Goods();
goods.costprice=
100
;
goods.productid=
692356222
;
goods.producttypeid=
11
;
goods.productname=
"测试商品"
;
goods.discountprice=
0.01
;
goods.productdescription=
"商品描述"
;
goods.companydesc=
"测试商户简单描述"
;
goods.comanyadress=
"商户地址未知"
;
goods.companyname=
"测试商户"
;
goods.score=
1
;
goods.status=
1
;
goods.stock=
300
;
count=
1
;
initData();
initView();
}
private
void
initData() {
username =
"客户名称"
;
mobile =
"13800380038"
;
adress=
"客户地址"
;
}
private
void
initView() {
usernameTV.setText(
"收货人:"
+username);
phoneTV.setText(mobile+
""
);
adressTV.setText(adress);
descTV.setText(goods.productdescription);
priceTV.setText(
"¥"
+goods.discountprice);
intgTV.setText(
"积分:"
+goods.score);
countTV1.setText(
"X"
+count);
countTV.setText(count+
""
);
intgtotal1TV.setText(
"共得到"
+count*goods.score+
"积分"
);
intgtotal2TV.setText(
"积分:"
+count*goods.score);
counttotalTV.setText(
"共"
+count+
"件"
);
pricetotal1TV.setText(
"¥"
+Arith.mul(goods.discountprice, count));
pricetotal2TV.setText(
"¥"
+Arith.mul(goods.discountprice, count));
//ImageLoader.getInstance().displayImage(goods.pic1, img);
}
/**
* 增加数量
* @param v
*/
@OnClick
(R.id.product_ordsubmit_count_add)
public
void
add(View v) {
count++;
countTV1.setText(
"X"
+count);
countTV.setText(count+
""
);
intgtotal1TV.setText(
"共得到"
+count*goods.score+
"积分"
);
intgtotal2TV.setText(
"积分:"
+count*goods.score);
counttotalTV.setText(
"共"
+count+
"件"
);
pricetotal1TV.setText(
"¥"
+Arith.mul(goods.discountprice, count));
pricetotal2TV.setText(
"¥"
+Arith.mul(goods.discountprice, count));
}
/**
* 减少数量
* @param v
*/
@OnClick
(R.id.product_ordsubmit_count_sub)
public
void
sub(View v) {
if
(count>
1
) {
count--;
countTV1.setText(
"X"
+count);
countTV.setText(count+
""
);
intgtotal1TV.setText(
"共得到"
+count*goods.score+
"积分"
);
intgtotal2TV.setText(
"积分:"
+count*goods.score);
counttotalTV.setText(
"共"
+count+
"件"
);
pricetotal1TV.setText(
"¥"
+Arith.mul(goods.discountprice, count));
pricetotal2TV.setText(
"¥"
+Arith.mul(goods.discountprice, count));
}
}
/**
* 提交订单
* @param v
*/
@OnClick
(R.id.product_ordsubmit_ok)
public
void
submit(View v) {
final
OrderInfo orderInfo=
new
OrderInfo();
orderInfo.userid=
13752
;
orderInfo.areacode=
23
;
orderInfo.buildno=
"10"
;
orderInfo.roomno=
"1001"
;
orderInfo.producttypeid=goods.producttypeid;
orderInfo.productid=goods.productid;
orderInfo.amount=goods.discountprice;
//单价
orderInfo.account=count;
//数量
orderInfo.totalamount=Arith.mul(goods.discountprice, count);
//double offsetamount;//抵扣金额
orderInfo.score=count*goods.score;
//int assessitem;//评价项
//int assesslevel;//评价级别
//String assesscontent;//评价内容
//long payid=;//支付编号
orderInfo.status=
2
;
//支付状态待付款
orderInfo.type=
11
;
//日用品
orderInfo.usermemo =sayEt.getText().toString();
//业主备注
orderInfo.address =adress;
orderInfo.productname =goods.productname;
//
orderInfo.desccontext =goods.productdescription;
//
orderInfo.outtradeno=System.currentTimeMillis()+
""
+orderInfo.userid;
orderInfo.merchantid=goods.companyid;
submitorder(orderInfo);
}
/**
* 订单提交成功,进入付款界面
* @param orderInfo
* @return
*/
private
void
submitorder(OrderInfo orderInfo) {
Intent intent=
new
Intent(
this
, SelectPayTypeActivity.
class
);
intent.putExtra(
"data"
, orderInfo);
startActivity(intent);
}
}
1
<span style=
"white-space:pre"
> </span>activty_main.xml
1
<!--?xml version="1.0" encoding="utf-8"?--><linearlayout android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"> <scrollview android:layout_height="0dp" android:layout_width="match_parent" android:background="@color/igray" android:layout_weight="1"> <linearlayout android:layout_height="wrap_content" android:layout_width="match_parent" android:orientation="vertical"> <linearlayout android:layout_height="wrap_content" android:layout_width="match_parent" android:orientation="vertical" android:background="@color/white" android:paddingbottom="10dp" android:paddingleft="15dp" android:paddingright="15dp" android:paddingtop="10dp"> <linearlayout android:layout_height="wrap_content" android:layout_width="match_parent" android:orientation="horizontal"> <textview android:id="@+id/product_ordsubmit_username" android:layout_height="wrap_content" android:layout_width="0dp" android:layout_weight="1" android:gravity="center_vertical" android:text="收货人:" android:textcolor="@color/black" android:textsize="14sp"> <textview android:id="@+id/product_ordsubmit_phone" android:layout_height="wrap_content" android:layout_width="0dp" android:layout_weight="1" android:gravity="center_vertical|right" android:text="13862325641" android:textcolor="@color/igray_et" android:textsize="14sp"> </textview></textview></linearlayout> <linearlayout android:layout_height="wrap_content" android:layout_width="match_parent" android:orientation="vertical"> <textview android:layout_height="wrap_content" android:layout_width="match_parent" android:gravity="center_vertical" android:text="收货地址:" android:textcolor="@color/igray_et" android:textsize="14sp"> <textview android:id="@+id/product_ordsubmit_adress" android:layout_height="wrap_content" android:layout_width="match_parent" android:text="上海市徐汇区浦北路中星城15号" android:textcolor="@color/igray_et" android:textsize="14sp" android:drawableright="@drawable/next"> </textview></textview></linearlayout> </linearlayout> <view android:layout_height="5dp" android:layout_width="match_parent" android:background="@color/igray"> <linearlayout android:layout_height="wrap_content" android:layout_width="match_parent" android:orientation="vertical" android:background="@color/white" android:paddingleft="15dp" android:paddingright="15dp"> <linearlayout android:layout_height="wrap_content" android:layout_width="match_parent" android:orientation="horizontal"> <imageview android:id="@+id/product_ordsubmit_img" android:src="@drawable/user_head" android:layout_height="100dp" android:layout_width="100dp" android:scaletype="centerCrop"> <linearlayout android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical"> <textview android:id="@+id/product_ordsubmit_desc" android:layout_height="0dp" android:layout_width="match_parent" android:layout_weight="1" android:text="商品描述" android:textcolor="@color/black" android:textsize="14sp" android:layout_margin="10dp"> <linearlayout android:layout_height="wrap_content" android:layout_width="match_parent" android:orientation="horizontal"> <textview android:id="@+id/product_ordsubmit_price" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="¥49" android:textcolor="@color/Orange" android:textsize="18sp" android:layout_margin="5dp"> <textview android:id="@+id/product_ordsubmit_intg" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="积分:2" android:textcolor="@color/igray_et" android:textsize="14sp" android:layout_margin="5dp"> <view android:layout_height="0dp" android:layout_width="0dp" android:layout_weight="1"> <textview android:id="@+id/product_ordsubmit_count1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="X2" android:textcolor="@color/igray_et" android:textsize="14sp" android:layout_margin="5dp"> </textview></view></textview></textview></linearlayout> </textview></linearlayout> </imageview></linearlayout> <linearlayout android:layout_height="wrap_content" android:layout_width="match_parent" android:orientation="horizontal" android:paddingbottom="5dp" android:paddingtop="5dp" android:gravity="center_vertical"> <textview android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="购买数量" android:textcolor="@color/black" android:textsize="14sp"> <view android:layout_height="match_parent" android:layout_width="0dp" android:layout_weight="1"> <imageview android:id="@+id/product_ordsubmit_count_sub" android:src="@drawable/sub_orange" android:layout_height="25dp" android:layout_width="25dp"> <textview android:id="@+id/product_ordsubmit_count" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="3" android:textcolor="@color/black" android:textsize="18sp" android:layout_marginleft="10dp" android:layout_marginright="10dp"> <imageview android:id="@+id/product_ordsubmit_count_add" android:src="@drawable/add_orange" android:layout_height="25dp" android:layout_width="25dp"> </imageview></textview></imageview></view></textview></linearlayout> <linearlayout android:layout_height="wrap_content" android:layout_width="match_parent" android:orientation="horizontal" android:paddingbottom="5dp" android:paddingtop="5dp" android:gravity="center_vertical" android:focusableintouchmode="true"> <textview android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="业主留言:" android:textcolor="@color/black" android:textsize="14sp"> <edittext android:id="@+id/product_ordsubmit_say_et" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textcolor="@color/igray_et" android:textsize="14sp" android:layout_marginleft="10dp" android:layout_marginright="10dp" android:hint="选填,可以填你对卖家一致达成的要求" android:singleline="true"> </edittext></textview></linearlayout> <linearlayout android:layout_height="wrap_content" android:layout_width="match_parent" android:orientation="horizontal" android:paddingbottom="5dp" android:paddingtop="5dp" android:gravity="center_vertical"> <textview android:id="@+id/product_ordsubmit_intgtotal1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="共得到6积分" android:textcolor="@color/igray_et" android:textsize="14sp"> <view android:layout_height="match_parent" android:layout_width="0dp" android:layout_weight="1"> <textview android:id="@+id/product_ordsubmit_counttotal" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="共3件" android:textcolor="@color/black" android:textsize="14sp"> <textview android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="合计:" android:textcolor="@color/black" android:textsize="14sp" android:layout_marginleft="10dp"> <textview android:id="@+id/product_ordsubmit_pricetotal1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="¥127" android:textcolor="@color/Orange" android:textsize="18sp"> </textview></textview></textview></view></textview></linearlayout> </linearlayout> </view></linearlayout> </scrollview> <linearlayout android:layout_height="44dp" android:layout_width="match_parent" android:background="@color/white" android:paddingbottom="5dp" android:paddingleft="15dp" android:paddingright="15dp" android:paddingtop="5dp" android:gravity="right|center_vertical"> <textview android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="合计:" android:textcolor="@color/black" android:textsize="14sp"> <textview android:id="@+id/product_ordsubmit_pricetotal2" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="¥127" android:textcolor="@color/Orange" android:textsize="14sp"> <textview android:id="@+id/product_ordsubmit_intgtotal2" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="积分:6" android:textcolor="@color/igray_et" android:textsize="12sp" android:layout_marginleft="10dp" android:layout_marginright="10dp"><button android:id="@+id/product_ordsubmit_ok" android:layout_height="wrap_content" android:layout_width="wrap_content" android:background="@drawable/shape_btn_oval_orange_bg2" android:text="确认"></button></textview></textview></textview></linearlayout></linearlayout>
2.在mainactivty中点击确认按钮调用支付方式选择页面SelectPayTypeActivity,用来发起支付选择
1
SelectPayTypeActivity.java
1
@ContentView(R.layout.activity_select_pay_type) public class SelectPayTypeActivity extends Activity { @ViewInject(R.id.paytype_of_weixin_ck) private CheckBox weixinCK; @ViewInject(R.id.paytype_of_zhifubao_ck) private CheckBox zhifubaoCK; @ViewInject(R.id.last_pay_count_tv) private TextView payCountTv; private OrderInfo orderInfo; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ViewUtils.inject(this); initData(); initView(); } private void initView() { payCountTv.setText("¥"+orderInfo.totalamount); } private void initData() { orderInfo=(OrderInfo) getIntent().getSerializableExtra("data"); } @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); int code=intent.getIntExtra("result", 1); switch (code) { case 0://表示成功 finish(); break; case -1://表示失败 finish(); break; case -2:////表示取消 finish(); break; case 1://未知不做处理 break; default: break; } } @OnClick(R.id.paytype_of_weixin) public void wx(View v) { if (zhifubaoCK.isChecked()) { zhifubaoCK.setChecked(false); } if (!weixinCK.isChecked()) { weixinCK.setChecked(true); } } @OnClick(R.id.paytype_of_zhifubao) public void zfb(View v) { if (weixinCK.isChecked()) { weixinCK.setChecked(false); } if (!zhifubaoCK.isChecked()) { zhifubaoCK.setChecked(true); } } @OnClick(R.id.paytype_of_weixin_ck) public void wxck(View v) { if (zhifubaoCK.isChecked()) { zhifubaoCK.setChecked(false); } if (!weixinCK.isChecked()) { weixinCK.setChecked(true); } } @OnClick(R.id.paytype_of_zhifubao_ck) public void zfbck(View v) { if (weixinCK.isChecked()) { weixinCK.setChecked(false); } if (!zhifubaoCK.isChecked()) { zhifubaoCK.setChecked(true); } } @OnClick(R.id.btn_pay_submit) public void paysubmit(View v) { if (weixinCK.isChecked()) { if (!isWeixinAvilible(this)) { Toast.makeText(this, "请先安装微信或者选择其他支付方式",0).show(); return; } //微信支付 payByWX(handler,orderInfo); }else{ if (!isZfbAvilible(this)) { Toast.makeText(this, "请先安支付宝或者选择其他支付方式",0).show(); return; } //支付宝支付 payByzfb(handler,orderInfo); } } Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what) { case 9000://支付宝支付成功 Intent it; finish(); break; case 8000://支付宝支付失败 finish(); break; default: break; } } }; /** * 调用支付宝支付 * @param handler * @param order */ private void payByzfb(Handler handler, OrderInfo order) { AlipayUtil alipay=new AlipayUtil(this,order,handler); } /** * 调用微信支付 * @param handler * @param orderInfo2 */ private void payByWX(Handler handler, OrderInfo order) { WXpayUtil wxpay=new WXpayUtil(this,order); } /** * 检查微信是否存在 * @param context * @return */ public boolean isWeixinAvilible(Context context) { PackageManager packageManager = context.getPackageManager();// 获取packagemanager List<packageinfo> pinfo = packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息 if (pinfo != null) { for (int i = 0; i < pinfo.size(); i++) { String pn = pinfo.get(i).packageName; System.out.println(pinfo.get(i).packageName); if (pn.equals("com.tencent.mm")) { return true; } } } return false; } /** * 检查支付包是否存在 * @param context * @return */ private boolean isZfbAvilible(Context context) { PackageManager packageManager = context.getPackageManager();// 获取packagemanager List<packageinfo> pinfo = packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息 if (pinfo != null) { for (int i = 0; i < pinfo.size(); i++) { String pn = pinfo.get(i).packageName; System.out.println(pinfo.get(i).packageName); if (pn.equals("com.alipay.android.app")) { return true; } } } return false; } }</packageinfo></packageinfo>activty_selecte_pay_type.xml
1
1
<!--?xml version="1.0" encoding="utf-8"?--><linearlayout android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android" android:background="@color/white"> <linearlayout android:layout_height="wrap_content" android:layout_width="match_parent" android:orientation="horizontal" android:gravity="center_vertical" android:padding="10dp"> <textview android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="需支付:" android:textcolor="@color/black" android:textsize="18sp" android:layout_marginleft="10dp"> <textview android:id="@+id/last_pay_count_tv" android:layout_height="wrap_content" android:layout_width="match_parent" android:text="¥152" android:textcolor="@color/Orange" android:textsize="22sp" android:layout_marginleft="10dp"> </textview></textview></linearlayout> <view android:layout_height="5dp" android:layout_width="match_parent" android:background="@color/igray"> <linearlayout android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" android:paddingbottom="10dp" android:paddingleft="10dp" android:paddingright="10dp"> <linearlayout android:id="@+id/paytype_of_weixin" android:layout_height="wrap_content" android:layout_width="match_parent" android:orientation="horizontal" android:gravity="center_vertical" android:padding="10dp"> <imageview android:src="@drawable/icon64_wx" android:layout_height="44dp" android:layout_width="44dp"> <textview android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="微信支付" android:textsize="20sp" android:layout_marginleft="10dp" android:layout_marginright="10dp"> <view android:layout_height="match_parent" android:layout_width="0dp" android:layout_weight="1"> <checkbox android:id="@+id/paytype_of_weixin_ck" android:layout_height="wrap_content" android:layout_width="wrap_content" android:checked="true" android:focusable="false"> </checkbox></view></textview></imageview></linearlayout> <view android:layout_height="1dp" android:layout_width="match_parent" android:background="@color/igray"> <linearlayout android:id="@+id/paytype_of_zhifubao" android:layout_height="wrap_content" android:layout_width="match_parent" android:orientation="horizontal" android:gravity="center_vertical" android:padding="10dp"> <imageview android:src="@drawable/zfb_icon_120" android:layout_height="44dp" android:layout_width="44dp"> <textview android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="支付宝" android:textsize="20sp" android:layout_marginleft="10dp" android:layout_marginright="10dp"> <view android:layout_height="match_parent" android:layout_width="0dp" android:layout_weight="1"> <checkbox android:id="@+id/paytype_of_zhifubao_ck" android:layout_height="wrap_content" android:layout_width="wrap_content" android:focusable="false"> </checkbox></view></textview></imageview></linearlayout> <view android:layout_height="1dp" android:layout_width="match_parent" android:background="@color/igray"> <relativelayout android:layout_height="match_parent" android:layout_width="match_parent"><button android:id="@+id/btn_pay_submit" android:layout_height="40dp" android:layout_width="match_parent" android:background="@drawable/shape_btn_oval_orange_bg2" android:text="支付" android:textcolor="@color/white" android:layout_marginleft="40dp" android:layout_marginright="40dp" android:layout_alignparentbottom="true" android:layout_marginbottom="10dp"></button></relativelayout></view></view></linearlayout></view></linearlayout>
3.根据支付方式调用对应工具类微信(WXpayUtil),支付宝(AlipayUtil)
1
1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
public
class
WXpayUtil {
private
IWXAPI api;
private
OrderInfo order;
private
Context context;
private
PayReq req;
private
Map<string,string> resultunifiedorder;
private
static
final
String TAG =
"ewuye.online.SelectPayTypeActivity"
;
public
WXpayUtil(Context mcontext,OrderInfo order){
//初始化微信支付
this
.order=order;
this
.context=mcontext;
if
(TextUtils.isEmpty(ParameterConfig.WX_APP_ID) || TextUtils.isEmpty(ParameterConfig.WX_MCH_ID) || TextUtils.isEmpty(ParameterConfig.WX_API_KEY)) {
new
AlertDialog.Builder(context).setTitle(
"警告"
).setMessage(
"需要配置WX_APP_ID | WX_MCH_ID| WX_API_KEY\n请到ParameterConfig.java里配置"
)
.setPositiveButton(
"确定"
,
new
DialogInterface.OnClickListener() {
public
void
onClick(DialogInterface dialoginterface,
int
i) {
//
((Activity)context).finish();
}
}).show();
return
;
}
api = WXAPIFactory.createWXAPI(context,
null
);
req =
new
PayReq();
//生成prepay_id
GetPrepayIdTask getPrepayId =
new
GetPrepayIdTask();
getPrepayId.execute();
}
/**
* 用于获取
* @author 95
*
*/
private
class
GetPrepayIdTask
extends
AsyncTask<
void
, string=
""
>> {
private
ProgressDialog dialog;
@Override
protected
void
onPreExecute() {
dialog = ProgressDialog.show(context,
"提示"
,
"正在获取预支付订单..."
);
}
@Override
protected
void
onPostExecute(Map<string,string> result) {
if
(dialog !=
null
) {
dialog.dismiss();
}
resultunifiedorder=result;
genPayReq();
}
@Override
protected
void
onCancelled() {
super
.onCancelled();
}
@Override
protected
Map<string,string> doInBackground(Void... params) {
String url = String.format(
"https://api.mch.weixin.qq.com/pay/unifiedorder"
);
String entity = genProductArgs();
Log.e(
"orion"
,entity);
byte
[] buf = httpPost(url, entity);
String content =
new
String(buf);
Log.e(
"orion"
, content);
Map<string,string> xml=decodeXml(content);
return
xml;
}
}
private
void
genPayReq() {
req.appId = ParameterConfig.WX_APP_ID;
req.partnerId = ParameterConfig.WX_MCH_ID;
req.prepayId = resultunifiedorder.get(
"prepay_id"
);
req.packageValue =
"prepay_id="
+resultunifiedorder.get(
"prepay_id"
);
req.nonceStr = genNonceStr();
req.timeStamp = String.valueOf(genTimeStamp());
List<namevaluepair> signParams =
new
LinkedList<namevaluepair>();
signParams.add(
new
BasicNameValuePair(
"appid"
, req.appId));
signParams.add(
new
BasicNameValuePair(
"noncestr"
, req.nonceStr));
signParams.add(
new
BasicNameValuePair(
"package"
, req.packageValue));
signParams.add(
new
BasicNameValuePair(
"partnerid"
, req.partnerId));
signParams.add(
new
BasicNameValuePair(
"prepayid"
, req.prepayId));
signParams.add(
new
BasicNameValuePair(
"timestamp"
, req.timeStamp));
req.sign = genAppSign(signParams);
Log.e(
"orion"
, signParams.toString());
sendPayReq();
}
private
void
sendPayReq() {
api.registerApp(ParameterConfig.WX_APP_ID);
api.sendReq(req);
}
private
String genProductArgs() {
StringBuffer xml =
new
StringBuffer();
try
{
String nonceStr = genNonceStr();
xml.append(
""
);
List<namevaluepair> packageParams =
new
LinkedList<namevaluepair>();
packageParams.add(
new
BasicNameValuePair(
"appid"
, ParameterConfig.WX_APP_ID));
packageParams.add(
new
BasicNameValuePair(
"body"
, order.productname));
packageParams.add(
new
BasicNameValuePair(
"mch_id"
, ParameterConfig.WX_MCH_ID));
packageParams.add(
new
BasicNameValuePair(
"nonce_str"
, nonceStr));
packageParams.add(
new
BasicNameValuePair(
"notify_url"
, ParameterConfig.WX_notifyUrl));
packageParams.add(
new
BasicNameValuePair(
"out_trade_no"
,genOutTradNo()));
packageParams.add(
new
BasicNameValuePair(
"spbill_create_ip"
,
"127.0.0.1"
));
packageParams.add(
new
BasicNameValuePair(
"total_fee"
, (
int
)(order.totalamount*
100
)+
""
));
packageParams.add(
new
BasicNameValuePair(
"trade_type"
,
"APP"
));
String sign = genPackageSign(packageParams);
packageParams.add(
new
BasicNameValuePair(
"sign"
, sign));
String xmlstring =toXml(packageParams);
return
new
String(xmlstring.toString().getBytes(),
"ISO8859-1"
);
//return xmlstring;
}
catch
(Exception e) {
Log.e(TAG,
"genProductArgs fail, ex = "
+ e.getMessage());
return
null
;
}
}
private
String genAppSign(List<namevaluepair> params) {
StringBuilder sb =
new
StringBuilder();
for
(
int
i =
0
; i < params.size(); i++) {
sb.append(params.get(i).getName());
sb.append(
'='
);
sb.append(params.get(i).getValue());
sb.append(
'&'
);
}
sb.append(
"key="
);
sb.append(ParameterConfig.WX_API_KEY);
String appSign = getMessageDigest(sb.toString().getBytes());
Log.e(
"orion"
,appSign);
return
appSign;
}
private
HttpClient getNewHttpClient() {
try
{
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(
null
,
null
);
SSLSocketFactory sf =
new
SSLSocketFactoryEx(trustStore);
sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
HttpParams params =
new
BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
SchemeRegistry registry =
new
SchemeRegistry();
registry.register(
new
Scheme(
"http"
, PlainSocketFactory.getSocketFactory(),
80
));
registry.register(
new
Scheme(
"https"
, sf,
443
));
ClientConnectionManager ccm =
new
ThreadSafeClientConnManager(params, registry);
return
new
DefaultHttpClient(ccm, params);
}
catch
(Exception e) {
return
new
DefaultHttpClient();
}
}
private
class
SSLSocketFactoryEx
extends
SSLSocketFactory {
SSLContext sslContext = SSLContext.getInstance(
"TLS"
);
public
SSLSocketFactoryEx(KeyStore truststore)
throws
NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
super
(truststore);
TrustManager tm =
new
X509TrustManager() {
public
X509Certificate[] getAcceptedIssuers() {
return
null
;
}
@Override
public
void
checkClientTrusted(X509Certificate[] chain, String authType)
throws
java.security.cert.CertificateException {
}
@Override
public
void
checkServerTrusted(X509Certificate[] chain, String authType)
throws
java.security.cert.CertificateException {
}
};
sslContext.init(
null
,
new
TrustManager[] { tm },
null
);
}
@Override
public
Socket createSocket(Socket socket, String host,
int
port,
boolean
autoClose)
throws
IOException, UnknownHostException {
return
sslContext.getSocketFactory().createSocket(socket, host, port, autoClose);
}
@Override
public
Socket createSocket()
throws
IOException {
return
sslContext.getSocketFactory().createSocket();
}
}
public
byte
[] httpPost(String url, String entity) {
if
(url ==
null
|| url.length() ==
0
) {
Log.e(TAG,
"httpPost, url is null"
);
return
null
;
}
HttpClient httpClient = getNewHttpClient();
HttpPost httpPost =
new
HttpPost(url);
try
{
httpPost.setEntity(
new
StringEntity(entity));
httpPost.setHeader(
"Accept"
,
"application/json"
);
httpPost.setHeader(
"Content-type"
,
"application/json"
);
HttpResponse resp = httpClient.execute(httpPost);
if
(resp.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
Log.e(TAG,
"httpGet fail, status code = "
+ resp.getStatusLine().getStatusCode());
return
null
;
}
return
EntityUtils.toByteArray(resp.getEntity());
}
catch
(Exception e) {
Log.e(TAG,
"httpPost exception, e = "
+ e.getMessage());
e.printStackTrace();
return
null
;
}
}
private
String genOutTradNo() {
Random random =
new
Random();
return
getMessageDigest(String.valueOf(random.nextInt(
10000
)).getBytes());
}
public
Map<string,string> decodeXml(String content) {
try
{
Map<string, string=
""
> xml =
new
HashMap<string, string=
""
>();
XmlPullParser parser = Xml.newPullParser();
parser.setInput(
new
StringReader(content));
int
event = parser.getEventType();
while
(event != XmlPullParser.END_DOCUMENT) {
String nodeName=parser.getName();
switch
(event) {
case
XmlPullParser.START_DOCUMENT:
break
;
case
XmlPullParser.START_TAG:
if
(
"xml"
.equals(nodeName)==
false
){
//实例化student对象
xml.put(nodeName,parser.nextText());
}
break
;
case
XmlPullParser.END_TAG:
break
;
}
event = parser.next();
}
return
xml;
}
catch
(Exception e) {
Log.e(
"orion"
,e.toString());
}
return
null
;
}
private
String genNonceStr() {
Random random =
new
Random();
return
getMessageDigest(String.valueOf(random.nextInt(
10000
)).getBytes());
}
private
long
genTimeStamp() {
return
System.currentTimeMillis() /
1000
;
}
public
String getMessageDigest(
byte
[] buffer) {
char
hexDigits[] = {
'0'
,
'1'
,
'2'
,
'3'
,
'4'
,
'5'
,
'6'
,
'7'
,
'8'
,
'9'
,
'a'
,
'b'
,
'c'
,
'd'
,
'e'
,
'f'
};
try
{
MessageDigest mdTemp = MessageDigest.getInstance(
"MD5"
);
mdTemp.update(buffer);
byte
[] md = mdTemp.digest();
int
j = md.length;
char
str[] =
new
char
[j *
2
];
int
k =
0
;
for
(
int
i =
0
; i < j; i++) {
byte
byte0 = md[i];
str[k++] = hexDigits[byte0 >>>
4
&
0xf
];
str[k++] = hexDigits[byte0 &
0xf
];
}
return
new
String(str);
}
catch
(Exception e) {
return
null
;
}
}
/**
生成签名
*/
private
String genPackageSign(List<namevaluepair> params) {
StringBuilder sb =
new
StringBuilder();
for
(
int
i =
0
; i < params.size(); i++) {
sb.append(params.get(i).getName());
sb.append(
'='
);
sb.append(params.get(i).getValue());
sb.append(
'&'
);
}
sb.append(
"key="
);
sb.append(ParameterConfig.WX_API_KEY);
String packageSign = getMessageDigest(sb.toString().getBytes()).toUpperCase();
Log.e(
"orion"
,packageSign);
return
packageSign;
}
private
String toXml(List<namevaluepair> params) {
StringBuilder sb =
new
StringBuilder();
sb.append(
"<xml>"
);
for
(
int
i =
0
; i < params.size(); i++) {
sb.append(
"<"
+params.get(i).getName()+
">"
);
sb.append(params.get(i).getValue());
sb.append(
"<!--"
+params.get(i).getName()+
"-->"
);
}
sb.append(
"</xml>"
);
Log.e(
"orion"
,sb.toString());
return
sb.toString();
}
}
</namevaluepair></namevaluepair></string,></string,></string,string></namevaluepair></namevaluepair></namevaluepair></namevaluepair></namevaluepair></string,string></string,string></string,string></
void
,></string,string>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
public
class
AlipayUtil {
private
Activity context;
private
OrderInfo order;
private
Handler mhandler;
private
static
final
int
SDK_PAY_FLAG =
1
;
public
AlipayUtil(Activity context, OrderInfo order,Handler mhandler) {
this
.context=context;
this
.order=order;
this
.mhandler=mhandler;
pay();
}
private
void
pay() {
//判断是否注册商户到支付宝
if
(TextUtils.isEmpty(ParameterConfig.PARTNER) || TextUtils.isEmpty(ParameterConfig.RSA_PRIVATE) || TextUtils.isEmpty(ParameterConfig.SELLER)) {
new
AlertDialog.Builder(context).setTitle(
"警告"
).setMessage(
"需要配置PARTNER | RSA_PRIVATE| SELLER\n请到ParameterConfig.java里配置"
)
.setPositiveButton(
"确定"
,
new
DialogInterface.OnClickListener() {
public
void
onClick(DialogInterface dialoginterface,
int
i) {
//
((Activity)context).finish();
}
}).show();
return
;
}
String orderInfo = getOrderInfo(order);
/**
* 特别注意,这里的签名逻辑需要放在服务端,切勿将私钥泄露在代码中!
*/
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(context);
// 调用支付接口,获取支付结果
String result = alipay.pay(payInfo,
true
);
Message msg =
new
Message();
msg.what = SDK_PAY_FLAG;
msg.obj = result;
mHandler.sendMessage(msg);
}
};
// 必须异步调用
Thread payThread =
new
Thread(payRunnable);
payThread.start();
}
@SuppressLint
(
"HandlerLeak"
)
private
Handler mHandler =
new
Handler() {
@SuppressWarnings
(
"unused"
)
public
void
handleMessage(Message msg) {
switch
(msg.what) {
case
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();
// 同步返回需要验证的信息
String resultStatus = payResult.getResultStatus();
// 判断resultStatus 为“9000”则代表支付成功,具体状态码代表含义可参考接口文档
if
(TextUtils.equals(resultStatus,
"9000"
)) {
mhandler.sendEmptyMessage(
9000
);
//Toast.makeText(context, "支付成功", Toast.LENGTH_SHORT).show();
}
else
{
// 判断resultStatus 为非"9000"则代表可能支付失败
// "8000"代表支付结果因为支付渠道原因或者系统原因还在等待支付结果确认,最终交易是否成功以服务端异步通知为准(小概率状态)
if
(TextUtils.equals(resultStatus,
"8000"
)) {
Toast.makeText(context,
"支付结果确认中"
, Toast.LENGTH_SHORT).show();
}
else
{
// 其他值就可以判断为支付失败,包括用户主动取消支付,或者系统返回的错误
Toast.makeText(context,
"支付宝支付失败"
, Toast.LENGTH_SHORT).show();
}
mhandler.sendEmptyMessage(
8000
);
}
break
;
}
default
:
break
;
}
};
};
/**
* create the order info. 创建订单信息
*
*/
private
String getOrderInfo(OrderInfo order) {
// 签约合作者身份ID
String orderInfo =
"partner="
+
"\""
+ ParameterConfig.PARTNER +
"\""
;
// 签约卖家支付宝账号
orderInfo +=
"&seller_id="
+
"\""
+ParameterConfig.SELLER +
"\""
;
// 商户网站唯一订单号
orderInfo +=
"&out_trade_no="
+
"\""
+ order.outtradeno +
"\""
;
// 商品名称
orderInfo +=
"&subject="
+
"\""
+ order.productname +
"\""
;
// 商品详情
orderInfo +=
"&body="
+
"\""
+ order.desccontext +
"\""
;
// 商品金额
orderInfo +=
"&total_fee="
+
"\""
+ order.totalamount +
"\""
;
// 服务器异步通知页面路径
orderInfo +=
"¬ify_url="
+
"\""
+ ParameterConfig.aliPay_notifyURL +
"\""
;
// 服务接口名称, 固定值
orderInfo +=
"&service=\"mobile.securitypay.pay\""
;
// 支付类型, 固定值
orderInfo +=
"&payment_type=\"1\""
;
// 参数编码, 固定值
orderInfo +=
"&_input_charset=\"utf-8\""
;
// 设置未付款交易的超时时间
// 默认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;
}
/**
* sign the order info. 对订单信息进行签名
*
* @param content
* 待签名订单信息
*/
private
String sign(String content) {
return
SignUtils.sign(content, ParameterConfig.RSA_PRIVATE);
}
/**
* get the sign type we use. 获取签名方式
*
*/
private
String getSignType() {
return
"sign_type=\"RSA\""
;
}
}
1
微信的回调actvity
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package
com.gan.mypay.wxapi;
import
com.gan.mypay.ParameterConfig;
import
com.gan.mypay.R;
import
com.gan.mypay.SelectPayTypeActivity;
import
com.tencent.mm.sdk.constants.ConstantsAPI;
import
com.tencent.mm.sdk.modelbase.BaseReq;
import
com.tencent.mm.sdk.modelbase.BaseResp;
import
com.tencent.mm.sdk.openapi.IWXAPI;
import
com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import
com.tencent.mm.sdk.openapi.WXAPIFactory;
import
android.app.Activity;
import
android.app.AlertDialog;
import
android.content.Intent;
import
android.os.Bundle;
import
android.util.Log;
import
android.widget.Toast;
public
class
WXPayEntryActivity
extends
Activity
implements
IWXAPIEventHandler{
private
static
final
String TAG =
"MicroMsg.SDKSample.WXPayEntryActivity"
;
private
IWXAPI api;
// private TextView reulttv;
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.wx_pay_result);
api = WXAPIFactory.createWXAPI(
this
, ParameterConfig.WX_APP_ID);
api.handleIntent(getIntent(),
this
);
}
@Override
protected
void
onNewIntent(Intent intent) {
super
.onNewIntent(intent);
setIntent(intent);
api.handleIntent(intent,
this
);
}
@Override
public
void
onReq(BaseReq req) {
}
@Override
public
void
onResp(BaseResp resp) {
Log.d(TAG,
"onPayFinish, errCode = "
+ resp.errCode);
if
(resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
AlertDialog.Builder builder =
new
AlertDialog.Builder(
this
);
builder.setTitle(
"提示"
);
//builder.setMessage(getString(R.string.pay_result_callback_msg, String.valueOf(resp.errCode)));
builder.show();
Intent intent;
int
code = resp.errCode;
switch
(code) {
case
0
:
Toast.makeText(
this
,
"支付成功"
,
0
).show();
intent=
new
Intent(
this
,SelectPayTypeActivity.
class
);
intent.putExtra(
"result"
,
0
);
startActivity(intent);
finish();
break
;
case
-
1
:
Toast.makeText(
this
,
"支付失败"
,
0
).show();
intent=
new
Intent(
this
,SelectPayTypeActivity.
class
);
intent.putExtra(
"result"
, -
1
);
startActivity(intent);
finish();
break
;
case
-
2
:
Toast.makeText(
this
,
"支付取消"
,
0
).show();
intent=
new
Intent(
this
,SelectPayTypeActivity.
class
);
intent.putExtra(
"result"
, -
2
);
startActivity(intent);
finish();
break
;
default
:
break
;
}
}
}
}
0 0
- android集成支付,支付宝,微信
- Android 支付宝和微信支付集成
- Android 支付宝和微信支付集成
- Android 集成微信支付和支付宝
- Android 支付宝和微信支付集成
- Android集成微信和支付宝支付
- Android 支付宝和微信支付集成
- 快速集成微信支付和支付宝支付
- 【Java】快速集成微信支付和支付宝支付
- 【Java】快速集成微信支付和支付宝支付
- Android怎么集成支付宝支付和微信支付功能(上)
- Android怎么集成支付宝支付和微信支付功能(下)
- 关于android微信支付 和 支付宝支付的集成
- android微信和支付宝支付
- Android 支付宝和微信支付
- Android-Pay 支持微信和支付宝两种主流支付的集成库, 两行代码实现微信支付, 三行代码实现支付宝支付
- Android中关于微信支付和支付宝支付
- Android 微信支付和支付宝支付 快速接入
- Java的小图形练习
- 增量矩阵与其转置矩阵的乘积_baidu_20160913-3
- jQuery和DOM对象互转
- 数据结构Java实现【1】----算法概述
- XXX-笔试 偶数间的距离
- Android 集成微信支付和支付宝
- Oracle date和timestamp
- 主成分分析PCA
- 巴蜀 1088
- Android Studio Eclipse常用快捷键;Android Studio常用快捷键
- a1007 . Maximum Subsequence Sum (25)
- android反编译
- HDU2795 线段树
- js 拼接字符串传参乱码问题
原创粉丝点击
热门IT博客
热门问题
老师的惩罚
人脸识别
我在镇武司摸鱼那些年
重生之率土为王
我在大康的咸鱼生活
盘龙之生命进化
天生仙种
凡人之先天五行
春回大明朝
姑娘不必设防,我是瞎子
小腹刺痛怎么回事
排卵期小腹痛
小腹按压疼痛
女生小腹痛的原因
左侧小腹疼痛
小腹疼痛是怎么回事男
女性右侧小腹隐痛
小腹疼痛男性
小腹疼痛原因
左边小腹疼痛怎么回事
右下小腹疼痛怎么回事
小腹坠胀疼痛
小腹疼痛的原因有哪些
小腹右边疼痛
男的小腹疼痛怎么回事
小腹右侧疼痛怎么办
右边小腹疼痛怎么回事
小腹疼痛咋回事
小腹疼痛有哪些原因
小腹中间胀痛
长期小腹疼痛
什么妇科病会小腹痛
小腹疼女
女生小腹疼
女性小腹疼
怀孕小腹疼
做完小腹疼
小腹疼怎么回事
孕39周小腹像来例假疼
感觉小腹下坠并隐约疼
排卵期小腹疼
小腹左侧疼
小腹疼男性
深入时候小腹疼
小腹疼女性
她揉着小腹叫肚子疼
胎动时小腹有种牵拉疼
咳嗽小腹疼
怀孕小腹会疼吗
怀孕初期小腹疼
小腹一按就疼