Shopify / mobile-buy-sdk-android

Shopify’s Mobile Buy SDK makes it simple to sell physical products inside your mobile app. With a few lines of code, you can connect your app with the Shopify platform and let your users buy your products using their credit card.
MIT License
214 stars 136 forks source link

Create customer and getcustomer is not working #545

Closed kvrajukonduru closed 5 years ago

kvrajukonduru commented 6 years ago

I tried to create a customer. I'm getting onResponse ..but always customer object is null.below is the code. .Request to help us.

Error:java.lang.NullPointerException: Attempt to invoke virtual method 'com.shopify.graphql.support.ID com.shopify.buy3.Storefront$Customer.getId()' on a null object reference.

Code: private void createCustomerAccount(String email,String pwd){

//input
Storefront.CustomerCreateInput input = new Storefront.CustomerCreateInput(email, pwd)
        .setFirstName("RAJU123")
        .setLastName("VENKAT123")
        .setAcceptsMarketing(true)
        .setPhone("1-123-456-7890");

//query
Storefront.MutationQuery mutationQueryCreateCustomer = Storefront.mutation(mutation -> mutation
        .customerCreate(input, query -> query
                .customer(customer -> customer
                        .id()
                        .email()
                        .firstName()
                        .lastName()
                )
                .userErrors(userError -> userError
                        .field()
                        .message()
                )
        )
);

//API CALL for create customer Account

graphClient.mutateGraph(mutationQueryCreateCustomer).enqueue(new GraphCall.Callback<Storefront.Mutation>() {
    @Override
    public void onResponse(@NonNull GraphResponse<Storefront.Mutation> response) {
        Log.d("RAJU","Create customer Account API SUCESS:");

        if (response.data() != null) {

            String id = response.data().getCustomerCreate().getCustomer().getId().toString();
            String email = response.data().getCustomerCreate().getCustomer().getEmail();
            Log.d("RAJU","Create Customer Info:"+email+":"+id);
        }

    }

    @Override
    public void onFailure(@NonNull GraphError error) {
        Log.d("RAJU","Create customer Account API FAIL:"+error.getMessage());

    }

});

}

GujaratiMonali commented 6 years ago

please try this code.. //input

Storefront.CustomerCreateInput input = new Storefront.CustomerCreateInput(email, pwd)
        .setFirstName("RAJU123")
        .setLastName("VENKAT123")
        .setAcceptsMarketing(true)
        .setPhone("1-123-456-7890");

//query

Storefront.MutationQuery mutationQueryCreateCustomer = Storefront.mutation(mutation -> mutation
        .customerCreate(input, query -> query
                .customer(customer -> customer
                        .id()
                        .email()
                        .firstName()
                        .lastName()
                       .phone()
                )
                .userErrors(userError -> userError
                        .field()
                        .message()
                )
        )
);

//API CALL for create customer Account

graphClient.mutateGraph(query).enqueue(new GraphCall.Callback<Storefront.Mutation>() {
            @Override
            public void onResponse(@NonNull GraphResponse<Storefront.Mutation> response) {
                if (!response.data().getCustomerCreate().getUserErrors().isEmpty()) {
                    for (Storefront.UserError error : response.data().getCustomerCreate().getUserErrors()) {
                        Log.e("TAG", "error" + error.getMessage());
                    } 
                } else {
                    Storefront.Customer customer = response.data().getCustomerCreate().getCustomer();
                    Log.e("TAG", "data " + response.data());
                    Log.e("TAG", "id " + customer.getId());
                    Log.e("TAG", "firstname " + customer.getFirstName());
                    Log.e("TAG", "lastname " + customer.getLastName());
                    Log.e("TAG", "phone " + customer.getPhone());
                    Log.e("TAG", "email " + customer.getEmail());

                }
            }

            @Override
            public void onFailure(@NonNull GraphError error) {
                Log.e("TAG", "customer" + error.getMessage());
            }
        });

any query than please ask..

kvrajukonduru commented 6 years ago

Thanks GujaratiMonali . I will check and update you soon. Once again thanks for your support

ghost commented 4 years ago

graphClient.mutateGraph(query).enqueue(new GraphCall.Callback() { @Override public void onResponse(@NonNull GraphResponse response) { if (!response.data().getCustomerCreate().getUserErrors().isEmpty()) { for (Storefront.UserError error : response.data().getCustomerCreate().getUserErrors()) { Log.e("TAG", "error" + error.getMessage()); } } else { Storefront.Customer customer = response.data().getCustomerCreate().getCustomer(); Log.e("TAG", "data " + response.data()); Log.e("TAG", "id " + customer.getId()); Log.e("TAG", "firstname " + customer.getFirstName()); Log.e("TAG", "lastname " + customer.getLastName()); Log.e("TAG", "phone " + customer.getPhone()); Log.e("TAG", "email " + customer.getEmail());

           }
       }

       @Override
       public void onFailure(@NonNull GraphError error) {
           Log.e("TAG", "customer" + error.getMessage());
       }
   });

I am getting this error "java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List com.shopify.buy3.Storefront$CustomerCreatePayload.getUserErrors()' on a null object reference"