Здесь ниже моего полного кода, где я сталкиваюсь вопрос и грохот, как, kdkdsalglasdlkgvkasdlnbglkjdsalkglkfdaskl; vlkasdlkjlhksdalkglkhsdaoihfgewoitgsdghdslkjavnlcsalnglksadlkgkl; ndsalknflkjsdalkhglnksdalnkglksadlkngsdlknaglnk; askl; gnlkdsak; lgskl; Dh; ksaldkngdsa; LJ; fmsdkvclknsdlnkgvsdalknhlknasd; ljgk; lsdakjl; gsldkj; agjkl; сал; jksg
пакет com.ctsuk.newplacestesting;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.ActionBar;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.stripe.android.ApiResultCallback;
import com.stripe.android.Stripe;
import com.stripe.android.model.Card;
import com.stripe.android.model.PaymentMethod;
import com.stripe.android.model.PaymentMethodCreateParams;
import com.stripe.android.model.Token;
import com.stripe.android.view.CardMultilineWidget;
import com.stripe.exception.StripeException;
import com.stripe.model.Charge;
import com.stripe.model.Customer;
import com.stripe.net.RequestOptions;
import java.util.HashMap;
import java.util.Map;
public class AddPayment extends AppCompatActivity {
CardMultilineWidget cardMultilineWidget;
Button save;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_payment);
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("Payment");
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
cardMultilineWidget = findViewById(R.id.card_input_widget);
save = findViewById(R.id.save_payment);
save.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view) {
saveCard();
}
});
}
private void saveCard() {
Card card = cardMultilineWidget.getCard();
if(card == null){
Toast.makeText(getApplicationContext(),"Invalid card",Toast.LENGTH_SHORT).show();
}else {
if (!card.validateCard()) {
Toast.makeText(getApplicationContext(), "Invalid card", Toast.LENGTH_SHORT).show();
} else {
CreateToken(card);
}
}
}
private void CreateToken( Card card) {
Stripe stripe = new Stripe(getApplicationContext(), getString(R.string.publishablekey));
stripe.createToken(
card, new ApiResultCallback<Token>() {
public void onSuccess(Token token)
{
chargeCustomer(token);
Toast.makeText(AddPayment.this, token.getId(), Toast.LENGTH_SHORT).show();
}
public void onError(Exception error)
{
Toast.makeText(getApplicationContext(), error.getLocalizedMessage(), Toast.LENGTH_LONG).show();
}
}
);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
@SuppressLint("StaticFieldLeak")
public void chargeCustomer(Token token) {
final Map<String, Object> chargeParams = new HashMap<String, Object>();
chargeParams.put("amount", 300);
chargeParams.put("currency", "usd");
chargeParams.put("card", token.getId());
chargeParams.put("description","54");
chargeParams.put("Customer","john@gmail.com");
new AsyncTask<String, String, String>() {
Charge charge;
@Override
protected String doInBackground(String... params) {
try {
com.stripe.Stripe.apiKey = "sk_test_47WLnrKYKZgZuc09bbclbDDB009VQkjSkK";
charge = Charge.create(chargeParams);
Log.i("IsCharged", charge.getCreated().toString());
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Toast.makeText(AddPayment.this, ""+charge.getCreated().toString()+charge.getBalanceTransaction()+charge.getCustomer(), Toast.LENGTH_SHORT).show();
}
});
} catch (Exception e) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Toast.makeText(AddPayment.this, ""+e.toString(), Toast.LENGTH_SHORT).show();
}
});
}
return "success";
}
protected void onPostExecute(String result) {
if(result.equals("success")){
Toast.makeText(AddPayment.this, "Card Charged : " + charge.getCreated() + "\nPaid : " +charge.getPaid(), Toast.LENGTH_LONG).show();
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Toast.makeText(AddPayment.this, ""+charge.getTransfer(), Toast.LENGTH_SHORT).show();
}
});
}
};
}.execute();
}
}