2

我正在尝试使用 PayPal 去年底发布的最新实现将 PayPal 支付流集成到我的 Android 应用程序中。但不幸的是,我无法让它工作。知道我做错了什么吗?您能给我的任何帮助将不胜感激。

这是我的代码:

public class PM_Fragment extends Fragment {
    private static final String CONFIG_ENVIRONMENT = //Enter the correct environment here;
    private static final String CONFIG_CLIENT_ID = //you need to register with PayPal and enter your client_ID here;

    private static final int REQUEST_CODE_PAYMENT = 1;
    private static final int REQUEST_CODE_FUTURE_PAYMENT = 2;

    private static PayPalConfiguration config = new PayPalConfiguration()
        .environment(CONFIG_ENVIRONMENT)
        .clientId(CONFIG_CLIENT_ID)
        .acceptCreditCards(true)
        .languageOrLocale("EN")
        .rememberUser(true)
        .merchantName("Company name");

    EditText Name, Age;
    Spinner amount;
    Button Donate;

    @Override
    public View onCreateView(LayoutInflater inflater,
        ViewGroup container, Bundle savedInstanceState) {
        /**
        * Inflate the layout for this fragment
        */
        View rootView= inflater.inflate(
            R.layout.pm_fragment, container, false);

        Name = (EditText) rootView.findViewById(R.id.editText1);
        Age = (EditText) rootView.findViewById(R.id.editText2);
        amount = (Spinner) rootView.findViewById(R.id.spinner1);
        Donate = (Button) rootView.findViewById(R.id.button1);

        Donate.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                onBuyPressed(v);
            }
        });

        initPaymentService();

        return rootView;
    }

    public void initPaymentService() {
        try {
            Intent intent = new Intent(getActivity(), PayPalService.class);
            intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);

            getActivity().startService(intent);
        } catch (Exception e) {
            Log.i("PayPal Exception", e.getMessage());
        }
    }

    public double onChoiceMade() {
        int pos = amount.getSelectedItemPosition();
        double payment;

        if (pos == 0) {
            payment = 5.00;
        } else if (pos == 1) {
            payment = 10.00;
        } else if (pos == 2) {
            payment = 20.00;
        } else {
            payment = 50.00;
        }
        return payment;
    }

    public void onBuyPressed(View pressed) {
        try{
            int age = Integer.parseInt(Age.getText().toString());
            if (age >= 18 && age < 99) {
                PayPalPayment thingToBuy = new PayPalPayment(new BigDecimal(onChoiceMade()), "EUR", "Rockestra Donation", PayPalPayment.PAYMENT_INTENT_SALE);

                Intent intent = new Intent(getActivity(), PaymentActivity.class);
                intent.putExtra(PaymentActivity.EXTRA_PAYMENT, thingToBuy);
                startActivityForResult(intent, REQUEST_CODE_PAYMENT);
            } else {
                Toast.makeText(getActivity(), "You must be 18 years of age to Process a payment. Please enter a correct age.", Toast.LENGTH_LONG).show();
                Age.setText("");
            }
        } catch (NumberFormatException e){
            Toast.makeText(getActivity(), "Age value cannot be empty. \n Please enter a valid age.", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE_PAYMENT) {
            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(getActivity().getApplicationContext(), "PaymentConfirmation info received from PayPal",
                            Toast.LENGTH_LONG).show();
                    } catch (JSONException e) {
                        Log.e("paymentExample", "an extremely unlikely failure occurred: ", e);
                    }
                }
            } 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 was submitted. Please see the docs.");
            }
        } else if (requestCode == REQUEST_CODE_FUTURE_PAYMENT) {
            if (resultCode == Activity.RESULT_OK) {
                PayPalAuthorization auth = data
                    .getParcelableExtra(PayPalFuturePaymentActivity.EXTRA_RESULT_AUTHORIZATION);
                if (auth != null) {
                    try {
                        Log.i("FuturePaymentExample", auth.toJSONObject().toString(4));

                        String authorization_code = auth.getAuthorizationCode();
                        Log.i("FuturePaymentExample", authorization_code);

                        sendAuthorizationToServer(auth);
                        Toast.makeText(getActivity().getApplicationContext(), "Future Payment code received from PayPal",
                            Toast.LENGTH_LONG).show();
                    } catch (JSONException e) {
                        Log.e("FuturePaymentExample", "an extremely unlikely failure occurred: ", e);
                    }
                }
            } else if (resultCode == Activity.RESULT_CANCELED) {
                Log.i("FuturePaymentExample", "The user canceled.");
            } else if (resultCode == PayPalFuturePaymentActivity.RESULT_EXTRAS_INVALID) {
                Log.i("FuturePaymentExample",
                      "Probably the attempt to previously start the PayPalService had an invalid PayPalConfiguration. Please see the docs.");
            }
        }
    }

    private void sendAuthorizationToServer(PayPalAuthorization authorization) {
    }

    @Override
    public void onDestroy() {
        // Stop service when done
        getActivity().stopService(new Intent(getActivity(), PayPalService.class));
        super.onDestroy();
    }
}
4

1 回答 1

2

我在片段中实现了以下代码。请看下面。请注意,您需要进行一些更改,因此我也查看了 PayPal 文档。

班级代码:

public class DonationFragment extends Fragment {
     private static final String CONFIG_ENVIRONMENT = //Enter the correct environment here;
     private static final String CONFIG_CLIENT_ID = //you need to register with PayPal and enter your client_ID here;

        private static final int REQUEST_CODE_PAYMENT = 1;
        private static final int REQUEST_CODE_FUTURE_PAYMENT = 2;

