Skip to content

Commit

Permalink
Add web example
Browse files Browse the repository at this point in the history
  • Loading branch information
brnunes-stripe committed Sep 16, 2022
1 parent 5da0b57 commit 2386350
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 16 deletions.
3 changes: 2 additions & 1 deletion example/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@
<activity android:name=".activity.ComposeExampleActivity" />
<activity android:name=".activity.KlarnaPaymentActivity"/>
<activity android:name=".activity.AffirmPaymentActivity"/>
<activity android:name=".activity.AlipayPaymentActivity"/>
<activity android:name=".activity.AlipayPaymentNativeActivity"/>
<activity android:name=".activity.AlipayPaymentWebActivity"/>
<activity android:name=".activity.ConnectUSBankAccountActivity"/>
<activity android:name=".activity.ManualUSBankAccountPaymentMethodActivity"/>
</application>
Expand Down
3 changes: 2 additions & 1 deletion example/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@

<string name="alipay_example_intro">Tapping the button below will create a PaymentIntent and then use Alipay to confirm it</string>
<string name="confirm_alipay_button">Confirm with Alipay</string>
<string name="confirm_with_alipay">Alipay Payment Intent Example</string>
<string name="confirm_with_alipay_native">Alipay Payment Intent Native Example</string>
<string name="confirm_with_alipay_web">Alipay Payment Intent Web Example</string>

<!-- SEPA Debit -->
<string name="sepa_debit_mandate">By providing your IBAN and confirming this payment, you are authorizing EXAMPLE COMPANY NAME and Stripe, our payment service provider, to send instructions to your bank to debit your account and your bank to debit your account in accordance with those instructions. You are entitled to a refund from your bank under the terms and conditions of your agreement with your bank. A refund must be claimed within 8 weeks starting from the date on which your account was debited.</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import com.stripe.example.R
import com.stripe.example.databinding.PaymentExampleActivityBinding
import org.json.JSONObject

class AlipayPaymentActivity : StripeIntentActivity() {
class AlipayPaymentNativeActivity : StripeIntentActivity() {

private val viewBinding: PaymentExampleActivityBinding by lazy {
PaymentExampleActivityBinding.inflate(layoutInflater)
Expand All @@ -29,6 +29,9 @@ class AlipayPaymentActivity : StripeIntentActivity() {
)
}

private var clientSecret: String? = null
private var confirmed = false

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(viewBinding.root)
Expand All @@ -42,11 +45,19 @@ class AlipayPaymentActivity : StripeIntentActivity() {
viewModel.status.observe(this, Observer(viewBinding.status::setText))

viewBinding.confirmWithPaymentButton.setOnClickListener {
createAndConfirmPaymentIntent(
country = "US",
paymentMethodCreateParams = PaymentMethodCreateParams.createAlipay(),
supportedPaymentMethods = "alipay"
)
clientSecret?.let {
// If we already loaded the Payment Intent and haven't confirmed, try again
if (!confirmed) {
updateStatus("\n\nPayment Intent already created, trying to confirm")
confirmPayment(it)
}
} ?: run {
createAndConfirmPaymentIntent(
country = "US",
paymentMethodCreateParams = PaymentMethodCreateParams.createAlipay(),
supportedPaymentMethods = "alipay"
)
}
}
}

Expand All @@ -59,8 +70,6 @@ class AlipayPaymentActivity : StripeIntentActivity() {
mandateDataParams: MandateDataParams?,
onPaymentIntentCreated: (String) -> Unit
) {
val secret = responseData.getString("secret")
onPaymentIntentCreated(secret)
viewModel.status.value +=
"\n\nStarting PaymentIntent confirmation" +
(
Expand All @@ -69,20 +78,32 @@ class AlipayPaymentActivity : StripeIntentActivity() {
} ?: ""
)

clientSecret = responseData.getString("secret").also {
confirmPayment(it)
}
}

private fun confirmPayment(clientSecret: String) {
stripe.confirmAlipayPayment(
confirmPaymentIntentParams = ConfirmPaymentIntentParams.createAlipay(secret),
confirmPaymentIntentParams = ConfirmPaymentIntentParams.createAlipay(clientSecret),
authenticator = { data ->
PayTask(this).payV2(data, true)
},
callback = object : ApiResultCallback<PaymentIntentResult> {
override fun onSuccess(result: PaymentIntentResult) {
val paymentIntent = result.intent
when (paymentIntent.status) {
StripeIntent.Status.Succeeded ->
StripeIntent.Status.Succeeded -> {
confirmed = true
updateStatus("\n\nPayment succeeded")
}
StripeIntent.Status.RequiresAction ->
stripe.handleNextActionForPayment(this@AlipayPaymentActivity, secret)
else -> updateStatus("\n\nPayment failed or canceled")
updateStatus("\n\nUser canceled confirmation")
else ->
updateStatus(
"\n\nPayment failed or canceled." +
"\nStatus: ${paymentIntent.status}"
)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package com.stripe.example.activity

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.lifecycle.Observer
import com.stripe.android.ApiResultCallback
import com.stripe.android.PaymentConfiguration
import com.stripe.android.PaymentIntentResult
import com.stripe.android.Stripe
import com.stripe.android.model.ConfirmPaymentIntentParams
import com.stripe.android.model.MandateDataParams
import com.stripe.android.model.PaymentMethodCreateParams
import com.stripe.android.model.StripeIntent
import com.stripe.example.R
import com.stripe.example.databinding.PaymentExampleActivityBinding
import org.json.JSONObject

class AlipayPaymentWebActivity : StripeIntentActivity() {

private val viewBinding: PaymentExampleActivityBinding by lazy {
PaymentExampleActivityBinding.inflate(layoutInflater)
}

private val stripe: Stripe by lazy {
Stripe(
applicationContext,
PaymentConfiguration.getInstance(applicationContext).publishableKey
)
}

private var clientSecret: String? = null
private var confirmed = false

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

viewBinding.confirmWithPaymentButton.text =
resources.getString(R.string.confirm_alipay_button)
viewBinding.paymentExampleIntro.text =
resources.getString(R.string.alipay_example_intro)

viewModel.inProgress.observe(this) { enableUi(!it) }
viewModel.status.observe(this, Observer(viewBinding.status::setText))

viewBinding.confirmWithPaymentButton.setOnClickListener {
clientSecret?.let {
// If we already loaded the Payment Intent and haven't confirmed, try again
if (!confirmed) {
updateStatus("\n\nPayment Intent already created, trying to confirm")
confirmPayment(it)
}
} ?: run {
createAndConfirmPaymentIntent(
country = "US",
paymentMethodCreateParams = PaymentMethodCreateParams.createAlipay(),
supportedPaymentMethods = "alipay"
)
}
}
}

override fun handleCreatePaymentIntentResponse(
responseData: JSONObject,
params: PaymentMethodCreateParams?,
shippingDetails: ConfirmPaymentIntentParams.Shipping?,
stripeAccountId: String?,
existingPaymentMethodId: String?,
mandateDataParams: MandateDataParams?,
onPaymentIntentCreated: (String) -> Unit
) {
viewModel.status.value +=
"\n\nStarting PaymentIntent confirmation" +
(
stripeAccountId?.let {
" for $it"
} ?: ""
)

clientSecret = responseData.getString("secret").also {
confirmPayment(it)
}
}

private fun confirmPayment(clientSecret: String) {
stripe.confirmPayment(this, ConfirmPaymentIntentParams.createAlipay(clientSecret))
}

private fun updateStatus(appendMessage: String) {
viewModel.status.value += appendMessage
viewModel.inProgress.postValue(false)
}

private fun enableUi(enable: Boolean) {
viewBinding.progressBar.visibility = if (enable) View.INVISIBLE else View.VISIBLE
viewBinding.confirmWithPaymentButton.isEnabled = enable
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)

// Handle the result of stripe.confirmPayment
stripe.onPaymentResult(
requestCode,
data,
object : ApiResultCallback<PaymentIntentResult> {
override fun onSuccess(result: PaymentIntentResult) {
val paymentIntent = result.intent
val status = paymentIntent.status
when (status) {
StripeIntent.Status.Succeeded ->
updateStatus("\n\nPayment succeeded")
StripeIntent.Status.RequiresAction ->
updateStatus("\n\nUser canceled confirmation")
else ->
updateStatus(
"\n\nPayment failed or canceled." +
"\nStatus: ${paymentIntent.status}"
)
}
}

override fun onError(e: Exception) {
updateStatus("\n\nError: ${e.message}")
}
}
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,12 @@ class LauncherActivity : AppCompatActivity() {
AffirmPaymentActivity::class.java
),
Item(
activity.getString(R.string.confirm_with_alipay),
AlipayPaymentActivity::class.java
activity.getString(R.string.confirm_with_alipay_native),
AlipayPaymentNativeActivity::class.java
),
Item(
activity.getString(R.string.confirm_with_alipay_web),
AlipayPaymentWebActivity::class.java
),
Item(
activity.getString(R.string.becs_debit_example),
Expand Down

0 comments on commit 2386350

Please sign in to comment.