-
Notifications
You must be signed in to change notification settings - Fork 659
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add klarna to sdk w/ example #4325
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?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" | ||
android:paddingStart="10dp" | ||
android:paddingEnd="10dp" | ||
android:paddingBottom="20dp"> | ||
|
||
<com.google.android.material.progressindicator.LinearProgressIndicator | ||
android:id="@+id/progress_bar" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:indeterminate="true" | ||
android:visibility="invisible" | ||
style="@style/Widget.MaterialComponents.LinearProgressIndicator" /> | ||
|
||
<TextView | ||
android:layout_marginTop="10dp" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:text="@string/klarna_example_intro" /> | ||
|
||
<Button | ||
android:id="@+id/confirm_with_klarna_button" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:layout_marginBottom="4dp" | ||
android:text="@string/confirm_klarna_button" /> | ||
|
||
<TextView | ||
android:id="@+id/status" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:typeface="monospace" | ||
android:layout_marginTop="40dp"/> | ||
</LinearLayout> | ||
</ScrollView> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.stripe.example.activity | ||
|
||
import android.os.Bundle | ||
import android.view.View | ||
import androidx.lifecycle.Observer | ||
import com.stripe.android.model.Address | ||
import com.stripe.android.model.PaymentMethod | ||
import com.stripe.android.model.PaymentMethodCreateParams | ||
import com.stripe.example.databinding.KlarnaPaymentActivityBinding | ||
|
||
class KlarnaPaymentActivity : StripeIntentActivity() { | ||
|
||
private val viewBinding: KlarnaPaymentActivityBinding by lazy { | ||
KlarnaPaymentActivityBinding.inflate(layoutInflater) | ||
} | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(viewBinding.root) | ||
|
||
viewModel.inProgress.observe(this, { enableUi(!it) }) | ||
viewModel.status.observe(this, Observer(viewBinding.status::setText)) | ||
|
||
viewBinding.confirmWithKlarnaButton.setOnClickListener { | ||
createAndConfirmPaymentIntent("US", confirmParams, "klarna") | ||
} | ||
} | ||
|
||
private fun enableUi(enable: Boolean) { | ||
viewBinding.progressBar.visibility = if (enable) View.INVISIBLE else View.VISIBLE | ||
viewBinding.confirmWithKlarnaButton.isEnabled = enable | ||
} | ||
|
||
private companion object { | ||
private val confirmParams = PaymentMethodCreateParams.createKlarna( | ||
billingDetails = PaymentMethod.BillingDetails( | ||
email = "[email protected]", | ||
address = Address.Builder() | ||
.setCountry("US") | ||
.build() | ||
) | ||
) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import android.content.Context | |
import androidx.test.core.app.ApplicationProvider | ||
skyler-stripe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import com.google.common.truth.Truth.assertThat | ||
import com.stripe.android.exception.InvalidRequestException | ||
import com.stripe.android.model.Address | ||
import com.stripe.android.model.PaymentMethod | ||
import com.stripe.android.model.PaymentMethodCreateParams | ||
import com.stripe.android.model.PaymentMethodCreateParamsFixtures | ||
|
@@ -338,4 +339,47 @@ internal class PaymentMethodEndToEndTest { | |
assertThat(paymentMethod?.type) | ||
.isEqualTo(PaymentMethod.Type.WeChatPay) | ||
} | ||
|
||
@Test | ||
fun createPaymentMethod_withKlarna_shouldCreateObject() { | ||
val missingAddressException = assertFailsWith<InvalidRequestException>( | ||
"Address is required to create a klarna payment method" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. where are these messages from? is it possible to build them programmatically? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These messages come from the Stripe api's error messages, we parse the response. So there's not really any way to generate them programatically. I was following the after pay example here. I don't think there's a much cleaner way. |
||
) { | ||
val params = PaymentMethodCreateParams.createKlarna( | ||
billingDetails = | ||
PaymentMethodCreateParamsFixtures.BILLING_DETAILS.copy(address = null) | ||
) | ||
Stripe(context, ApiKeyFixtures.KLARNA_PUBLISHABLE_KEY) | ||
.createPaymentMethodSynchronous(params) | ||
} | ||
|
||
assertThat(missingAddressException.message) | ||
.isEqualTo("You must provide `billing_details[address][country]` to use Klarna.") | ||
|
||
val missingCountryException = assertFailsWith<InvalidRequestException>( | ||
"Country is required to create a klarna payment method" | ||
) { | ||
val address = Address(country = null) | ||
val params = PaymentMethodCreateParams.createKlarna( | ||
billingDetails = | ||
PaymentMethodCreateParamsFixtures.BILLING_DETAILS.copy(address = address) | ||
) | ||
|
||
Stripe(context, ApiKeyFixtures.KLARNA_PUBLISHABLE_KEY) | ||
.createPaymentMethodSynchronous(params) | ||
} | ||
|
||
assertThat(missingCountryException.message) | ||
.isEqualTo("You must provide `billing_details[address][country]` to use Klarna.") | ||
|
||
val params = PaymentMethodCreateParams.createKlarna( | ||
billingDetails = PaymentMethodCreateParamsFixtures.BILLING_DETAILS.copy() | ||
) | ||
|
||
val paymentMethod = | ||
Stripe(context, ApiKeyFixtures.KLARNA_PUBLISHABLE_KEY) | ||
.createPaymentMethodSynchronous(params) | ||
|
||
assertThat(paymentMethod?.type).isEqualTo(PaymentMethod.Type.Klarna) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder should we exhaust this to customize all possible params provided by the toy server(not needed in this PR)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True. It might be good to just have them all there in case we need them in the future. Unfortunately, it's not super well documented. That's why I only added this one for now.