        private static PayPalConfiguration config = new PayPalConfiguration()
                .environment(CONFIG_ENVIRONMENT)
                .clientId(CONFIG_CLIENT_ID)
                .acceptCreditCards(true)
                .languageOrLocale("EN")
                .rememberUser(true)
                .merchantName("Company name");

        EditText Name, Age;
        Spinner amount;
        Button Donate;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_donations, container, false);

            Name = (EditText) rootView.findViewById(R.id.editText1);
            Age = (EditText) rootView.findViewById(R.id.editText2);
            amount = (Spinner) rootView.findViewById(R.id.spinner1);
            Donate = (Button) rootView.findViewById(R.id.button1);

            Donate.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    onBuyPressed(v);
                }

            });

            initPaymentService();

            return rootView;
        }

        public void initPaymentService() {
            try {
                Intent intent = new Intent(getActivity(), PayPalService.class);
                 intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);

             getActivity().startService(intent);
            } catch (Exception e) {
                Log.i("PayPal Exception", e.getMessage());
            }
        }

        public double onChoiceMade() {
            int pos = amount.getSelectedItemPosition();
            double payment;

            if (pos == 0) {
                payment = 5.00;
            }else if (pos == 1) {
                payment = 10.00;
            }else if (pos == 2) {
                payment = 20.00;
            }else {
                payment = 50.00;
            }
        return payment;
        }

        public void onBuyPressed(View pressed) {

            try{
                int age = Integer.parseInt(Age.getText().toString());
                if (age >= 18 && age < 99) {
                    PayPalPayment thingToBuy = new PayPalPayment(new BigDecimal(onChoiceMade()), "EUR", "Rockestra Donation", PayPalPayment.PAYMENT_INTENT_SALE);

                    Intent intent = new Intent(getActivity(), PaymentActivity.class);
                    intent.putExtra(PaymentActivity.EXTRA_PAYMENT, thingToBuy);
                    startActivityForResult(intent, REQUEST_CODE_PAYMENT);
                }
                else  {
                    Toast.makeText(getActivity(), "You must be 18 years of age to Process a payment. Please enter a correct age.", Toast.LENGTH_LONG).show();
                    Age.setText("");
                }
            }catch (NumberFormatException e){
                Toast.makeText(getActivity(), "Age value cannot be empty. \n Please enter a valid age.", Toast.LENGTH_LONG).show();
                }
        }

        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (requestCode == REQUEST_CODE_PAYMENT) {
                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(getActivity().getApplicationContext(), "PaymentConfirmation info received from PayPal",
                                    Toast.LENGTH_LONG).show();

                        } catch (JSONException e) {
                            Log.e("paymentExample", "an extremely unlikely failure occurred: ", e);
                        }
                    }
                } 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 was submitted. Please see the docs.");
                }
            } else if (requestCode == REQUEST_CODE_FUTURE_PAYMENT) {
                if (resultCode == Activity.RESULT_OK) {
                    PayPalAuthorization auth = data
                            .getParcelableExtra(PayPalFuturePaymentActivity.EXTRA_RESULT_AUTHORIZATION);
                    if (auth != null) {
                        try {
                            Log.i("FuturePaymentExample", auth.toJSONObject().toString(4));

                            String authorization_code = auth.getAuthorizationCode();
                            Log.i("FuturePaymentExample", authorization_code);

                            sendAuthorizationToServer(auth);
                            Toast.makeText(getActivity().getApplicationContext(), "Future Payment code received from PayPal",
                                    Toast.LENGTH_LONG).show();

                        } catch (JSONException e) {
                            Log.e("FuturePaymentExample", "an extremely unlikely failure occurred: ", e);
                        }
                    }
                } else if (resultCode == Activity.RESULT_CANCELED) {
                    Log.i("FuturePaymentExample", "The user canceled.");
                } else if (resultCode == PayPalFuturePaymentActivity.RESULT_EXTRAS_INVALID) {
                    Log.i("FuturePaymentExample",
                            "Probably the attempt to previously start the PayPalService had an invalid PayPalConfiguration. Please see the docs.");
                }
            }
        }

        private void sendAuthorizationToServer(PayPalAuthorization authorization) {

        }

        @Override
        public void onDestroy() {
            // Stop service when done
            getActivity().stopService(new Intent(getActivity(), PayPalService.class));
            super.onDestroy();
        }
}

请注意,我已经实现了一个微调器来选择在这个程序中捐赠的金额。此外,我还包含了一个检查,以确保用户的年龄大于 18 岁且小于 100 岁。

XML 布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/bg_black"
    android:gravity="center_horizontal|center_vertical"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:text="Name (optional):"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="262dp"
        android:layout_height="wrap_content"
        android:ems="10"
        android:gravity="center_vertical"
        android:inputType="textPersonName" >

        <requestFocus />
    </EditText>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:gravity="center"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:text="Age:"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/editText2"
            android:layout_width="79dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:gravity="center_vertical"
            android:inputType="number" />
    </LinearLayout>

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:text="Amount to donate:"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="276dp"
        android:layout_height="wrap_content"
        android:entries="@array/donations"
        android:gravity="center_vertical" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="141dp"
        android:gravity="center"
        android:orientation="vertical" >

        <Button
            android:id="@+id/button1"
            android:layout_width="180dp"
            android:layout_height="49dp"
            android:background="@drawable/checkout"
            android:text="Donate"
            android:textColor="#000000" />

    </LinearLayout>

</LinearLayout>

希望这可以帮助 :)

于 2014-06-18T09:26:54.227 回答