Skip to content

Commit

Permalink
Add PaymentService implementation and related support classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric M. Lee committed Jul 27, 2017
1 parent c714c98 commit 19b5c96
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,34 @@ package com.deliveryhero.android.braintreesample

import android.app.Application
import android.widget.TextView
import com.deliveryhero.android.braintreesample.service.ApiClientRequestInterceptor
import com.deliveryhero.android.braintreesample.service.PaymentService
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import timber.log.Timber

class BraintreeSampleApplication : Application() {

companion object {

private val okhttp by lazy {
OkHttpClient.Builder()
.addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
.addInterceptor(ApiClientRequestInterceptor)
.build()
}
private val retrofit by lazy {
Retrofit.Builder().baseUrl("https://braintree-sample-merchant.herokuapp.com")
.client(okhttp)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build()
}
val paymentService by lazy { retrofit.create(PaymentService::class.java) }

fun initializeTrees(outputTo: TextView) {
Timber.plant(Timber.DebugTree())
Timber.plant(OutputTree(outputTo))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.deliveryhero.android.braintreesample.feature.payment

import com.deliveryhero.android.braintreesample.model.ClientToken
import io.reactivex.Single

interface RetrieveToken {

operator fun invoke(): Single<ClientToken>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.deliveryhero.android.braintreesample.feature.payment

import com.deliveryhero.android.braintreesample.BraintreeSampleApplication.Companion.paymentService

class RetrieveTokenImpl : RetrieveToken {

override operator fun invoke() = paymentService.getClientToken(null, null)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.deliveryhero.android.braintreesample.model

import com.google.gson.annotations.SerializedName

data class ClientToken(@SerializedName("client_token") val value: String = "")
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.deliveryhero.android.braintreesample.service

import com.deliveryhero.android.braintreesample.model.ClientToken
import io.reactivex.Single
import okhttp3.Interceptor
import okhttp3.Response
import retrofit2.http.GET
import retrofit2.http.Query

interface PaymentService {

@GET("/client_token")
fun getClientToken(@Query("customer_id") customerId: String?,
@Query("merchant_account_id") merchantAccountId: String?)
: Single<ClientToken>
}

object ApiClientRequestInterceptor : Interceptor {

override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request()
.newBuilder()
.addHeader("User-Agent", "braintree/android-demo-app/2.5.5-SNAPSHOT")
.addHeader("Accept", "application/json")
.build()
return chain.proceed(request)
}
}

0 comments on commit 19b5c96

Please sign in to comment.