Skip to content
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

feat(Apple Pay): allow for customization of event updates #808

Merged
merged 2 commits into from
Apr 7, 2023

Conversation

cbarton
Copy link
Member

@cbarton cbarton commented Apr 5, 2023

Adds the options.callbacks option which supports the following
callbacks:

  • onPaymentMethodSelected
  • onShippingContactSelected
  • onShippingMethodSelected
  • onPaymentAuthorized
    to correspond with the ApplePaySession events.

A callback must return either the Apple Pay update object or a Promise
that resolves with that object.

That update object will be used to complete the ApplePaySession event
to open up the ability to fetch custom taxes on payment method selection,
update shipping costs on shipping contact/method selection and validate
email/phone addresses on authorization.

This does not remove the existing events being emitted but the workflow
should be updated to prefer using onPaymentAuthorized to complete the
purchase instead of async on the token event. The event that is sent
to that callback includes the payment.recurlyToken for use.

This also enhances the token event to include the raw payment event as
the second argument.

const applePay = recurly.ApplePay({ 
  ..., 
  paymentRequest: { requiredShippingContactFields: ['email'] },
  callbacks: {
    onPaymentAuthorized: ({ payment: { recurlyToken, shippingContact: { emailAddress } } }) => {
      if (emailAddress === "test@example.com") return { errors: [new window.ApplePayError(...)] };
    },
  },
});

This also enhances the token event to include the raw payment event as the second argument.

@cbarton
Copy link
Member Author

cbarton commented Apr 5, 2023

Another option is to allow the client to pass in the various handlers on initialize and they call the complete function (that we control) to close out:

const applePay = recurly.ApplePay({ 
  callbacks: {
    onPaymentMethodSelected: (event: ApplePayPaymentMethodEvent, 
                              complete: (update: ApplePayPaymentMethodUpdate) => void) => void;
  },
});

We would still support the emitters as well, but this would be the primary way of defining the explicit apple callbacks.

The token event still works fine, though we should think about if we want the paymentAuthorized event to trigger after we receive the token (and emit it as part of the event) so the merchant can process the purchase to be more aligned with the UX of the payment sheet flow.

@coveralls
Copy link

coveralls commented Apr 5, 2023

Coverage Status

Coverage: 92.473% (+0.04%) from 92.437% when pulling 466aaf1 on apple-pay-errors into 31fc194 on master.

Adds the `options.callbacks` option which supports the following
callbacks:
- `onPaymentMethodSelected`
- `onShippingContactSelected`
- `onShippingMethodSelected`
- `onPaymentAuthorized`
to correspond with the `ApplePaySession` events.

A callback must return either the Apple Pay update object or a Promise
that resolves with that object.

That update object will be used to complete the `ApplePaySession` event
to open up the ability to fetch custom taxes on payment method selection,
update shipping costs on shipping contact/method selection and validate
email/phone addresses on authorization.

This does not remove the existing events being emitted but the workflow
should be updated to prefer using `onPaymentAuthorized` to complete the
purchase instead of async on the `token` event. The event that is sent
to that callback includes the `payment.recurlyToken` for use.

This also enhances the `token` event to include the raw payment event as
the second argument.
@chrissrogers chrissrogers merged commit 9bca45b into master Apr 7, 2023
@chrissrogers chrissrogers deleted the apple-pay-errors branch April 7, 2023 19:48
recurly-integrations pushed a commit that referenced this pull request Apr 19, 2023
[Full Changelog](v4.22.9...v4.23.0)

**Merged Pull Requests**

- chore(paypal): add types for display options [#812](#812) ([cbarton](https://github.com/cbarton))
- fix(apple pay): Do not teardown Braintree on cancel [#811](#811) ([cbarton](https://github.com/cbarton))
- fix(types): add type definition for Recurly#ready [#810](#810) ([cbarton](https://github.com/cbarton))
- feat(google pay): add support for the paymentDataRequest [#809](#809) ([cbarton](https://github.com/cbarton))
- feat(Apple Pay): allow for customization of event updates [#808](#808) ([cbarton](https://github.com/cbarton))
- Update config type with requiredShippingContactFields [#806](#806) ([BARK-RMILLER](https://github.com/BARK-RMILLER))
- feat(apple pay): add support for `recurringPaymentRequest`, move to `options.paymentRequest` [#804](#804) ([cbarton](https://github.com/cbarton))
- fix(apple pay): emit events when restore pricing addresses [#803](#803) ([cbarton](https://github.com/cbarton))
- chore(apple pay): capture async test errors to fail faster [#802](#802) ([cbarton](https://github.com/cbarton))
- feat(apple pay): populate contacts with pricing addresses [#801](#801) ([cbarton](https://github.com/cbarton))
- feat(apple pay): allow for supportedNetworks selection [#800](#800) ([cbarton](https://github.com/cbarton))
- feat(apple pay): update tax when billing/shipping contact changes [#798](#798) ([cbarton](https://github.com/cbarton))
- feat(apple pay): populate billingContact with form address fields [#797](#797) ([cbarton](https://github.com/cbarton))
- feat(Apple Pay): validate and support more ApplePayPaymentRequest features [#794](#794) ([cbarton](https://github.com/cbarton))
- [feat][google-pay] send raw payment data to server [#789](#789) ([cbarton](https://github.com/cbarton))

**Closed Issues**

- property 'ready' does not exist on type 'Recurly'.ts [#795](#795)
- Missing types definitions for `coupon` method [#698](#698)
- If formRef unmounts during call, recurly.token fails silently. [#697](#697)
- The user can enter 4 digits as the month value in the expiration date field. [#678](#678)

##### Minified MD5 Checksum
```
75584733b78f6ca6c6a65267b07fb9ad ./build/recurly.min.js
```
##### [SRI Hash](https://www.srihash.org/)
```
pX41INy9qepZfHHYL13noWEbReg+bDOqU2jzjgkC3kkDh36KlPFeiJBxUgca8DtP
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants