Skip to content

Commit

Permalink
checkout: Add possibility for additional data to `PaymentFlowActionTr…
Browse files Browse the repository at this point in the history
…iggerClientSDK` (#349)
  • Loading branch information
tessig authored Nov 22, 2021
1 parent 386261c commit 100608f
Show file tree
Hide file tree
Showing 12 changed files with 261 additions and 228 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
| SumGrandTotalWithGiftCards() | GrandTotalWithGiftCards |
| - | GrandTotalNetWithGiftCards |

**checkout**
* Add possibility to have additional data in `PaymentFlowActionTriggerClientSDK`

## v3.5.0
**general**
Expand Down
2 changes: 1 addition & 1 deletion checkout/application/placeorder/validate_payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func PaymentValidator(ctx context.Context, p *process.Process, paymentService *a
Failed: process.PaymentErrorOccurredReason{Error: ValidatePaymentErrorNoActionURL},
}
}
p.UpdateState(states.TriggerClientSDK{}.Name(), states.NewTriggerClientSDKStateData(flowStatus.ActionData.URL))
p.UpdateState(states.TriggerClientSDK{}.Name(), states.NewTriggerClientSDKStateData(flowStatus.ActionData.URL, flowStatus.ActionData.DisplayData))
default:
return process.RunResult{
Failed: process.PaymentErrorOccurredReason{Error: fmt.Sprintf("Payment action not supported: %q", flowStatus.Action)},
Expand Down
8 changes: 6 additions & 2 deletions checkout/application/placeorder/validate_payment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,18 @@ func TestPaymentValidator(t *testing.T) {
Status: domain.PaymentFlowStatusUnapproved,
Action: domain.PaymentFlowActionTriggerClientSDK,
ActionData: domain.FlowActionData{
URL: &url.URL{Scheme: "https", Host: "redirect-url.com"},
URL: &url.URL{Scheme: "https", Host: "redirect-url.com"},
DisplayData: `{"foo": "bar"}`,
},
},
},
want: want{
runResult: process.RunResult{Failed: nil},
state: states.TriggerClientSDK{}.Name(),
stateData: process.StateData(&url.URL{Scheme: "https", Host: "redirect-url.com"}),
stateData: process.StateData(states.TriggerClientSDKData{
URL: &url.URL{Scheme: "https", Host: "redirect-url.com"},
Data: `{"foo": "bar"}`,
}),
},
},
{
Expand Down
17 changes: 13 additions & 4 deletions checkout/domain/placeorder/states/trigger_client_sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,26 @@ type (
paymentService *application.PaymentService
validator process.PaymentValidatorFunc
}

// TriggerClientSDKData holds the data which must be sent to the client via SDK
TriggerClientSDKData struct {
URL *url.URL
Data string
}
)

var _ process.State = TriggerClientSDK{}

func init() {
gob.Register(&url.URL{})
gob.Register(TriggerClientSDKData{})
}

// NewTriggerClientSDKStateData creates data required for this state
func NewTriggerClientSDKStateData(url *url.URL) process.StateData {
return process.StateData(url)
func NewTriggerClientSDKStateData(url *url.URL, data string) process.StateData {
return process.StateData(TriggerClientSDKData{
URL: url,
Data: data,
})
}

// Inject dependencies
Expand Down Expand Up @@ -54,7 +63,7 @@ func (r TriggerClientSDK) Run(ctx context.Context, p *process.Process) process.R
}

// Rollback the state operations
func (r TriggerClientSDK) Rollback(ctx context.Context, _ process.RollbackData) error {
func (r TriggerClientSDK) Rollback(_ context.Context, _ process.RollbackData) error {
return nil
}

Expand Down
4 changes: 3 additions & 1 deletion checkout/domain/placeorder/states/trigger_client_sdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,7 @@ func TestTriggerClientSDK_Run(t *testing.T) {
}

func TestNewTriggerClientSDKStateData(t *testing.T) {
assert.Equal(t, process.StateData(&url.URL{Host: "test.com"}), states.NewTriggerClientSDKStateData(&url.URL{Host: "test.com"}))
assert.Equal(t,
process.StateData(states.TriggerClientSDKData{URL: &url.URL{Host: "test.com"}, Data: "data"}),
states.NewTriggerClientSDKStateData(&url.URL{Host: "test.com"}, "data"))
}
6 changes: 4 additions & 2 deletions checkout/interfaces/graphql/dto/states.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ type (
TriggerClientSDK struct {
Name string
URL string
Data string
}

// PaymentRequestAPI holds all data needed to create a PaymentRequest
Expand Down Expand Up @@ -192,8 +193,9 @@ func (s *PostRedirect) MapFrom(pctx process.Context) {
// MapFrom the internal process state to the graphQL state fields
func (t *TriggerClientSDK) MapFrom(pctx process.Context) {
t.Name = pctx.CurrentStateName
if stateData, ok := pctx.CurrentStateData.(*url.URL); ok {
t.URL = stateData.String()
if stateData, ok := pctx.CurrentStateData.(states.TriggerClientSDKData); ok {
t.URL = stateData.URL.String()
t.Data = stateData.Data
}
}

Expand Down
1 change: 1 addition & 0 deletions checkout/interfaces/graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ type Commerce_Checkout_PlaceOrderState_State_Redirect implements Commerce_Checko
type Commerce_Checkout_PlaceOrderState_State_TriggerClientSDK implements Commerce_Checkout_PlaceOrderState_State {
name: String!
URL: String!
Data: String!
}

type Commerce_Checkout_PlaceOrderState_State_ShowWalletPayment implements Commerce_Checkout_PlaceOrderState_State {
Expand Down
Loading

0 comments on commit 100608f

Please sign in to comment.