如何在Android应用程序集成贝宝? [英] How integrate Paypal in android Application?
问题描述
我尝试使用沙盒Android应用程序集成贝宝。我越来越渴望成功使用PayPal,但是当我做支付,然后屏幕会得到无形的情况下直接响应。
我怎样才能获得上述问题的答案?
这是我的code。
私人无效invokeSimplePayment()
{
尝试
{
PayPalPayment支付=新PayPalPayment();
payment.setSubtotal(新的BigDecimal(AMT));
payment.setCurrencyType(Currency_ code [code]);
payment.setRecipient(Rec_Email);
payment.setPaymentType(PayPal.PAYMENT_TYPE_GOODS);
。意图checkoutIntent = PayPal.getInstance()结算(支付,这一点);
startActivityForResult(checkoutIntent,请求);
}
赶上(例外五)
{
e.printStackTrace();
}
}
公共无效onActivityResults(INT申请code,INT结果code,意图数据)
{
开关(结果code)
{
案例Activity.RESULT_OK:
resultTitle =成功;
resultInfo =您已经成功地完成了这个;
// resultExtra =事务ID:+ data.getStringExtra(PayPalActivity.EXTRA_PAY_KEY);
打破;
案例Activity.RESULT_CANCELED:
resultTitle =取消;
resultInfo =该交易已被取消。
resultExtra =;
打破;
案例PayPalActivity.RESULT_FAILURE:
resultTitle =失败;
resultInfo = data.getStringExtra(PayPalActivity.EXTRA_ERROR_MESSAGE);
resultExtra =错误ID:+ data.getStringExtra(PayPalActivity.EXTRA_ERROR_ID);
}
的System.out.println(结果==============+ resultTitle);
的System.out.println(ResultInfo ==============+ resultInfo);
}
这是我的code和它工作正常。有两类。
有关从沙箱贝宝lliev envoirment你必须做两件事情:把实际的帐户持有人的集中reciepient并提交您的应用程序,以贝宝可以实时ID
公共类ResultDelegate实现PayPalResultDelegate,序列化{
私有静态最后长的serialVersionUID = 10001L;
公共无效onPaymentSucceeded(字符串payKey,字符串paymentStatus){
main.resultTitle =成功;
main.resultInfo =您已成功完成交易。
main.resultExtra =键:+ payKey;
}
公共无效onPaymentFailed(字符串paymentStatus,字符串的correlationID,
字符串payKey,字符串ErrorID中,字符串的errorMessage){
main.resultTitle =失败;
main.resultInfo =的errorMessage;
main.resultExtra =错误ID:+ ErrorID中+\ nCorrelation ID:
+的correlationID +\ nPay键:+ payKey;
}
公共无效onPaymentCanceled(字符串paymentStatus){
main.resultTitle =取消;
main.resultInfo =该交易已被取消。
main.resultExtra =;
}
主类:
公共类主要的扩展活动实现OnClickListener {
//要使用的贝服务器 - 也可以是ENV_NONE和ENV_LIVE
私有静态最终诠释服务器= PayPal.ENV_LIVE;
//你从贝宝收到您的申请的ID
私有静态最后弦乐的appid =APP-0N8000046V443613X;
//这是通过在对startActivityForResult()的Android功能,使用的值是由你
私有静态最终诠释请求= 1;
公共静态最后弦乐版本=10.12.09.8053;
受保护的静态最终诠释INITIALIZE_SUCCESS = 0;
受保护的静态最终诠释INITIALIZE_FAILURE = 1;
TextView的labelSimplePayment;
的LinearLayout layoutSimplePayment;
CheckoutButton launchSimplePayment;
按钮ExitApp命令;
TextView的称号;
TextView的信息;
TextView的额外的;
TextView的labelKey;
TextView的appVersion;
EditText上输入preapprovalKey;
公共静态字符串resultTitle;
公共静态字符串resultInfo;
公共静态字符串resultExtra;
私人字符串isuename;
私人字符串isueprice;
处理器hRefresh =新的处理程序(){
@覆盖
公共无效的handleMessage(信息MSG){
开关(msg.what){
案例INITIALIZE_SUCCESS:
setupButtons();
打破;
案例INITIALIZE_FAILURE:
showFailure();
打破;
}
}
};
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
螺纹libraryInitializationThread =新的Thread(){
@覆盖
公共无效的run(){
initLibrary();
//库被初始化,让我们创建CheckoutButton和更新UI。
如果(PayPal.getInstance()。isLibraryInitialized()){
hRefresh.sendEmptyMessage(INITIALIZE_SUCCESS);
}
其他 {
hRefresh.sendEmptyMessage(INITIALIZE_FAILURE);
}
}
};
libraryInitializationThread.start();
isuename = getIntent()getStringExtra(姓名)修剪()。
。isueprice = getIntent()getStringExtra(代价)取代($,).trim();
Log.v(isuename,+ isuename);
Log.v(isueprice,+ isueprice);
的LinearLayout内容=新的LinearLayout(本);
content.setLayoutParams(新的LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT,android.view.ViewGroup.LayoutParams.FILL_PARENT));
content.setGravity(Gravity.CENTER_HORIZONTAL);
content.setOrientation(LinearLayout.VERTICAL);
content.setPadding(10,10,10,10);
content.setBackgroundColor(Color.WHITE);
layoutSimplePayment =新的LinearLayout(本);
layoutSimplePayment.setLayoutParams(新的LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT,android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
layoutSimplePayment.setGravity(Gravity.CENTER_HORIZONTAL);
layoutSimplePayment.setOrientation(LinearLayout.VERTICAL);
layoutSimplePayment.setPadding(0,5,0,5);
labelSimplePayment =新的TextView(本);
labelSimplePayment.setGravity(Gravity.CENTER_HORIZONTAL);
labelSimplePayment.setText(C&放大器; EN);
labelSimplePayment.setTextColor(Color.RED);
labelSimplePayment.setTextSize(45.0f);
layoutSimplePayment.addView(labelSimplePayment);
// labelSimplePayment.setVisibility(View.GONE);
content.addView(layoutSimplePayment);
的LinearLayout layoutKey =新的LinearLayout(本);
layoutKey.setLayoutParams(新的LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT,android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
layoutKey.setGravity(Gravity.CENTER_HORIZONTAL);
layoutKey.setOrientation(LinearLayout.VERTICAL);
layoutKey.setPadding(0,1,0,5);
输入preapprovalKey =新的EditText(本);
输入preapprovalKey.setLayoutParams(新的LayoutParams(200,45));
输入preapprovalKey.setGravity(Gravity.CENTER);
输入preapprovalKey.setSingleLine(真正的);
输入preapprovalKey.setHint(请输入PA键);
layoutKey.addView(输入preapprovalKey);
输入preapprovalKey.setVisibility(View.GONE);
labelKey =新的TextView(本);
labelKey.setGravity(Gravity.CENTER_HORIZONTAL);
labelKey.setPadding(0,-5,0,0);
labelKey.setText((用于preapproval));
layoutKey.addView(labelKey);
labelKey.setVisibility(View.GONE);
content.addView(layoutKey);
标题=新的TextView(本);
title.setLayoutParams(新LinearLayout.LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT,android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
title.setPadding(0,5,0,5);
title.setGravity(Gravity.CENTER_HORIZONTAL);
title.setTextSize(30.0f);
title.setVisibility(View.GONE);
content.addView(职称);
信息=新的TextView(本);
info.setLayoutParams(新LinearLayout.LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT,android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
info.setPadding(0,5,0,5);
info.setGravity(Gravity.CENTER_HORIZONTAL);
info.setTextSize(20.0f);
info.setVisibility(View.VISIBLE);
info.setText(请等待初始化贝宝......!);
info.setTextColor(Color.BLACK);
content.addView(信息);
额外=新的TextView(本);
extra.setLayoutParams(新LinearLayout.LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT,android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
extra.setPadding(0,5,0,5);
extra.setGravity(Gravity.CENTER_HORIZONTAL);
extra.setTextSize(12.0f);
extra.setVisibility(View.GONE);
content.addView(额外);
的LinearLayout layoutExit =新的LinearLayout(本);
layoutExit.setLayoutParams(新的LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT,android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
layoutExit.setGravity(Gravity.CENTER_HORIZONTAL);
layoutExit.setOrientation(LinearLayout.VERTICAL);
layoutExit.setPadding(0,15,0,5);
= ExitApp命令新的Button(本);
exitApp.setLayoutParams(新的LayoutParams(200,android.view.ViewGroup.LayoutParams.WRAP_CONTENT)); //半模拟PP按钮大小
exitApp.setOnClickListener(本);
exitApp.setText(退出);
layoutExit.addView(ExitApp命令);
content.addView(layoutExit);
appVersion =新的TextView(本);
appVersion.setGravity(Gravity.CENTER_HORIZONTAL);
appVersion.setPadding(0,-5,0,0);
appVersion.setText(\ñ\ nSimple演示构建+建设+\ nMPL库构建+ PayPal.getBuild());
content.addView(appVersion);
appVersion.setVisibility(View.GONE);
的setContentView(内容);
}
公共无效setupButtons(){
贝页= PayPal.getInstance();
//获取CheckoutButton。有五种不同的大小。按钮上的文字可以是类型TEXT_PAY或TEXT_DONATE的。
launchSimplePayment = pp.getCheckoutButton(这一点,PayPal.BUTTON_194x37,CheckoutButton.TEXT_PAY);
//你需要有一个OnClickListener的CheckoutButton。对于这种应用,MPL_Example实现OnClickListener和我们
//有以下的onClick()方法。
launchSimplePayment.setOnClickListener(本);
//该CheckoutButton是一个Android的LinearLayout,所以我们可以把它添加到我们的显示像任何其他视图。
layoutSimplePayment.addView(launchSimplePayment);
//获取CheckoutButton。有五种不同的大小。按钮上的文字可以是类型TEXT_PAY或TEXT_DONATE的。
//显示我们的标签和preapproval的EditText。
labelSimplePayment.setVisibility(View.VISIBLE);
info.setText();
info.setVisibility(View.GONE);
}
公共无效showFailure(){
title.setText(故障);
info.setText(无法初始化贝宝库。);
title.setVisibility(View.VISIBLE);
info.setVisibility(View.VISIBLE);
}
私人无效initLibrary(){
贝页= PayPal.getInstance();
如果(PP == NULL){
PP = PayPal.initWithAppID(这一点,APPID,服务器);
pp.setLanguage(EN_US); //设置库中的语言。
pp.setFeesPayer(PayPal.FEEPAYER_EACHRECEIVER);
//设置为true,如果该交易将需要运费。
pp.setShippingEnabled(真正的);
//动态金额计算,您可以设置基于用户的收货地址税款和运费金额。航运必须
//启用了动态金额计算。这也要求你创建一个实现PaymentAdjuster和序列化的类。
pp.setDynamicAmountCalculationEnabled(假);
// -
}
}
私人PayPalPayment exampleSimplePayment(){
//创建一个基本PayPalPayment。
PayPalPayment支付=新PayPalPayment();
//设置货币类型此付款。
payment.setCurrencyType(美元);
//设置收件人支付。这也可以是一个电话号码。
payment.setRecipient(harshd_1312435282_per@gmail.com);
//设置付款,不含税和运费金额的数额。
payment.setSubtotal(新的BigDecimal(isueprice));
//设置的付款方式。这可以是PAYMENT_TYPE_GOODS,PAYMENT_TYPE_SERVICE,PAYMENT_TYPE_PERSONAL,或PAYMENT_TYPE_NONE。
payment.setPaymentType(PayPal.PAYMENT_TYPE_GOODS);
// PayPalInvoiceData可以包含税和运费金额。它还包含PayPalInvoiceItem的ArrayList从而可以
//填写。这些不需要任何交易。
PayPalInvoiceData发票=新PayPalInvoiceData();
//设置税额。
invoice.setTax(新的BigDecimal(0));
//设置的出厂量。
invoice.setShipping(新的BigDecimal(0));
// PayPalInvoiceItem已经提供给它几个参数。所有这些参数是必需的。
PayPalInvoiceItem ITEM1 =新PayPalInvoiceItem();
//设置项目的名称。
item1.setName(isuename);
//设置ID。这是任何你想都与该项目相关的ID。
item1.setID(87239);
//设置总价应该是(数量*单价)。所有PayPalInvoiceItem的总价加起来应该
至小于或等于所述付款的小计//。
/ * item1.setTotalPrice(新的BigDecimal(2.99));
//设置单位价格。
item1.setUnitPrice(新的BigDecimal(2.00));
//设置数量。
item1.setQuantity(3); * /
//将PayPalInvoiceItem添加到PayPalInvoiceData。或者,您可以创建一个ArrayList< PayPalInvoiceItem>
//并把它传递给PayPalInvoiceData功能setInvoiceItems()。
。invoice.getInvoiceItems()加(item1的);
//创建并添加另一个PayPalInvoiceItem添加到PayPalInvoiceData。
/ * PayPalInvoiceItem项目2 =新PayPalInvoiceItem();
item2.setName(贺词);
item2.setID(56691);
item2.setTotalPrice(新的BigDecimal(2.25));
item2.setUnitPrice(新的BigDecimal(0.25));
item2.setQuantity(9);
。invoice.getInvoiceItems()加(ITEM2); * /
//设置PayPalPayment发票数据。
payment.setInvoiceData(发票);
//设置商户名称。这是你的应用程序或公司的名称。
payment.setMerchantName(C&放大器; EN);
//设置支付的说明。
payment.setDescription(简单赔付);
//设置自定义ID。这是任何你想都与支付相关的ID。
payment.setCustomID(8873482296);
//设置即时付款通知的URL。该URL将通过PayPal的服务器完成付款后,被击中。
//payment.setIpnUrl("http://www.exampleapp.com/ipn);
//设置备忘录。本备忘录将通过贝宝发送到必要的当事人通知的一部分。
payment.setMemo(你好!我是做一个备忘录付款。);
返回付款;
}
@覆盖
公共无效的onClick(视图v){
如果(V == launchSimplePayment){
//使用我们的辅助函数来创建简单的付款。
PayPalPayment支付= exampleSimplePayment();
//使用Google Checkout来创建我们的意图。
意图checkoutIntent = PayPal.getInstance()结算(支付,这一点,新ResultDelegate())。
//使用Android的startActivityForResult(),并通过我们的意图。这将启动库。
startActivityForResult(checkoutIntent,请求);
}否则,如果(V == ExitApp命令){
意图=新的意图();
in.putExtra(支付,无薪);
/*in.putExtra("condition,假); * /
的setResult(1英寸); //这里我设置请求code 1,你可以把根据您的要求
完();
}
}
@覆盖
公共无效onActivityResult(INT申请code,INT结果code,意图数据){
如果(要求code!=请求)
返回;
如果(main.resultTitle ==SUCCESS){
意图=新的意图();
in.putExtra(支付,付);
的setResult(22,中);
}否则,如果(main.resultTitle ==失败){
意图=新的意图();
in.putExtra(支付,无薪);
的setResult(22,中);
// 完();
}否则,如果(main.resultTitle ==取消){
意图=新的意图();
in.putExtra(支付,无薪);
的setResult(22,中);
// 完();
}
launchSimplePayment.updateButton();
title.setText(resultTitle);
title.setVisibility(View.VISIBLE);
info.setText(resultInfo);
info.setVisibility(View.VISIBLE);
extra.setText(resultExtra);
extra.setVisibility(View.VISIBLE);
完();
}
@覆盖
公共布尔的onkeydown(INT键code,KeyEvent的事件){
如果(键code == KeyEvent.KEY code_BACK){
意图=新的意图();
in.putExtra(支付,无薪);
的setResult(1,在);
完();
返回true;
}
返回super.onKeyDown(键code,事件);
}
I try to integrate paypal with Android app using sandbox. I'm getting successfully longing with paypal but when I am doing payment then Screen will get invisible directly without Response.
How can I get the response for the above question?
This is my code.
private void invokeSimplePayment()
{
try
{
PayPalPayment payment = new PayPalPayment();
payment.setSubtotal(new BigDecimal(Amt));
payment.setCurrencyType(Currency_code[code]);
payment.setRecipient("Rec_Email");
payment.setPaymentType(PayPal.PAYMENT_TYPE_GOODS);
Intent checkoutIntent = PayPal.getInstance().checkout(payment, this);
startActivityForResult(checkoutIntent, request);
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void onActivityResults(int requestCode, int resultCode, Intent data)
{
switch(resultCode)
{
case Activity.RESULT_OK:
resultTitle = "SUCCESS";
resultInfo = "You have successfully completed this " ;
//resultExtra = "Transaction ID: " + data.getStringExtra(PayPalActivity.EXTRA_PAY_KEY);
break;
case Activity.RESULT_CANCELED:
resultTitle = "CANCELED";
resultInfo = "The transaction has been cancelled.";
resultExtra = "";
break;
case PayPalActivity.RESULT_FAILURE:
resultTitle = "FAILURE";
resultInfo = data.getStringExtra(PayPalActivity.EXTRA_ERROR_MESSAGE);
resultExtra = "Error ID: " + data.getStringExtra(PayPalActivity.EXTRA_ERROR_ID);
}
System.out.println("Result=============="+resultTitle);
System.out.println("ResultInfo=============="+resultInfo);
}
This is my code and it works fine. There are two classes.
For paypal from sandbox to lliev envoirment you have to do 2 things: put the actual account holder in set reciepient and get a live ID by submitting your app to PayPal
public class ResultDelegate implements PayPalResultDelegate, Serializable {
private static final long serialVersionUID = 10001L;
public void onPaymentSucceeded(String payKey, String paymentStatus) {
main.resultTitle = "SUCCESS";
main.resultInfo = "You have successfully completed your transaction.";
main.resultExtra = "Key: " + payKey;
}
public void onPaymentFailed(String paymentStatus, String correlationID,
String payKey, String errorID, String errorMessage) {
main.resultTitle = "FAILURE";
main.resultInfo = errorMessage;
main.resultExtra = "Error ID: " + errorID + "\nCorrelation ID: "
+ correlationID + "\nPay Key: " + payKey;
}
public void onPaymentCanceled(String paymentStatus) {
main.resultTitle = "CANCELED";
main.resultInfo = "The transaction has been cancelled.";
main.resultExtra = "";
}
The main class:
public class main extends Activity implements OnClickListener {
// The PayPal server to be used - can also be ENV_NONE and ENV_LIVE
private static final int server = PayPal.ENV_LIVE;
// The ID of your application that you received from PayPal
private static final String appID = "APP-0N8000046V443613X";
// This is passed in for the startActivityForResult() android function, the value used is up to you
private static final int request = 1;
public static final String build = "10.12.09.8053";
protected static final int INITIALIZE_SUCCESS = 0;
protected static final int INITIALIZE_FAILURE = 1;
TextView labelSimplePayment;
LinearLayout layoutSimplePayment;
CheckoutButton launchSimplePayment;
Button exitApp;
TextView title;
TextView info;
TextView extra;
TextView labelKey;
TextView appVersion;
EditText enterPreapprovalKey;
public static String resultTitle;
public static String resultInfo;
public static String resultExtra;
private String isuename;
private String isueprice;
Handler hRefresh = new Handler(){
@Override
public void handleMessage(Message msg) {
switch(msg.what){
case INITIALIZE_SUCCESS:
setupButtons();
break;
case INITIALIZE_FAILURE:
showFailure();
break;
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
Thread libraryInitializationThread = new Thread() {
@Override
public void run() {
initLibrary();
// The library is initialized so let's create our CheckoutButton and update the UI.
if (PayPal.getInstance().isLibraryInitialized()) {
hRefresh.sendEmptyMessage(INITIALIZE_SUCCESS);
}
else {
hRefresh.sendEmptyMessage(INITIALIZE_FAILURE);
}
}
};
libraryInitializationThread.start();
isuename=getIntent().getStringExtra("name").trim();
isueprice=getIntent().getStringExtra("price").replace("$", "").trim();
Log.v("isuename ",""+isuename);
Log.v("isueprice ",""+isueprice);
LinearLayout content = new LinearLayout(this);
content.setLayoutParams(new LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT, android.view.ViewGroup.LayoutParams.FILL_PARENT));
content.setGravity(Gravity.CENTER_HORIZONTAL);
content.setOrientation(LinearLayout.VERTICAL);
content.setPadding(10, 10, 10, 10);
content.setBackgroundColor(Color.WHITE);
layoutSimplePayment = new LinearLayout(this);
layoutSimplePayment.setLayoutParams(new LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
layoutSimplePayment.setGravity(Gravity.CENTER_HORIZONTAL);
layoutSimplePayment.setOrientation(LinearLayout.VERTICAL);
layoutSimplePayment.setPadding(0, 5, 0, 5);
labelSimplePayment = new TextView(this);
labelSimplePayment.setGravity(Gravity.CENTER_HORIZONTAL);
labelSimplePayment.setText("C&EN");
labelSimplePayment.setTextColor(Color.RED);
labelSimplePayment.setTextSize(45.0f);
layoutSimplePayment.addView(labelSimplePayment);
// labelSimplePayment.setVisibility(View.GONE);
content.addView(layoutSimplePayment);
LinearLayout layoutKey = new LinearLayout(this);
layoutKey.setLayoutParams(new LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
layoutKey.setGravity(Gravity.CENTER_HORIZONTAL);
layoutKey.setOrientation(LinearLayout.VERTICAL);
layoutKey.setPadding(0, 1, 0, 5);
enterPreapprovalKey = new EditText(this);
enterPreapprovalKey.setLayoutParams(new LayoutParams(200, 45));
enterPreapprovalKey.setGravity(Gravity.CENTER);
enterPreapprovalKey.setSingleLine(true);
enterPreapprovalKey.setHint("Enter PA Key");
layoutKey.addView(enterPreapprovalKey);
enterPreapprovalKey.setVisibility(View.GONE);
labelKey = new TextView(this);
labelKey.setGravity(Gravity.CENTER_HORIZONTAL);
labelKey.setPadding(0, -5, 0, 0);
labelKey.setText("(Required for Preapproval)");
layoutKey.addView(labelKey);
labelKey.setVisibility(View.GONE);
content.addView(layoutKey);
title = new TextView(this);
title.setLayoutParams(new LinearLayout.LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
title.setPadding(0, 5, 0, 5);
title.setGravity(Gravity.CENTER_HORIZONTAL);
title.setTextSize(30.0f);
title.setVisibility(View.GONE);
content.addView(title);
info = new TextView(this);
info.setLayoutParams(new LinearLayout.LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
info.setPadding(0, 5, 0, 5);
info.setGravity(Gravity.CENTER_HORIZONTAL);
info.setTextSize(20.0f);
info.setVisibility(View.VISIBLE);
info.setText("Please Wait! Initializing Paypal...");
info.setTextColor(Color.BLACK);
content.addView(info);
extra = new TextView(this);
extra.setLayoutParams(new LinearLayout.LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
extra.setPadding(0, 5, 0, 5);
extra.setGravity(Gravity.CENTER_HORIZONTAL);
extra.setTextSize(12.0f);
extra.setVisibility(View.GONE);
content.addView(extra);
LinearLayout layoutExit = new LinearLayout(this);
layoutExit.setLayoutParams(new LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
layoutExit.setGravity(Gravity.CENTER_HORIZONTAL);
layoutExit.setOrientation(LinearLayout.VERTICAL);
layoutExit.setPadding(0, 15, 0, 5);
exitApp = new Button(this);
exitApp.setLayoutParams(new LayoutParams(200, android.view.ViewGroup.LayoutParams.WRAP_CONTENT)); //Semi mimic PP button sizes
exitApp.setOnClickListener(this);
exitApp.setText("Exit");
layoutExit.addView(exitApp);
content.addView(layoutExit);
appVersion = new TextView(this);
appVersion.setGravity(Gravity.CENTER_HORIZONTAL);
appVersion.setPadding(0, -5, 0, 0);
appVersion.setText("\n\nSimple Demo Build " + build + "\nMPL Library Build " + PayPal.getBuild());
content.addView(appVersion);
appVersion.setVisibility(View.GONE);
setContentView(content);
}
public void setupButtons() {
PayPal pp = PayPal.getInstance();
// Get the CheckoutButton. There are five different sizes. The text on the button can either be of type TEXT_PAY or TEXT_DONATE.
launchSimplePayment = pp.getCheckoutButton(this, PayPal.BUTTON_194x37, CheckoutButton.TEXT_PAY);
// You'll need to have an OnClickListener for the CheckoutButton. For this application, MPL_Example implements OnClickListener and we
// have the onClick() method below.
launchSimplePayment.setOnClickListener(this);
// The CheckoutButton is an android LinearLayout so we can add it to our display like any other View.
layoutSimplePayment.addView(launchSimplePayment);
// Get the CheckoutButton. There are five different sizes. The text on the button can either be of type TEXT_PAY or TEXT_DONATE.
// Show our labels and the preapproval EditText.
labelSimplePayment.setVisibility(View.VISIBLE);
info.setText("");
info.setVisibility(View.GONE);
}
public void showFailure() {
title.setText("FAILURE");
info.setText("Could not initialize the PayPal library.");
title.setVisibility(View.VISIBLE);
info.setVisibility(View.VISIBLE);
}
private void initLibrary() {
PayPal pp = PayPal.getInstance();
if(pp == null) {
pp = PayPal.initWithAppID(this, appID, server);
pp.setLanguage("en_US"); // Sets the language for the library.
pp.setFeesPayer(PayPal.FEEPAYER_EACHRECEIVER);
// Set to true if the transaction will require shipping.
pp.setShippingEnabled(true);
// Dynamic Amount Calculation allows you to set tax and shipping amounts based on the user's shipping address. Shipping must be
// enabled for Dynamic Amount Calculation. This also requires you to create a class that implements PaymentAdjuster and Serializable.
pp.setDynamicAmountCalculationEnabled(false);
// --
}
}
private PayPalPayment exampleSimplePayment() {
// Create a basic PayPalPayment.
PayPalPayment payment = new PayPalPayment();
// Sets the currency type for this payment.
payment.setCurrencyType("USD");
// Sets the recipient for the payment. This can also be a phone number.
payment.setRecipient("harshd_1312435282_per@gmail.com");
// Sets the amount of the payment, not including tax and shipping amounts.
payment.setSubtotal(new BigDecimal(isueprice));
// Sets the payment type. This can be PAYMENT_TYPE_GOODS, PAYMENT_TYPE_SERVICE, PAYMENT_TYPE_PERSONAL, or PAYMENT_TYPE_NONE.
payment.setPaymentType(PayPal.PAYMENT_TYPE_GOODS);
// PayPalInvoiceData can contain tax and shipping amounts. It also contains an ArrayList of PayPalInvoiceItem which can
// be filled out. These are not required for any transaction.
PayPalInvoiceData invoice = new PayPalInvoiceData();
// Sets the tax amount.
invoice.setTax(new BigDecimal("0"));
// Sets the shipping amount.
invoice.setShipping(new BigDecimal("0"));
// PayPalInvoiceItem has several parameters available to it. None of these parameters is required.
PayPalInvoiceItem item1 = new PayPalInvoiceItem();
// Sets the name of the item.
item1.setName(isuename);
// Sets the ID. This is any ID that you would like to have associated with the item.
item1.setID("87239");
// Sets the total price which should be (quantity * unit price). The total prices of all PayPalInvoiceItem should add up
// to less than or equal the subtotal of the payment.
/* item1.setTotalPrice(new BigDecimal("2.99"));
// Sets the unit price.
item1.setUnitPrice(new BigDecimal("2.00"));
// Sets the quantity.
item1.setQuantity(3);*/
// Add the PayPalInvoiceItem to the PayPalInvoiceData. Alternatively, you can create an ArrayList<PayPalInvoiceItem>
// and pass it to the PayPalInvoiceData function setInvoiceItems().
invoice.getInvoiceItems().add(item1);
// Create and add another PayPalInvoiceItem to add to the PayPalInvoiceData.
/*PayPalInvoiceItem item2 = new PayPalInvoiceItem();
item2.setName("Well Wishes");
item2.setID("56691");
item2.setTotalPrice(new BigDecimal("2.25"));
item2.setUnitPrice(new BigDecimal("0.25"));
item2.setQuantity(9);
invoice.getInvoiceItems().add(item2);*/
// Sets the PayPalPayment invoice data.
payment.setInvoiceData(invoice);
// Sets the merchant name. This is the name of your Application or Company.
payment.setMerchantName("C&EN");
// Sets the description of the payment.
payment.setDescription("simple payment");
// Sets the Custom ID. This is any ID that you would like to have associated with the payment.
payment.setCustomID("8873482296");
// Sets the Instant Payment Notification url. This url will be hit by the PayPal server upon completion of the payment.
//payment.setIpnUrl("http://www.exampleapp.com/ipn");
// Sets the memo. This memo will be part of the notification sent by PayPal to the necessary parties.
payment.setMemo("Hi! I'm making a memo for a payment.");
return payment;
}
@Override
public void onClick(View v) {
if(v == launchSimplePayment) {
// Use our helper function to create the simple payment.
PayPalPayment payment = exampleSimplePayment();
// Use checkout to create our Intent.
Intent checkoutIntent = PayPal.getInstance().checkout(payment, this, new ResultDelegate());
// Use the android's startActivityForResult() and pass in our Intent. This will start the library.
startActivityForResult(checkoutIntent, request);
} else if(v == exitApp) {
Intent in = new Intent();
in.putExtra("payment", "unpaid");
/*in.putExtra("condition", "false");*/
setResult(1,in);//Here I am Setting the Requestcode 1, you can put according to your requirement
finish();
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode != request)
return;
if(main.resultTitle=="SUCCESS"){
Intent in = new Intent();
in.putExtra("payment", "paid");
setResult(22,in);
}else if(main.resultTitle=="FAILURE"){
Intent in = new Intent();
in.putExtra("payment", "unpaid");
setResult(22,in);
// finish();
}else if(main.resultTitle=="CANCELED"){
Intent in = new Intent();
in.putExtra("payment", "unpaid");
setResult(22,in);
// finish();
}
launchSimplePayment.updateButton();
title.setText(resultTitle);
title.setVisibility(View.VISIBLE);
info.setText(resultInfo);
info.setVisibility(View.VISIBLE);
extra.setText(resultExtra);
extra.setVisibility(View.VISIBLE);
finish();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Intent in = new Intent();
in.putExtra("payment", "unpaid");
setResult(1,in);
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
这篇关于如何在Android应用程序集成贝宝?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!