Advertisement
Guest User

BraintreeKotlin in androidstudio

a guest
Oct 24th, 2019
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.37 KB | None | 0 0
  1. AndroidManifest.xml
  2. ----------------------
  3.  
  4. <?xml version="1.0" encoding="utf-8"?>
  5. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  6. package="com.example.braintreekotlinsample">
  7.  
  8. <uses-permission android:name="android.permission.INTERNET"/>
  9. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
  10. <application
  11. android:allowBackup="true"
  12. android:icon="@mipmap/ic_launcher"
  13. android:label="@string/app_name"
  14. android:roundIcon="@mipmap/ic_launcher_round"
  15. android:supportsRtl="true"
  16. android:theme="@style/AppTheme">
  17. <activity android:name=".MainActivity">
  18. <intent-filter>
  19. <action android:name="android.intent.action.MAIN"/>
  20.  
  21. <category android:name="android.intent.category.LAUNCHER"/>
  22. </intent-filter>
  23. </activity>
  24.  
  25. <activity android:name="com.braintreepayments.api.BraintreeBrowserSwitchActivity"
  26. android:launchMode="singleTask">
  27. <intent-filter>
  28. <action android:name="android.intent.action.VIEW" />
  29. <category android:name="android.intent.category.DEFAULT" />
  30. <category android:name="android.intent.category.BROWSABLE" />
  31. <data android:scheme="${applicationId}.braintree" />
  32. </intent-filter>
  33. </activity>
  34. </application>
  35.  
  36. </manifest>
  37.  
  38. ---------------------------------------
  39. activity_main.xml
  40. ---------------------------------------
  41. <?xml version="1.0" encoding="utf-8"?>
  42. <RelativeLayout
  43. xmlns:android="http://schemas.android.com/apk/res/android"
  44. xmlns:tools="http://schemas.android.com/tools"
  45. xmlns:app="http://schemas.android.com/apk/res-auto"
  46. android:layout_width="match_parent"
  47. android:layout_height="match_parent"
  48. tools:context=".MainActivity">
  49.  
  50. <LinearLayout
  51. android:id="@+id/waiting_group"
  52. android:layout_centerInParent="true"
  53. android:orientation="vertical"
  54. android:visibility="visible"
  55. android:layout_width="match_parent"
  56. android:layout_height="wrap_content">
  57.  
  58. <TextView
  59. android:layout_width="wrap_content"
  60. android:layout_height="wrap_content"
  61. android:text="Please wait for the token to get"
  62. android:layout_gravity="center_horizontal"/>
  63.  
  64.  
  65. </LinearLayout>
  66.  
  67.  
  68. <LinearLayout
  69. android:id="@+id/payment_group"
  70. android:layout_centerInParent="true"
  71. android:orientation="vertical"
  72. android:visibility="gone"
  73. android:layout_width="match_parent"
  74. android:layout_height="wrap_content">
  75.  
  76. <TextView
  77. android:layout_width="wrap_content"
  78. android:layout_height="wrap_content"
  79. android:text="CHECKOUT"
  80. android:layout_gravity="center_horizontal"
  81. android:textStyle="bold"
  82. android:textSize="20sp"/>
  83. <android.support.design.widget.TextInputLayout android:layout_width="match_parent"
  84. android:layout_height="wrap_content">
  85.  
  86. <EditText
  87. android:id="@+id/edt_payment"
  88. android:hint="Choose the amount to pay(USD)"
  89. android:gravity="center_horizontal"
  90. android:inputType="number"
  91. android:layout_width="match_parent"
  92. android:layout_height="wrap_content"/>
  93.  
  94. </android.support.design.widget.TextInputLayout>
  95.  
  96. <Button
  97. android:id="@+id/btn_pay"
  98. android:text="PAY"
  99. android:layout_gravity="center_horizontal"
  100. android:layout_width="wrap_content"
  101. android:layout_height="wrap_content"/>
  102.  
  103.  
  104. </LinearLayout>
  105.  
  106.  
  107. </RelativeLayout>
  108.  
  109.  
  110. -----------------------------------------
  111. MainActivity.kt
  112. -----------------------------------------
  113.  
  114. package com.example.braintreekotlinsample
  115. import android.app.Activity
  116. import android.app.VoiceInteractor
  117. import android.content.Intent
  118. import android.os.AsyncTask
  119. import android.support.v7.app.AppCompatActivity
  120. import android.os.Bundle
  121. import android.os.Parcel
  122. import android.os.Parcelable
  123. import android.util.Log
  124. import android.view.View
  125. import android.widget.Toast
  126. import com.android.volley.Request
  127. import com.android.volley.RequestQueue
  128. import com.android.volley.Response
  129. import com.android.volley.toolbox.StringRequest
  130. import com.android.volley.toolbox.Volley
  131. import com.braintreepayments.api.dropin.DropInActivity
  132. import com.braintreepayments.api.dropin.DropInRequest
  133. import com.braintreepayments.api.dropin.DropInResult
  134. import com.braintreepayments.api.models.PaymentMethodNonce
  135. import com.loopj.android.http.AsyncHttpClient
  136. import com.loopj.android.http.TextHttpResponseHandler
  137. import cz.msebera.android.httpclient.Header
  138. import kotlinx.android.synthetic.main.activity_main.*
  139. import java.io.File
  140. import kotlin.Exception
  141.  
  142. class MainActivity : AppCompatActivity() {
  143.  
  144. companion object{
  145. val API_GET_TOKEN = "TOKEN LINK HERE"
  146. val API_CHECKOUT = "CHECKOUT LINK"
  147. val REQUEST_CODE: Int = 111
  148.  
  149. internal lateinit var token:String
  150. internal lateinit var amount:String
  151. internal var paramsHash:HashMap<String,String>?=null
  152.  
  153. }
  154.  
  155. override fun onCreate(savedInstanceState: Bundle?) {
  156. super.onCreate(savedInstanceState)
  157. setContentView(R.layout.activity_main)
  158.  
  159. getToken()
  160.  
  161.  
  162. //Event
  163.  
  164. btn_pay.setOnClickListener {
  165. val dropInRequest = DropInRequest().clientToken(token)
  166. startActivityForResult(dropInRequest.getIntent(this),REQUEST_CODE)
  167. }
  168. }
  169.  
  170. private fun getToken(){
  171. val androidClient = AsyncHttpClient()
  172. androidClient.get(API_GET_TOKEN, object:TextHttpResponseHandler(){
  173. override fun onSuccess(statusCode: Int, headers: Array<out Header>?, responseString: String?) {
  174. runOnUiThread {
  175. payment_group.visibility = View.VISIBLE
  176. waiting_group.visibility = View.GONE
  177. token = responseString!!.toString()
  178. }
  179. }
  180.  
  181. override fun onFailure(
  182. statusCode: Int,
  183. headers: Array<out Header>?,
  184. responseString: String?,
  185. throwable: Throwable?
  186. ) {
  187. runOnUiThread {
  188. Toast.makeText(this@MainActivity,"Failed to load token:"+throwable.toString(),Toast.LENGTH_SHORT).show()
  189.  
  190. }
  191. }
  192.  
  193. })
  194. }
  195.  
  196. override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
  197. if(requestCode == REQUEST_CODE){
  198.  
  199. }
  200. if(requestCode == Activity.RESULT_OK){
  201. val result:DropInResult = data!!.getParcelableExtra<DropInResult>(DropInResult.EXTRA_DROP_IN_RESULT)
  202. val nonce:PaymentMethodNonce? = result.paymentMethodNonce
  203. val stringNonce:String = nonce!!.nonce
  204.  
  205. if(!edt_payment.text.toString().isEmpty()){
  206. amount = edt_payment.text.toString()
  207. paramsHash = HashMap()
  208.  
  209. paramsHash!!["amount"] = amount
  210. paramsHash!!["nonce"] = stringNonce
  211.  
  212. sendPayment()
  213. }
  214. else
  215. Toast.makeText(this@MainActivity,"Please enter valid amount",Toast.LENGTH_LONG).show()
  216.  
  217. }
  218.  
  219. else if(resultCode == Activity.RESULT_CANCELED)
  220. Toast.makeText(this@MainActivity,"User Cancel",Toast.LENGTH_LONG).show()
  221.  
  222. else {
  223. val error:Exception = data!!.getSerializableExtra(DropInActivity.EXTRA_ERROR) as Exception
  224. Log.d("ERROR", error.message)
  225.  
  226. }
  227.  
  228.  
  229. }
  230.  
  231. private fun sendPayment(){
  232. val queue:RequestQueue = Volley.newRequestQueue(this@MainActivity)
  233.  
  234. val stringRequest = object:StringRequest(Request.Method.POST, API_CHECKOUT,
  235. Response.Listener { response ->
  236. if(response.toString().contains("Successful"))
  237. Toast.makeText(this@MainActivity, "Transaction Successful",Toast.LENGTH_SHORT).show()
  238. else
  239. Toast.makeText(this@MainActivity, "Transaction Failed",Toast.LENGTH_SHORT).show()
  240. }, Response.ErrorListener { error ->
  241. Log.d("ERROR","Volley Error"+error.message)
  242.  
  243. }){
  244. override fun getParams(): MutableMap<String, String?>? {
  245. if(paramsHash == null)
  246. return null
  247. var params = HashMap<String,String?>()
  248. for(key:String in paramsHash!!.keys)
  249. params.set(key,paramsHash!![key])
  250. return params
  251.  
  252.  
  253.  
  254. }
  255.  
  256. override fun getHeaders(): MutableMap<String, String> {
  257. val params = HashMap<String,String>()
  258. params["Content-Type"] = "application/x-www-form-urlencoded"
  259. return params
  260. }
  261. }
  262. }
  263.  
  264.  
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement