安卓中的 FORT SDK 集成

Posted

技术标签:

【中文标题】安卓中的 FORT SDK 集成【英文标题】:FORT SDK integration in android 【发布时间】:2017-04-09 05:59:52 【问题描述】:

我想在我使用“FORT”sdk 的应用中集成支付功能。我确实遵循了“payfort.com”网站上的文档,但是当我集成库时,我的应用程序崩溃了。

这是错误输出:

E/androidRuntime: FATAL EXCEPTION: main
                                                                    Process: com.newsolution.jiibli, PID: 12133
                                                                    java.lang.RuntimeException: Unable to start activity ComponentInfocom.newsolution.jiibli/com.payfort.fort.android.sdk.activities.InitSecureConnectionActivity: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253)
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
                                                                        at android.app.ActivityThread.access$1100(ActivityThread.java:223)
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                        at android.os.Looper.loop(Looper.java:148)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:7223)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
                                                                     Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
                                                                        at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:343)
                                                                        at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:312)
                                                                        at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:277)
                                                                        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
                                                                        at com.payfort.fort.android.sdk.activities.InitSecureConnectionActivity.onCreate(InitSecureConnectionActivity.java:50)
                                                                        at android.app.Activity.performCreate(Activity.java:6877)
                                                                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349) 
                                                                        at android.app.ActivityThread.access$1100(ActivityThread.java:223) 
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) 
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                        at android.os.Looper.loop(Looper.java:148) 
                                                                        at android.app.ActivityThread.main(ActivityThread.java:7223) 
                                                                        at java.lang.reflect.Method.invoke(Native Method) 
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

【问题讨论】:

您的活动是否从AppCompatActivity延伸? 不,它是从 Activity 扩展而来的 我将其更改为从 AppCompatActivity 扩展,但是当我将 sdk 与新应用程序一起使用时,我遇到了同样的问题,它可以工作,但当前应用程序不行 Amal 你有没有找到解决这个问题的方法 我真的不记得我到底做了什么,所以我将添加我用 sdk 做的事情 【参考方案1】:

在 Manifest.xml 中:我从 AppCompactTheme 扩展样式

         <activity   android:name=".Activity.PaymentMethod"
             android:theme="@style/AppTheme"/>

在 gradle 中导入库

    compile project(':FORTSDKv1.2')

我以结果为目的启动 PaymentMethod 活动: 调用方法pay();

  @Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    activity = this;
    setContentView(R.layout.activity_payment_method);
    fortCallback = FortCallback.Factory.create();
    pay();
    
   public void pay() 
    try 
        ProviderInstaller.installIfNeeded(getApplicationContext());
     catch (GooglePlayServicesRepairableException e) 
        e.printStackTrace();
     catch (GooglePlayServicesNotAvailableException e) 
        e.printStackTrace();
    
    new sdkToken().execute();// get SDK Token and pass it to paid method


class sdkToken extends AsyncTask<String, Void, String> 
    public sdkToken() 
    

    protected void onPreExecute() 
        super.onPreExecute();
    

    protected String doInBackground(String... params) 
        signature = getSignature();
        String jsonRequestString = "\"access_code\" : \""+access_code+"\" " +
                ", \"service_command\" : \"SDK_TOKEN\", \"language\" : \"en\","
                + "\"merchant_identifier\" : \""+merchant_identifier+"\", \"signature\" : \"" + signature + "\", "
                + "\"device_id\" : \"" + FortSdk.getDeviceId(PaymentMethod.this) + "\"";

        HttpParams httpParameters = new BasicHttpParams();
        // Set the timeout in milliseconds until a connection is established.
        int timeoutConnection = 10000;
        HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
        // Set the default socket timeout (SO_TIMEOUT)
        // in milliseconds which is the timeout for waiting for data.
        int timeoutSocket = 10000;
        HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

        // Instantiate the custom HttpClient
        HttpClient httpclient = new MyHttpClient(httpParameters, getApplicationContext());
        // DefaultHttpClient httpclient =new DefaultHttpClient();
        HttpPost request = new HttpPost("https://sbpaymentservices.payfort.com/FortAPI/paymentApi");
        StringEntity param = null;
        try 
            param = new StringEntity(jsonRequestString);// Setup Http POST entity with JSON String
            // Setup request type as JSON
            request.addHeader("content-type", "application/json");
            request.setEntity(param);
            HttpResponse response = null;
            response = httpclient.execute(request);   // Post request to FORT
            sb = new StringBuilder(); // Read response using StringBuilder
            BufferedReader reader = null;
            reader = new BufferedReader(new InputStreamReader(
                    response.getEntity().getContent()), 65728);
            String line = null;
            while ((line = reader.readLine()) != null) 
                sb.append(line);
            
         catch (Exception e) 
            e.printStackTrace();
            return null;
        
        return "" + sb.toString();
    

    protected void onPostExecute(String fortResponse) 
        super.onPostExecute(fortResponse);
        if (fortResponse != null) 
            JSONObject jsonObject = null;
            System.out.println(sb.toString());
            try 
                jsonObject = new JSONObject(fortResponse);
                sdk_token = jsonObject.getString("sdk_token");
                System.out.println(sdk_token);
                progressBar.setVisibility(View.GONE);
                paid(sdk_token);
             catch (JSONException e) 
                e.printStackTrace();
            
         else 
            progressBar.setVisibility(View.GONE);
            Log.i("fortResponse", " fortResponse==null");
            Toast.makeText(activity, getString(R.string.compatible), Toast.LENGTH_LONG).show();
        
    


