Skip to content

Commit

Permalink
[MBL-1163] Add mutation and models for createPaymentIntent (#1942)
Browse files Browse the repository at this point in the history
* add mutation and models for createPaymentIntent

* lint
  • Loading branch information
mtgriego committed Feb 5, 2024
1 parent 629a735 commit b9634bf
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
6 changes: 6 additions & 0 deletions app/src/main/graphql/checkout.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,9 @@ mutation CreateCheckout($projectId: ID!, $amount: String!, $rewardIds: [ID!], $l
}
}
}

mutation CreatePaymentIntent($projectId: ID!, $amountDollars: String!) {
createPaymentIntent(input: { projectId: $projectId, amountDollars: $amountDollars } ) {
clientSecret
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import com.kickstarter.models.Category
import com.kickstarter.models.Checkout
import com.kickstarter.models.CheckoutPayment
import com.kickstarter.models.Comment
import com.kickstarter.models.CreatePaymentIntentInput
import com.kickstarter.models.CreatorDetails
import com.kickstarter.models.ErroredBacking
import com.kickstarter.models.Location
Expand Down Expand Up @@ -270,6 +271,10 @@ open class MockApolloClientV2 : ApolloClientTypeV2 {
override fun createCheckout(createCheckoutData: CreateCheckoutData): io.reactivex.Observable<CheckoutPayment> {
return io.reactivex.Observable.empty()
}

override fun createPaymentIntent(createPaymentIntentInput: CreatePaymentIntentInput): io.reactivex.Observable<String> {
return io.reactivex.Observable.empty()
}
}

open class MockApolloClient : ApolloClientType {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.kickstarter.models

data class CreatePaymentIntentInput(val project: Project, val amountDollars: String)
31 changes: 31 additions & 0 deletions app/src/main/java/com/kickstarter/services/KSApolloClientV2.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.kickstarter.services

import CreatePaymentIntentMutation
import android.util.Pair
import com.apollographql.apollo.ApolloCall
import com.apollographql.apollo.ApolloClient
Expand All @@ -11,6 +12,7 @@ import com.kickstarter.models.Category
import com.kickstarter.models.Checkout
import com.kickstarter.models.CheckoutPayment
import com.kickstarter.models.Comment
import com.kickstarter.models.CreatePaymentIntentInput
import com.kickstarter.models.CreatorDetails
import com.kickstarter.models.ErroredBacking
import com.kickstarter.models.Location
Expand Down Expand Up @@ -128,6 +130,8 @@ interface ApolloClientTypeV2 {
fun getProjectBacking(slug: String): Observable<Backing>

fun createCheckout(createCheckoutData: CreateCheckoutData): Observable<CheckoutPayment>

fun createPaymentIntent(createPaymentIntentInput: CreatePaymentIntentInput): Observable<String>
}

private const val PAGE_SIZE = 25
Expand Down Expand Up @@ -1433,4 +1437,31 @@ class KSApolloClientV2(val service: ApolloClient) : ApolloClientTypeV2 {
return@defer ps
}.subscribeOn(Schedulers.io())
}

override fun createPaymentIntent(createPaymentIntentInput: CreatePaymentIntentInput): Observable<String> {
return Observable.defer {
val ps = PublishSubject.create<String>()

this.service.mutate(
CreatePaymentIntentMutation.builder()
.projectId(encodeRelayId(createPaymentIntentInput.project))
.amountDollars(createPaymentIntentInput.amountDollars)
.build()
).enqueue(object : ApolloCall.Callback<CreatePaymentIntentMutation.Data>() {
override fun onFailure(e: ApolloException) {
ps.onError(e)
}

override fun onResponse(response: Response<CreatePaymentIntentMutation.Data>) {
if (response.hasErrors()) {
ps.onError(Exception(response.errors?.first()?.message))
} else {
ps.onNext(response.data?.createPaymentIntent()?.clientSecret() ?: "")
}
ps.onComplete()
}
})
return@defer ps
}
}
}

0 comments on commit b9634bf

Please sign in to comment.