-
Notifications
You must be signed in to change notification settings - Fork 659
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make Klarna creation methods public and create example
- Make `KlarnaSourceParams` public - Make `SourceParams.createKlarna()` public - Create `KlarnaSourceActivity`
- Loading branch information
1 parent
a7075d8
commit 08b28d3
Showing
10 changed files
with
159 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
example/src/main/java/com/stripe/example/activity/KlarnaSourceActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
example/src/main/java/com/stripe/example/activity/SourceViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters