Skip to content

Commit

Permalink
Make Klarna creation methods public and create example
Browse files Browse the repository at this point in the history
- Make `KlarnaSourceParams` public
- Make `SourceParams.createKlarna()` public
- Create `KlarnaSourceActivity`
  • Loading branch information
mshafrir-stripe committed Nov 25, 2019
1 parent 2a7a3ae commit c655090
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 5 deletions.
3 changes: 3 additions & 0 deletions example/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@

<activity
android:name=".activity.FpxPaymentActivity" />

<activity
android:name=".activity.KlarnaSourceActivity" />
</application>

</manifest>
3 changes: 2 additions & 1 deletion example/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ dependencies {
implementation 'androidx.multidex:multidex:2.0.1'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation "androidx.lifecycle:lifecycle-viewmodel:2.1.0"
implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0'

implementation 'com.google.android.gms:play-services-wallet:18.0.0'

Expand Down
36 changes: 36 additions & 0 deletions example/res/layout/activity_klarna_source.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>

<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<ProgressBar
android:id="@+id/progress_bar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="true"
android:visibility="invisible" />

<Button
android:id="@+id/btn_create_klarna_source"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="@dimen/example_vertical_spacing"
android:text="@string/create_klarna_source"
/>

<TextView
android:id="@+id/source_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:typeface="monospace" />
</LinearLayout>
</ScrollView>
3 changes: 3 additions & 0 deletions example/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<string name="create_rxjava">Create Token with RxJava</string>
<string name="launch_create_pm_sepa_debit">Create SEPA Debit Payment Method</string>
<string name="fpx_payment_example">FPX Payment Example</string>
<string name="klarna_source_example">Klarna Source Example</string>

<string name="select_add_card">Select or add card</string>
<string name="selected_card">Selected card:</string>
Expand Down Expand Up @@ -68,4 +69,6 @@

<string name="ready_to_charge">Ready to charge?</string>
<string name="not_selected">Not selected</string>

<string name="create_klarna_source">Create Klarna Source</string>
</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.stripe.example.activity

import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.stripe.android.model.KlarnaSourceParams
import com.stripe.android.model.SourceParams
import com.stripe.example.R
import kotlinx.android.synthetic.main.activity_klarna_source.*

class KlarnaSourceActivity : AppCompatActivity() {
private lateinit var viewModel: SourceViewModel

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

setContentView(R.layout.activity_klarna_source)

viewModel = ViewModelProviders.of(this)[SourceViewModel::class.java]

viewModel.createdSource.observe(this, Observer {
progress_bar.visibility = View.INVISIBLE
source_result.text = it.toString()
})

btn_create_klarna_source.setOnClickListener {
progress_bar.visibility = View.VISIBLE
createKlarnaSource()
}
}

private fun createKlarnaSource() {
viewModel.createSource(SourceParams.createKlarna(
returnUrl = RETURN_URL,
currency = "eur",
klarnaParams = KlarnaSourceParams(
purchaseCountry = "DE",
lineItems = LINE_ITEMS
)
))
}

private companion object {
private const val RETURN_URL = "https://example.com"

private val LINE_ITEMS = listOf(
KlarnaSourceParams.LineItem(
itemType = KlarnaSourceParams.LineItem.Type.Sku,
itemDescription = "towel",
totalAmount = 10000,
quantity = 1
),
KlarnaSourceParams.LineItem(
itemType = KlarnaSourceParams.LineItem.Type.Sku,
itemDescription = "digital watch",
totalAmount = 20000,
quantity = 2
),
KlarnaSourceParams.LineItem(
itemType = KlarnaSourceParams.LineItem.Type.Tax,
itemDescription = "taxes",
totalAmount = 1500
),
KlarnaSourceParams.LineItem(
itemType = KlarnaSourceParams.LineItem.Type.Shipping,
itemDescription = "ground shipping",
totalAmount = 499
)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ class LauncherActivity : AppCompatActivity() {
Item(activity.getString(R.string.launch_create_pm_sepa_debit),
CreateSepaDebitActivity::class.java),
Item(activity.getString(R.string.fpx_payment_example),
FpxPaymentActivity::class.java)
FpxPaymentActivity::class.java),
Item(activity.getString(R.string.klarna_source_example),
KlarnaSourceActivity::class.java)
)

override fun onCreateViewHolder(viewGroup: ViewGroup, i: Int): ExamplesViewHolder {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.stripe.example.activity

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import com.stripe.android.ApiResultCallback
import com.stripe.android.Stripe
import com.stripe.android.model.Source
import com.stripe.android.model.SourceParams
import com.stripe.example.Settings

internal class SourceViewModel(
application: Application
) : AndroidViewModel(application) {
private val stripe = Stripe(application.applicationContext, Settings.PUBLISHABLE_KEY)

@JvmSynthetic
internal val createdSource: MutableLiveData<Source> = MutableLiveData()

@JvmSynthetic
internal val createdSourceException: MutableLiveData<Exception> = MutableLiveData()

internal fun createSource(sourceParams: SourceParams) {
stripe.createSource(sourceParams, object : ApiResultCallback<Source> {
override fun onSuccess(result: Source) {
createdSource.value = result
}

override fun onError(e: Exception) {
createdSourceException.value = e
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ package com.stripe.android.model
* Be careful with this option: If the provided information is invalid,
* Klarna may reject the transaction without giving the customer a chance to correct it.
*/
internal data class KlarnaSourceParams @JvmOverloads constructor(
data class KlarnaSourceParams @JvmOverloads constructor(
/**
* The URL the customer should be redirected to after they have successfully verified the
* payment.
Expand Down
4 changes: 2 additions & 2 deletions stripe/src/main/java/com/stripe/android/model/SourceParams.kt
Original file line number Diff line number Diff line change
Expand Up @@ -509,8 +509,8 @@ class SourceParams private constructor(
* This is the currency for which the source will be chargeable once ready.
* @param klarnaParams Klarna-specific params
*/
@JvmSynthetic
internal fun createKlarna(
@JvmStatic
fun createKlarna(
returnUrl: String,
currency: String,
klarnaParams: KlarnaSourceParams
Expand Down

0 comments on commit c655090

Please sign in to comment.