private void paid(String sdk_token) 
    fortRequest = new FortRequest();
    Map<String, String> requestMap = new HashMap<>();
    requestMap.put("merchant_reference", random());
    requestMap.put("language", "en");
    requestMap.put("order_description", "android");
    requestMap.put("currency", "SAR");
    requestMap.put("amount", "100");
    requestMap.put("command", "AUTHORIZATION");//PURCHASE
    requestMap.put("customer_name", "add any name");
    requestMap.put("eci", "ECOMMERCE");
    requestMap.put("customer_email", "add email");
    requestMap.put("sdk_token", sdk_token);
    fortRequest.setShowResponsePage(true);
    fortRequest.setRequestMap(requestMap);
    try 
        FortSdk.getInstance().registerCallback(this, fortRequest, FortSdk.ENVIRONMENT.TEST, 5, fortCallback,
                new FortInterfaces.OnTnxProcessed() 
                    @Override
                    public void onCancel(Map<String, String> requestParamsMap, Map<String,
                            String> responseMap) 
                        //TODO: handle me
                        JSONObject responseObject = new JSONObject(responseMap);
                        try 
                            String response_message = responseObject.getString("response_message");
                            paymentType = 0;
                            Toast.makeText(PaymentMethod.this, "" + response_message, Toast.LENGTH_LONG).show();
                         catch (JSONException e) 
                            e.printStackTrace();
                        
                    

                    @Override
                    public void onSuccess(Map<String, String> requestParamsMap, Map<String,
                            String> fortResponseMap) 
                        System.out.println("success requestParamsMap " + requestParamsMap);
                        System.out.println("success fortResponseMap " + fortResponseMap);
                        JSONObject responseObject = new JSONObject(fortResponseMap);
                        try 
                            String response_message = responseObject.getString("response_message");
                            Toast.makeText(PaymentMethod.this, "" + response_message, Toast.LENGTH_LONG).show();
                            Intent intent = new Intent();
                            intent.putExtra("cardNo", cardNo);
                            intent.putExtra("cardId", cardId);
                            setResult(2, intent);
                            finish();
                         catch (JSONException e) 
                            e.printStackTrace();
                        
                    

                    @Override
                    public void onFailure(Map<String, String> requestParamsMap, Map<String,
                            String> fortResponseMap) 
                        System.out.println("requestParamsMap " + requestParamsMap);
                        System.out.println("fortResponseMap " + fortResponseMap);
                        JSONObject responseObject = new JSONObject(fortResponseMap);
                        try 
                            String response_message = responseObject.getString("response_message");
                            Toast.makeText(PaymentMethod.this, response_message, Toast.LENGTH_LONG).show();
                            Intent intent = new Intent(PaymentMethod.this, PayFail.class);
                            intent.putExtra("cardNo", cardNo);
                            intent.putExtra("cardType", cardType);
                            intent.putExtra("cardPersonName", cardPersonName);
                            startActivity(intent);
                         catch (JSONException e) 
                            e.printStackTrace();
                        
                    
                );
     catch (Exception e) 
        e.printStackTrace();
    



@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    super.onActivityResult(requestCode, resultCode, data);
        try 
            fortCallback.onActivityResult(requestCode, resultCode, data);
         catch (Exception e) 
            Log.i("exception", e.getMessage());
        


private String getSignature() 
    MessageDigest md = null;
    try 
        md = MessageDigest.getInstance("SHA-256");
        String text = "sadafdfeeeeaccess_code="+access_code+"
                + "device_id=" + FortSdk.getDeviceId(PaymentMethod.this)
                + "language=enmerchant_identifier="+merchant_identifier"+service_command=SDK_TOKENsadafdfeeee";
        md.update(text.getBytes("UTF-8")); // Change this to "UTF-16" if needed

     catch (Exception e) 
        e.printStackTrace();
    
    byte[] digest = md.digest();
    signature = String.format("%064x", new java.math.BigInteger(1, digest));
    return signature;


public static String random() 
    SecureRandom secureRandom = new SecureRandom();
    return new BigInteger(40, secureRandom).toString(32);

【讨论】:

【参考方案2】:

诀窍是什么,manifest 文件与 payfort sdk 冲突,你必须在 application 标签中使用 overide manifest

【讨论】:

将 Theme.Transparent 的父级更改为 Theme.AppCompat.Light.NoActionBar

以上是关于安卓中的 FORT SDK 集成的主要内容,如果未能解决你的问题,请参考以下文章

如何将安卓版智能生活 App SDK 集成到开发环境中?

智能生活 App 垂直品类- IPC SDK 架构及快速集成配置(安卓版)

集成安卓开发工具的eclipse,直接打开就能用吗?用不用下载android sdk?

安卓 短信验证MobSMS集成

亚马逊云盘与安卓集成

DK:安卓车牌识别/Android车牌识别SDK/Android车牌识别软件