### Configure build.gradle for ShouQianBa SDK Source: https://github.com/wosai/shouqianbademo-android/blob/master/androidstudio/README.md Add the flatDir repository and the required dependencies to your project's build.gradle file. ```gradle repositories { flatDir { dirs 'libs' } } dependencies { compile(name:'shouqianba', ext:'aar') compile 'com.google.code.gson:gson:2.3.1' } ``` -------------------------------- ### 配置 Android 项目依赖 Source: https://context7.com/wosai/shouqianbademo-android/llms.txt 在 build.gradle 文件中配置 SDK 依赖和本地 AAR 仓库路径。 ```groovy // build.gradle 配置 android { compileSdkVersion 21 buildToolsVersion "21.1.2" defaultConfig { applicationId "com.wosai.demo" minSdkVersion 14 targetSdkVersion 21 versionCode 1 versionName "1.0" } } repositories { flatDir { dirs 'libs' } } dependencies { compile(name:'shouqianba', ext:'aar') compile 'com.google.code.gson:gson:2.3.1' compile 'com.android.support:appcompat-v7:21.0.3' } ``` -------------------------------- ### 构建 OrderInfo 订单信息 Source: https://context7.com/wosai/shouqianbademo-android/llms.txt 创建并配置订单数据模型,包括商户信息、金额及 MD5 签名生成。 ```java import cn.wosai.upay.OrderInfo; import cn.wosai.upay.PayMethod; import java.math.BigInteger; import java.security.MessageDigest; // 商户配置信息 String storeId = "03322111001200200000024"; // 商户ID String appId = "1001200200000024"; // 应用ID String key = "dc1277093df84ff23b885cc776deca13"; // 签名密钥 // 创建订单信息 OrderInfo orderInfo = new OrderInfo(); orderInfo.setMerchantId(storeId); // 设置商户ID orderInfo.setAppId(appId); // 设置应用ID orderInfo.setOrderId("Test" + System.currentTimeMillis()); // 设置唯一订单号 orderInfo.setOperId("1000"); // 设置操作员ID orderInfo.setSubject("商品名称"); // 设置商品描述 orderInfo.setAmount(100L); // 设置金额(单位:分) orderInfo.setCurType("156"); // 设置货币类型(156=人民币) // 生成MD5签名 String signString = orderInfo.getMerchantId() + orderInfo.getAppId() + key; String sign = String.format("%032x", new BigInteger(1, MessageDigest.getInstance("MD5").digest(signString.getBytes()))); orderInfo.setSign(sign); // 设置签名 // 可选:指定支付方式 orderInfo.setPayMethod(PayMethod.UPAY_ALIPAY); // 支付宝 // orderInfo.setPayMethod(PayMethod.UPAY_WEIXIN); // 微信支付 // orderInfo.setPayMethod(PayMethod.UPAY_LAKALA); // 拉卡拉刷卡 ``` -------------------------------- ### UpayTask.queryOrder() Source: https://context7.com/wosai/shouqianbademo-android/llms.txt Queries the payment status and detailed information of an order, returning account, amount, batch number, and voucher number. ```APIDOC ## UpayTask.queryOrder() ### Description Queries the payment status and detailed information of an order. Returns the account, amount, batch number, order ID, status, time, and voucher number. ### Parameters #### Request Body - **info** (OrderInfo) - Required - The order information object containing details for the query. ``` -------------------------------- ### 发起支付请求 Source: https://context7.com/wosai/shouqianbademo-android/llms.txt 使用 UpayTask 发起通用支付或指定支付方式的请求,并通过 UpayCallBack 处理回调结果。 ```java import cn.wosai.upay.UpayTask; import cn.wosai.upay.UpayCallBack; import cn.wosai.upay.UpayResult; import cn.wosai.upay.PayMethod; // 通用支付(用户选择支付方式) private void checkout() { OrderInfo info = new OrderInfo(); info.setOrderId("Order" + System.currentTimeMillis()); info.setMerchantId(storeId); info.setAppId(appId); info.setSign(md5(info.getMerchantId() + info.getAppId() + key)); info.setOperId("1000"); info.setSubject("测试商品"); info.setAmount(1L); // 1分钱 info.setCurType("156"); new UpayTask(this).checkout(info, new UpayCallBack() { @Override public void onExecuteResult(UpayResult result) { if (result.getState() == UpayResult.PAID) { // 支付成功 String orderId = result.getOrderId(); Long amount = result.getAmount(); String time = result.getTime(); // 处理支付成功逻辑 } } }); } // 支付宝直接支付 private void alipayCheckout() { OrderInfo info = createOrderInfo(); info.setPayMethod(PayMethod.UPAY_ALIPAY); new UpayTask(this).checkout(info, new UpayCallBack() { @Override public void onExecuteResult(UpayResult result) { if (result.getState() == UpayResult.PAID) { handlePaymentSuccess(result); } } }); } // 微信直接支付 private void weChatCheckout() { OrderInfo info = createOrderInfo(); info.setPayMethod(PayMethod.UPAY_WEIXIN); new UpayTask(this).checkout(info, new UpayCallBack() { @Override public void onExecuteResult(UpayResult result) { if (result.getState() == UpayResult.PAID) { handlePaymentSuccess(result); } } }); } // 拉卡拉刷卡支付 private void lakalaCheckout() { OrderInfo info = createOrderInfo(); info.setPayMethod(PayMethod.UPAY_LAKALA); new UpayTask(this).checkout(info, new UpayCallBack() { @Override public void onExecuteResult(UpayResult result) { if (result.getState() == UpayResult.PAID) { handlePaymentSuccess(result); } } }); } ``` -------------------------------- ### Query Order Details with UpayTask Source: https://context7.com/wosai/shouqianbademo-android/llms.txt Use queryOrder to retrieve the payment status and detailed information of an order. Results are handled in the main thread via a Handler. ```java import cn.wosai.upay.UpayTask; import cn.wosai.upay.UpayCallBack; import cn.wosai.upay.UpayResult; import android.os.Handler; import android.os.Message; private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); UpayResult result = (UpayResult) msg.obj; showOrderDetails(result); } }; // 查询订单 private void queryOrder(OrderInfo info) { new UpayTask(this).queryOrder(info, new UpayCallBack() { @Override public void onExecuteResult(UpayResult result) { // 在主线程处理UI更新 Message message = handler.obtainMessage(); message.obj = result; handler.sendMessage(message); } }); } // 显示订单详情 private void showOrderDetails(UpayResult result) { StringBuilder sb = new StringBuilder(); sb.append("账户: " + result.getAccount() + "\n"); sb.append("金额: " + result.getAmount() + "\n"); sb.append("批次号: " + result.getBatchNo() + "\n"); sb.append("订单号: " + result.getOrderId() + "\n"); sb.append("状态: " + result.getState() + "\n"); sb.append("时间: " + result.getTime() + "\n"); sb.append("凭证号: " + result.getVoucherNo() + "\n"); new AlertDialog.Builder(this) .setTitle("订单详情") .setMessage(sb.toString()) .show(); } ``` -------------------------------- ### Android 完整支付流程示例 Source: https://context7.com/wosai/shouqianbademo-android/llms.txt 此 Activity 示例展示了如何集成收钱吧SDK以实现支付、查询和撤销功能。需要配置商户信息并实现支付回调。 ```java import android.app.Activity; import android.os.Bundle; import android.view.View; import java.math.BigInteger; import java.security.MessageDigest; import cn.wosai.upay.PayMethod; import cn.wosai.upay.UpayCallBack; import cn.wosai.upay.UpayResult; import cn.wosai.upay.UpayTask; import cn.wosai.upay.OrderInfo; public class PaymentActivity extends Activity { // 商户配置 private static final String STORE_ID = "03322111001200200000024"; private static final String APP_ID = "1001200200000024"; private static final String SECRET_KEY = "dc1277093df84ff23b885cc776deca13"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_payment); // 绑定支付按钮 findViewById(R.id.btn_pay).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startPayment(100L, "测试商品"); } }); } // 发起支付 private void startPayment(Long amount, String subject) { OrderInfo order = new OrderInfo(); order.setOrderId("PAY" + System.currentTimeMillis()); order.setMerchantId(STORE_ID); order.setAppId(APP_ID); order.setSign(generateSign(STORE_ID, APP_ID, SECRET_KEY)); order.setOperId("1000"); order.setSubject(subject); order.setAmount(amount); order.setCurType("156"); new UpayTask(this).checkout(order, new UpayCallBack() { @Override public void onExecuteResult(UpayResult result) { if (result.getState() == UpayResult.PAID) { onPaymentSuccess(result); } else { onPaymentFailed(); } } }); } // 支付成功处理 private void onPaymentSuccess(UpayResult result) { // 保存订单信息到本地或服务器 // 跳转到支付成功页面 } // 支付失败处理 private void onPaymentFailed() { // 显示失败提示 } // 生成签名 private String generateSign(String merchantId, String appId, String key) { try { String input = merchantId + appId + key; return String.format("%032x", new BigInteger(1, MessageDigest.getInstance("MD5").digest(input.getBytes()))); } catch (Exception e) { throw new RuntimeException(e); } } } ``` -------------------------------- ### Add Shouqianba SDK Dependency in Android Studio Source: https://github.com/wosai/shouqianbademo-android/blob/master/README.md Add the Shouqianba SDK as an AAR dependency and include the Gson library, which is required by the SDK. Ensure the 'libs' directory is configured in your repositories. ```gradle repositories { flatDir { dirs 'libs' } } dependencies { compile(name:'shouqianba', ext:'aar') compile 'com.google.code.gson:gson:2.3.1' // 收钱吧SDK需要用到 } ``` -------------------------------- ### UpayResult Handling Source: https://context7.com/wosai/shouqianbademo-android/llms.txt Details on how to process the UpayResult object returned from payment operations. ```APIDOC ## UpayResult ### Description Encapsulates the result of a payment operation, including order status, amount, and payment method. ### Response Fields - **state** (int) - The status of the order (e.g., UpayResult.PAID, UpayResult.REVOKED) - **orderId** (String) - The unique order identifier - **amount** (Long) - The payment amount in cents - **account** (String) - The payment account identifier - **time** (String) - The timestamp of the transaction - **batchNo** (String) - The batch number - **voucherNo** (String) - The voucher number - **payMethod** (PayMethod) - The method used for payment (e.g., ALIPAY, WEIXIN, LAKALA) ``` -------------------------------- ### Handle Payment Results with UpayResult Source: https://context7.com/wosai/shouqianbademo-android/llms.txt Process the UpayResult object to determine the final status of a payment. This includes checking for PAID or REVOKED states and extracting transaction details like order ID, amount, and payment method. ```java import cn.wosai.upay.UpayResult; import cn.wosai.upay.PayMethod; // 处理支付结果 private void handlePaymentResult(UpayResult result) { // 获取支付状态 int state = result.getState(); // 状态常量 // UpayResult.PAID - 支付成功 // UpayResult.REVOKED - 已撤销 if (state == UpayResult.PAID) { // 获取支付详情 String orderId = result.getOrderId(); // 订单号 Long amount = result.getAmount(); // 支付金额(分) String account = result.getAccount(); // 支付账户 String time = result.getTime(); // 支付时间 String batchNo = result.getBatchNo(); // 批次号 String voucherNo = result.getVoucherNo(); // 凭证号 PayMethod payMethod = result.getPayMethod(); // 支付方式 // 记录交易 logTransaction(orderId, amount, payMethod.name()); // 更新UI显示 showPaymentSuccess(amount, time); } else if (state == UpayResult.REVOKED) { // 订单已撤销 showMessage("订单已撤销"); } } // 判断支付方式并显示 private String getPayMethodName(PayMethod method) { if (method == PayMethod.UPAY_ALIPAY) { return "支付宝"; } else if (method == PayMethod.UPAY_WEIXIN) { return "微信支付"; } else if (method == PayMethod.UPAY_LAKALA) { return "拉卡拉刷卡"; } return "未知"; } ``` -------------------------------- ### UpayTask.revoke() Source: https://context7.com/wosai/shouqianbademo-android/llms.txt Revokes a completed payment order. Upon success, the order state is updated to REVOKED. ```APIDOC ## UpayTask.revoke() ### Description Revokes an already completed payment order. The orderInfo object must contain valid merchant credentials and a signature. ### Parameters #### Request Body - **orderInfo** (OrderInfo) - Required - The order information object containing merchantId, appId, and a valid MD5 signature. ``` -------------------------------- ### Revoke Completed Order with UpayTask Source: https://context7.com/wosai/shouqianbademo-android/llms.txt Use revoke to cancel a completed payment order. Ensure the OrderInfo object includes merchant ID, app ID, and a valid signature. The success state is indicated by UpayResult.REVOKED. ```java import cn.wosai.upay.UpayTask; import cn.wosai.upay.UpayCallBack; import cn.wosai.upay.UpayResult; // 撤销订单 private void revokeOrder(OrderInfo orderInfo) { // 确保订单信息包含必要的认证信息 orderInfo.setMerchantId(storeId); orderInfo.setAppId(appId); orderInfo.setSign(md5(orderInfo.getMerchantId() + orderInfo.getAppId() + key)); new UpayTask(this).revoke(orderInfo, new UpayCallBack() { @Override public void onExecuteResult(UpayResult result) { if (result.getState() == UpayResult.REVOKED) { // 撤销成功 updateOrderStatus(result); showToast("订单撤销成功"); } else { // 撤销失败 showToast("订单撤销失败"); } } }); } // 更新订单状态 private void updateOrderStatus(UpayResult result) { OrderInfo order = new OrderInfo(); order.setMerchantId(storeId); order.setAppId(appId); order.setSign(md5(order.getMerchantId() + order.getAppId() + key)); order.setTransactionId(result.getOrderId()); order.setAmount(result.getAmount()); order.setTime(result.getTime()); order.setStatus(result.getState()); order.setBatchNo(result.getBatchNo()); order.setVoucherNo(result.getVoucherNo()); order.setPayMethod(result.getPayMethod()); // 更新本地订单列表 refreshOrderList(order); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.