Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- AndroidManifest.xml
- ----------------------
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.braintreekotlinsample">
- <uses-permission android:name="android.permission.INTERNET"/>
- <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:roundIcon="@mipmap/ic_launcher_round"
- android:supportsRtl="true"
- android:theme="@style/AppTheme">
- <activity android:name=".MainActivity">
- <intent-filter>
- <action android:name="android.intent.action.MAIN"/>
- <category android:name="android.intent.category.LAUNCHER"/>
- </intent-filter>
- </activity>
- <activity android:name="com.braintreepayments.api.BraintreeBrowserSwitchActivity"
- android:launchMode="singleTask">
- <intent-filter>
- <action android:name="android.intent.action.VIEW" />
- <category android:name="android.intent.category.DEFAULT" />
- <category android:name="android.intent.category.BROWSABLE" />
- <data android:scheme="${applicationId}.braintree" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
- ---------------------------------------
- activity_main.xml
- ---------------------------------------
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity">
- <LinearLayout
- android:id="@+id/waiting_group"
- android:layout_centerInParent="true"
- android:orientation="vertical"
- android:visibility="visible"
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Please wait for the token to get"
- android:layout_gravity="center_horizontal"/>
- </LinearLayout>
- <LinearLayout
- android:id="@+id/payment_group"
- android:layout_centerInParent="true"
- android:orientation="vertical"
- android:visibility="gone"
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="CHECKOUT"
- android:layout_gravity="center_horizontal"
- android:textStyle="bold"
- android:textSize="20sp"/>
- <android.support.design.widget.TextInputLayout android:layout_width="match_parent"
- android:layout_height="wrap_content">
- <EditText
- android:id="@+id/edt_payment"
- android:hint="Choose the amount to pay(USD)"
- android:gravity="center_horizontal"
- android:inputType="number"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"/>
- </android.support.design.widget.TextInputLayout>
- <Button
- android:id="@+id/btn_pay"
- android:text="PAY"
- android:layout_gravity="center_horizontal"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>
- </LinearLayout>
- </RelativeLayout>
- -----------------------------------------
- MainActivity.kt
- -----------------------------------------
- package com.example.braintreekotlinsample
- import android.app.Activity
- import android.app.VoiceInteractor
- import android.content.Intent
- import android.os.AsyncTask
- import android.support.v7.app.AppCompatActivity
- import android.os.Bundle
- import android.os.Parcel
- import android.os.Parcelable
- import android.util.Log
- import android.view.View
- import android.widget.Toast
- import com.android.volley.Request
- import com.android.volley.RequestQueue
- import com.android.volley.Response
- import com.android.volley.toolbox.StringRequest
- import com.android.volley.toolbox.Volley
- import com.braintreepayments.api.dropin.DropInActivity
- import com.braintreepayments.api.dropin.DropInRequest
- import com.braintreepayments.api.dropin.DropInResult
- import com.braintreepayments.api.models.PaymentMethodNonce
- import com.loopj.android.http.AsyncHttpClient
- import com.loopj.android.http.TextHttpResponseHandler
- import cz.msebera.android.httpclient.Header
- import kotlinx.android.synthetic.main.activity_main.*
- import java.io.File
- import kotlin.Exception
- class MainActivity : AppCompatActivity() {
- companion object{
- val API_GET_TOKEN = "TOKEN LINK HERE"
- val API_CHECKOUT = "CHECKOUT LINK"
- val REQUEST_CODE: Int = 111
- internal lateinit var token:String
- internal lateinit var amount:String
- internal var paramsHash:HashMap<String,String>?=null
- }
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- getToken()
- //Event
- btn_pay.setOnClickListener {
- val dropInRequest = DropInRequest().clientToken(token)
- startActivityForResult(dropInRequest.getIntent(this),REQUEST_CODE)
- }
- }
- private fun getToken(){
- val androidClient = AsyncHttpClient()
- androidClient.get(API_GET_TOKEN, object:TextHttpResponseHandler(){
- override fun onSuccess(statusCode: Int, headers: Array<out Header>?, responseString: String?) {
- runOnUiThread {
- payment_group.visibility = View.VISIBLE
- waiting_group.visibility = View.GONE
- token = responseString!!.toString()
- }
- }
- override fun onFailure(
- statusCode: Int,
- headers: Array<out Header>?,
- responseString: String?,
- throwable: Throwable?
- ) {
- runOnUiThread {
- Toast.makeText(this@MainActivity,"Failed to load token:"+throwable.toString(),Toast.LENGTH_SHORT).show()
- }
- }
- })
- }
- override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
- if(requestCode == REQUEST_CODE){
- }
- if(requestCode == Activity.RESULT_OK){
- val result:DropInResult = data!!.getParcelableExtra<DropInResult>(DropInResult.EXTRA_DROP_IN_RESULT)
- val nonce:PaymentMethodNonce? = result.paymentMethodNonce
- val stringNonce:String = nonce!!.nonce
- if(!edt_payment.text.toString().isEmpty()){
- amount = edt_payment.text.toString()
- paramsHash = HashMap()
- paramsHash!!["amount"] = amount
- paramsHash!!["nonce"] = stringNonce
- sendPayment()
- }
- else
- Toast.makeText(this@MainActivity,"Please enter valid amount",Toast.LENGTH_LONG).show()
- }
- else if(resultCode == Activity.RESULT_CANCELED)
- Toast.makeText(this@MainActivity,"User Cancel",Toast.LENGTH_LONG).show()
- else {
- val error:Exception = data!!.getSerializableExtra(DropInActivity.EXTRA_ERROR) as Exception
- Log.d("ERROR", error.message)
- }
- }
- private fun sendPayment(){
- val queue:RequestQueue = Volley.newRequestQueue(this@MainActivity)
- val stringRequest = object:StringRequest(Request.Method.POST, API_CHECKOUT,
- Response.Listener { response ->
- if(response.toString().contains("Successful"))
- Toast.makeText(this@MainActivity, "Transaction Successful",Toast.LENGTH_SHORT).show()
- else
- Toast.makeText(this@MainActivity, "Transaction Failed",Toast.LENGTH_SHORT).show()
- }, Response.ErrorListener { error ->
- Log.d("ERROR","Volley Error"+error.message)
- }){
- override fun getParams(): MutableMap<String, String?>? {
- if(paramsHash == null)
- return null
- var params = HashMap<String,String?>()
- for(key:String in paramsHash!!.keys)
- params.set(key,paramsHash!![key])
- return params
- }
- override fun getHeaders(): MutableMap<String, String> {
- val params = HashMap<String,String>()
- params["Content-Type"] = "application/x-www-form-urlencoded"
- return params
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement