android studio中的paypal支付
Posted
技术标签:
【中文标题】android studio中的paypal支付【英文标题】:paypal payment in android studio 【发布时间】:2021-11-08 23:17:10 【问题描述】:当我安装PayPalConfiguration.ENVIRONMENT_NO_NETWORK
时,我的付款很好,当然在贝宝沙盒上不会保存此交易,要保存交易我需要切换到PayPalConfiguration.ENVIRONMENT_SANDBOX
,但我收到这样的错误:
系统错误,请最后一次重试。
我提供的客户 ID 是正确的,我的 paypal 个人沙盒帐户是正确的(我在网站上检查了它们,我用 paypal bar 写了它,它工作正常。工作正常)但我不明白为什么通过 android studio 我得到这个错误,我哪里出错了?以下是我所有的代码
主活动:
package com.example.demo;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.paypal.android.sdk.payments.PayPalConfiguration;
import com.paypal.android.sdk.payments.PayPalPayment;
import com.paypal.android.sdk.payments.PayPalService;
import com.paypal.android.sdk.payments.PaymentActivity;
import com.paypal.android.sdk.payments.PaymentConfirmation;
import org.json.JSONException;
import java.math.BigDecimal;
public class MainActivity extends AppCompatActivity
Button btnPay;
private static PayPalConfiguration config = new PayPalConfiguration()
.environment(PayPalConfiguration.ENVIRONMENT_SANDBOX)
.clientId("AYjsqsGPCffv-niyJYub6xbIQA_8dhRTJbGG0HWVk8p7VX3GR8iOzMEk1HfgkKNw1wWScgXnt0OSC6Pv");
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnPay = findViewById(R.id.btnPay);
Intent intent = new Intent(this, PayPalService.class);
intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);
startService(intent);
btnPay.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
onBuyPressed();
);
public void onBuyPressed()
PayPalPayment payment = new PayPalPayment(new BigDecimal(10),
"USD","BINDAI PAYPAL",PayPalPayment.PAYMENT_INTENT_SALE);
Intent intent = new Intent(this, PaymentActivity.class);
intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);
intent.putExtra(PaymentActivity.EXTRA_PAYMENT, payment);
startActivityForResult(intent, 0);
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data)
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK)
PaymentConfirmation confirm = data.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION);
if (confirm != null)
try
Log.i("paymentExample", confirm.toJSONObject().toString(4));
Toast.makeText(this, "payment success", Toast.LENGTH_SHORT).show();
catch (JSONException e)
e.printStackTrace();
else if (resultCode == Activity.RESULT_CANCELED)
Log.i("paymentExample", "The user canceled");
else if (resultCode == PaymentActivity.RESULT_EXTRAS_INVALID)
Log.i("paymentExample", "An invalid Payment or ...");
@Override
protected void onDestroy()
stopService(new Intent(this, PayPalService.class));
super.onDestroy();
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
tools:context=".MainActivity"
android:orientation="vertical">
<Button
android:layout_
android:layout_
android:id="@+id/btnPay"
android:text="Paypal"/>
</LinearLayout>
build.gradle:
dependencies
// SDK PAYPAL - START
implementation 'com.paypal.sdk:paypal-android-sdk:2.14.6'
// SDK PAYPAL - END
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
我的贝宝账户:
username: bindai243156@gmail.com
Password: Bindai243156
(这只是我创建的用于演示paypal支付功能的账户,这个账户里面有沙箱账户可以测试)
【问题讨论】:
【参考方案1】:正确设置设备的本地时间。
【讨论】:
我是第一次尝试这种paypal技术,所以对它不是很熟悉,您能说得具体点吗?谢谢!以上是关于android studio中的paypal支付的主要内容,如果未能解决你的问题,请参考以下文章
Webview android中的Paypal支付页面布局损坏可能css未正确加载?
android studio(java)中的Paypal:与服务器通信的问题