安卓 整合微信支付和支付寶
阿新 • • 發佈:2019-02-07
@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); } }
<span style="white-space:pre"> </span>activty_main.xml
<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ScrollView android:layout_width="match_parent" android:layout_height="0dp" android:background="@color/igray" android:layout_weight="1" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingLeft="15dp" android:paddingRight="15dp" android:paddingTop="10dp" android:paddingBottom="10dp" android:orientation="vertical" android:background="@color/white" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/product_ordsubmit_username" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="14sp" android:gravity="center_vertical" android:textColor="@color/black" android:text="收貨人:" /> <TextView android:id="@+id/product_ordsubmit_phone" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="14sp" android:gravity="center_vertical|right" android:textColor="@color/igray_et" android:text="13862325641" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="14sp" android:gravity="center_vertical" android:textColor="@color/igray_et" android:text="收貨地址:" /> <TextView android:id="@+id/product_ordsubmit_adress" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="14sp" android:textColor="@color/igray_et" android:drawableRight="@drawable/next" android:text="上海市徐彙區浦北路中星城15號" /> </LinearLayout> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="5dp" android:background="@color/igray"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white" android:paddingLeft="15dp" android:paddingRight="15dp" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageView android:id="@+id/product_ordsubmit_img" android:layout_width="100dp" android:layout_height="100dp" android:scaleType="centerCrop" android:src="@drawable/user_head" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/product_ordsubmit_desc" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:textSize="14sp" android:text="商品描述" android:layout_margin="10dp" android:textColor="@color/black"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/product_ordsubmit_price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="¥49" android:textSize="18sp" android:textColor="@color/Orange" android:layout_margin="5dp" /> <TextView android:id="@+id/product_ordsubmit_intg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="積分:2" android:textSize="14sp" android:layout_margin="5dp" android:textColor="@color/igray_et" /> <View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1"/> <TextView android:id="@+id/product_ordsubmit_count1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="X2" android:textSize="14sp" android:layout_margin="5dp" android:textColor="@color/igray_et" /> </LinearLayout> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingTop="5dp" android:gravity="center_vertical" android:paddingBottom="5dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="購買數量" android:textColor="@color/black" android:textSize="14sp" /> <View android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> <ImageView android:id="@+id/product_ordsubmit_count_sub" android:layout_width="25dp" android:layout_height="25dp" android:src="@drawable/sub_orange"/> <TextView android:id="@+id/product_ordsubmit_count" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="3" android:textColor="@color/black" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:textSize="18sp" /> <ImageView android:id="@+id/product_ordsubmit_count_add" android:layout_width="25dp" android:layout_height="25dp" android:src="@drawable/add_orange"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingTop="5dp" android:focusableInTouchMode="true" android:gravity="center_vertical" android:paddingBottom="5dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="業主留言:" android:textColor="@color/black" android:textSize="14sp" /> <EditText android:id="@+id/product_ordsubmit_say_et" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="選填,可以填你對賣家一致達成的要求" android:textColor="@color/igray_et" android:layout_marginLeft="10dp" android:singleLine="true" android:layout_marginRight="10dp" android:textSize="14sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingTop="5dp" android:gravity="center_vertical" android:paddingBottom="5dp" > <TextView android:id="@+id/product_ordsubmit_intgtotal1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="共得到6積分" android:textColor="@color/igray_et" android:textSize="14sp" /> <View android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> <TextView android:id="@+id/product_ordsubmit_counttotal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="14sp" android:textColor="@color/black" android:text="共3件"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="合計:" android:textColor="@color/black" android:layout_marginLeft="10dp" android:textSize="14sp" /> <TextView android:id="@+id/product_ordsubmit_pricetotal1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:textColor="@color/Orange" android:text="¥127"/> </LinearLayout> </LinearLayout> </LinearLayout> </ScrollView> <LinearLayout android:layout_width="match_parent" android:layout_height="44dp" android:paddingLeft="15dp" android:paddingRight="15dp" android:paddingTop="5dp" android:paddingBottom="5dp" android:gravity="right|center_vertical" android:background="@color/white"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="合計:" android:textColor="@color/black" android:textSize="14sp" /> <TextView android:id="@+id/product_ordsubmit_pricetotal2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="14sp" android:textColor="@color/Orange" android:text="¥127"/> <TextView android:id="@+id/product_ordsubmit_intgtotal2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="積分:6" android:textColor="@color/igray_et" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:textSize="12sp" /> <Button android:id="@+id/product_ordsubmit_ok" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/shape_btn_oval_orange_bg2" android:text="確認"/> </LinearLayout> </LinearLayout>
2.在mainactivty中點選確認按鈕呼叫支付方式選擇頁面SelectPayTypeActivity,用來發起支付選擇
SelectPayTypeActivity.java
activty_selecte_pay_type.xml<pre name="code" class="java">@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; } }
<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp"
android:gravity="center_vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textSize="18sp"
android:textColor="@color/black"
android:text="需支付:"/>
<TextView
android:id="@+id/last_pay_count_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textSize="22sp"
android:textColor="@color/Orange"
android:text="¥152"/>
</LinearLayout>
<View android:layout_width="match_parent"
android:layout_height="5dp"
android:background="@color/igray"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingBottom="10dp"
>
<LinearLayout
android:id="@+id/paytype_of_weixin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:orientation="horizontal"
android:gravity="center_vertical"
>
<ImageView android:layout_width="44dp"
android:layout_height="44dp"
android:src="@drawable/icon64_wx"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="微信支付"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:textSize="20sp"
/>
<View android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<CheckBox
android:id="@+id/paytype_of_weixin_ck"
android:layout_width="wrap_content"
android:checked="true"
android:layout_height="wrap_content"
android:focusable="false"
/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/igray"/>
<LinearLayout
android:id="@+id/paytype_of_zhifubao"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:orientation="horizontal"
android:gravity="center_vertical"
>
<ImageView android:layout_width="44dp"
android:layout_height="44dp"
android:src="@drawable/zfb_icon_120"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="支付寶"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:textSize="20sp"
/>
<View android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<CheckBox
android:id="@+id/paytype_of_zhifubao_ck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
/>
</LinearLayout>
<View android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/igray"/>
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:id="@+id/btn_pay_submit"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="支付"
android:textColor="@color/white"
android:layout_marginBottom="10dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:layout_alignParentBottom="true"
android:background="@drawable/shape_btn_oval_orange_bg2"
/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>