使用Shopify Mobile-Buy-SDK Android查询商品与实现购买功能问题
解决Shopify Android GraphQL SDK的几个核心问题
我帮你逐个拆解你遇到的问题,从类型转换、价格获取到购买流程,一步步来:
1. 类型转换失败:如何从Node转为Product
你的查询已经用了onProduct指定Product字段,但在回调里需要先取出node对象再做类型判断和转换。修改onResponse里的代码:
@Override public void onResponse(@NonNull GraphResponse<Storefront.QueryRoot> response) { Storefront.Node node = response.data().getNode(); if (node instanceof Storefront.Product) { Storefront.Product product = (Storefront.Product) node; Log.v("shop", "Product Title: " + product.getTitle()); // 在这里可以继续处理商品的其他属性 } else { Log.v("shop", "Node is not a Product"); } }
原因是getNode()返回的是通用的Node类型,需要通过instanceof确认类型后再强制转换,这是Java处理多态类型的标准方式。
2. 获取商品价格:查询Product的Variants字段
Shopify的商品价格实际存储在**商品变体(Variant)**中,所以需要在查询里添加variants相关字段:
修改你的Product查询部分:
_queryBuilder.onProduct(new Storefront.ProductQueryDefinition() { @Override public void define(Storefront.ProductQuery _queryBuilder) { _queryBuilder.title() // 添加变体查询,获取价格 .variants(new Storefront.ProductVariantConnectionQueryDefinition() { @Override public void define(Storefront.ProductVariantConnectionQuery _queryBuilder) { _queryBuilder.edges(new Storefront.ProductVariantEdgeQueryDefinition() { @Override public void define(Storefront.ProductVariantEdgeQuery _queryBuilder) { _queryBuilder.node(new Storefront.ProductVariantQueryDefinition() { @Override public void define(Storefront.ProductVariantQuery _queryBuilder) { _queryBuilder.price() // 商品当前价格 .compareAtPrice() // 原价(如果有折扣) .id(); // 变体ID,后续购买需要用到 } }); } }); } }); } });
之后在回调里获取价格:
if (node instanceof Storefront.Product) { Storefront.Product product = (Storefront.Product) node; // 取第一个变体(通常单商品只有一个变体) Storefront.ProductVariant variant = product.getVariants().getEdges().get(0).getNode(); Log.v("shop", "Price: " + variant.getPrice()); }
3. 检查用户是否已购买商品:查询用户的订单记录
要判断用户是否购买过该商品,需要先获取登录用户的Customer对象,再查询其订单中的商品:
首先确保用户已登录(通过Shopify的登录流程获取会话),然后执行以下查询:
Storefront.QueryRootQuery customerOrdersQuery = Storefront.query(new Storefront.QueryRootQueryDefinition() { @Override public void define(Storefront.QueryRootQuery _queryBuilder) { _queryBuilder.customer(new Storefront.CustomerQueryDefinition() { @Override public void define(Storefront.CustomerQuery _queryBuilder) { _queryBuilder.orders(new Storefront.OrderConnectionQueryDefinition() { @Override public void define(Storefront.OrderConnectionQuery _queryBuilder) { _queryBuilder.edges(new Storefront.OrderEdgeQueryDefinition() { @Override public void define(Storefront.OrderEdgeQuery _queryBuilder) { _queryBuilder.node(new Storefront.OrderQueryDefinition() { @Override public void define(Storefront.OrderQuery _queryBuilder) { _queryBuilder.lineItems(new Storefront.LineItemConnectionQueryDefinition() { @Override public void define(Storefront.LineItemConnectionQuery _queryBuilder) { _queryBuilder.edges(new Storefront.LineItemEdgeQueryDefinition() { @Override public void define(Storefront.LineItemEdgeQuery _queryBuilder) { _queryBuilder.node(new Storefront.LineItemQueryDefinition() { @Override public void define(Storefront.LineItemQuery _queryBuilder) { _queryBuilder.productId(); // 获取商品ID } }); } }); } }); } }); } }); } }); } }); } }); // 执行查询 cl.queryGraph(customerOrdersQuery).enqueue(new GraphCall.Callback<Storefront.QueryRoot>() { @Override public void onResponse(@NonNull GraphResponse<Storefront.QueryRoot> response) { Storefront.Customer customer = response.data().getCustomer(); if (customer != null) { boolean hasPurchased = false; String targetProductId = "72355741740"; // 你的商品ID // 遍历所有订单的商品 for (Storefront.OrderEdge orderEdge : customer.getOrders().getEdges()) { Storefront.Order order = orderEdge.getNode(); for (Storefront.LineItemEdge itemEdge : order.getLineItems().getEdges()) { Storefront.LineItem item = itemEdge.getNode(); if (targetProductId.equals(item.getProductId().toString())) { hasPurchased = true; break; } } if (hasPurchased) break; } Log.v("shop", "Has purchased: " + hasPurchased); } } @Override public void onFailure(@NonNull GraphError error) {} });
4. 完成购买流程:创建Checkout并跳转支付
Shopify的购买流程核心是创建Checkout对象,获取支付链接,然后通过WebView让用户完成支付:
// 替换为你的商品变体ID String variantId = "YOUR_VARIANT_ID"; Storefront.MutationQuery createCheckoutMutation = Storefront.mutation(new Storefront.MutationQueryDefinition() { @Override public void define(Storefront.MutationQuery _queryBuilder) { // 构建Checkout输入参数 Storefront.CreateCheckoutInput checkoutInput = new Storefront.CreateCheckoutInput.Builder() .lineItems(Collections.singletonList( new Storefront.CheckoutLineItemInput.Builder() .variantId(new ID(variantId)) .quantity(1) .build() )) .build(); // 执行创建Checkout的mutation _queryBuilder.createCheckout(checkoutInput, new Storefront.CheckoutQueryDefinition() { @Override public void define(Storefront.CheckoutQuery _queryBuilder) { _queryBuilder.webUrl(); // 获取支付页面的URL } }); } }); // 调用mutation cl.mutateGraph(createCheckoutMutation).enqueue(new GraphCall.Callback<Storefront.Mutation>() { @Override public void onResponse(@NonNull GraphResponse<Storefront.Mutation> response) { Storefront.Checkout checkout = response.data().getCreateCheckout(); if (checkout != null) { String checkoutUrl = checkout.getWebUrl(); // 打开WebView加载这个URL,让用户完成支付 Intent intent = new Intent(YourActivity.this, WebViewActivity.class); intent.putExtra("URL", checkoutUrl); startActivity(intent); } } @Override public void onFailure(@NonNull GraphError error) {} });
支付完成后,Shopify会自动处理订单状态,你可以通过Webhook或者后续查询订单API来确认支付结果,进而开放用户对文章的访问权限。
内容的提问来源于stack exchange,提问作者ganesh
相关产品推荐
相关产品推荐

