1
votes

I have implemented some native android paypal integration code under native interface in codenameone. Call from codenameOne:

MyNative my = (MyNative)NativeLookup.create(MyNative.class);

In the Native Interface:

package com.mycompany.myapp;
import com.codename1.system.NativeInterface;
public interface MyNative extends NativeInterface{   

}

I have given proper android.xapplication under build_hint and write android code under the impl class:

    package com.mycompany.myapp;
    import java.math.BigDecimal;

    import android.app.Activity;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;

    import android.widget.Toast;

    import com.paypal.android.sdk.payments.PayPalAuthorization;
    import com.paypal.android.sdk.payments.PayPalConfiguration;
    import com.paypal.android.sdk.payments.PayPalFuturePaymentActivity;
    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;

    public class MyNativeImpl extends Activity{
    // private static final String TAG = "paymentdemoblog";
    /**
     * - Set to PaymentActivity.ENVIRONMENT_PRODUCTION to move real money.
     * 
     * - Set to PaymentActivity.ENVIRONMENT_SANDBOX to use your test credentials
     * from https://developer.paypal.com
     * 
     * - Set to PayPalConfiguration.ENVIRONMENT_NO_NETWORK to kick the tires
     * without communicating to PayPal's servers.
     */
    // private static final String CONFIG_ENVIRONMENT =
    // PayPalConfiguration.ENVIRONMENT_NO_NETWORK;
    private static final String CONFIG_ENVIRONMENT = PayPalConfiguration.ENVIRONMENT_SANDBOX;

    // note that these credentials will differ between live & sandbox
    // environments.
    private static final String CONFIG_CLIENT_ID = "Aeqc2X1rBIEUtDNqsaRNr0h1neFo9QnNmfgmpA3D32uSLaHpGJu9NV1KfMnFmy7O-_hV47I7ST0SXDW2";

    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)
            // The following are only used in PayPalFuturePaymentActivity.
            .merchantName("Hipster Store")
            .merchantPrivacyPolicyUri(
                    Uri.parse("https://www.example.com/privacy"))
            .merchantUserAgreementUri(
                    Uri.parse("https://www.example.com/legal"));

    PayPalPayment thingToBuy;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent intent = new Intent(this, PayPalService.class);
        intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);
        startService(intent);
        findViewById(R.id.order).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                thingToBuy = new PayPalPayment(new BigDecimal("10"), "USD",
                        "HeadSet", PayPalPayment.PAYMENT_INTENT_SALE);

                Intent intent = new Intent(MyNativeImpl.this,
                        PaymentActivity.class);

                intent.putExtra(PaymentActivity.EXTRA_PAYMENT, thingToBuy);

                startActivityForResult(intent, REQUEST_CODE_PAYMENT);
            }
        });

    }

    public void onFuturePaymentPressed(View pressed) {
        Intent intent = new Intent(MyNativeImpl.this,
                PayPalFuturePaymentActivity.class);

        startActivityForResult(intent, REQUEST_CODE_FUTURE_PAYMENT);
    }

    @Override
    protected 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 {
                        System.out.println(confirm.toJSONObject().toString(4));
                        System.out.println(confirm.getPayment().toJSONObject()
                                .toString(4));

                        Toast.makeText(getApplicationContext(), "Order placed",
                                Toast.LENGTH_LONG).show();

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            } else if (resultCode == Activity.RESULT_CANCELED) {
                System.out.println("The user canceled.");
            } else if (resultCode == PaymentActivity.RESULT_EXTRAS_INVALID) {
                System.out
                        .println("An invalid Payment or PayPalConfiguration 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(getApplicationContext(),
                                "Future Payment code received from PayPal",
                                Toast.LENGTH_LONG).show();

                    } catch (Exception e) {
                        Log.e("FuturePaymentExample",
                                "an extremely unlikely failure occurred: ", e);
                                                e.printStackTrace();
                    }
                }
            } 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) {

    }

    public void onFuturePaymentPurchasePressed(View pressed) {
        // Get the Application Correlation ID from the SDK
        String correlationId = PayPalConfiguration
                .getApplicationCorrelationId(this);

        Log.i("FuturePaymentExample", "Application Correlation ID: "
                + correlationId);

        // TODO: Send correlationId and transaction details to your server for
        // processing with
        // PayPal...
        Toast.makeText(getApplicationContext(),
                "App Correlation ID received from SDK", Toast.LENGTH_LONG)
                .show();
    }

    @Override
    public void onDestroy() {
        // Stop service when done
        stopService(new Intent(this, PayPalService.class));
        super.onDestroy();
    }
    public boolean isSupported() {
        return true;
    }

}

Now I am getting exception during android build:

All input files are considered out-of-date for incremental task ':compileDebugJavaWithJavac'. Compiling with source level 1.7 and target level 1.7. :compileDebugJavaWithJavac - is not incremental (e.g. outputs have changed, no previous execution, etc.). file or directory '/tmp/build220258639476712910xxx/MyApplication/src/debug/java', not found Compiling with JDK Java compiler API. /tmp/build220258639476712910xxx/MyApplication/src/main/java/com/mycompany/myapp/MyNativeImpl.java:62: error: cannot find symbol setContentView(R.layout.activity_main); ^ symbol: variable activity_main location: class layout /tmp/build220258639476712910xxx/MyApplication/src/main/java/com/mycompany/myapp/MyNativeImpl.java:74: error: cannot find symbol Intent intent = new Intent(MainActivity.this, ^ symbol: class MainActivity /tmp/build220258639476712910xxx/MyApplication/src/main/java/com/mycompany/myapp/MyNativeImpl.java:67: error: cannot find symbol findViewById(R.id.order).setOnClickListener(new OnClickListener() { ^ symbol: variable order location: class id /tmp/build220258639476712910xxx/MyApplication/src/main/java/com/mycompany/myapp/MyNativeImpl.java:86: error: cannot find symbol Intent intent = new Intent(MainActivity.this, ^ symbol: class MainActivity location: class MyNativeImpl Note: Some input files use or override a deprecated API. Note: Recompile with -Xlint:deprecation for details. Note: Some input files use unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. 4 errors :compileDebugJavaWithJavac FAILED :compileDebugJavaWithJavac (Thread[Daemon worker,5,main]) completed. Took 10.51 secs.

FAILURE: Build failed with an exception.

Can anyone please help....

1
You seem to be running into some issues with PayPal integration, if you have an enterprise account you should contact our enterprise support for help. We can probably do the whole thing for you.Shai Almog
I've noticed you don't accept or upvote questions. It is generally good policy to answer and accept to indicate that an issue was resolved. This also gives points to the person answering and also to you which are helpful for many things on StackOverflow.Shai Almog

1 Answers

0
votes

There is no activity_main in the XML bundle which is why the auto-generated R class from Android can't find it.

I also noticed you inherited Activity in the impl class which is wrong you should create a separate activity class assuming that's actually what you want to do (you might need to interface with the core Codename One activity).

Since I'm unfamiliar with the PayPal integration I'm guessing that activity_main refers to your own activity which is really the CodenameOneActivity. In that sense most of that code is redundant as we already generate that, you just need to bind the calls into the paypal native code.