import * as React from 'react' ; import { PaymentRequestParamsConfig , PaymentRequestInterface , PaymentRequestParams , Callback , Resolve , Reject , } from './types' ; let request ; const hasSupport = ( ) => ! ! ( window as any ) . PaymentRequest ; const addEventListener = ( requestListener , event , callback ? ) => { if ( ! ! callback ) { requestListener . addEventListener ( event , ( e ) => e . updateWith ( new Promise ( ( resolve , reject ) => callback ( requestListener , resolve , reject ) ) ) ) ; } } ; export const abort = ( ) => request . abort ( ) ; export const show = ( params ) => ( ) => { request = new PaymentRequest ( params . methodData , params . details , params . options || { } ) ; addEventListener ( request , 'shippingaddresschange' , params . onShippingAddressChange ) ; addEventListener ( request , 'shippingoptionchange' , params . onShippingOptionChange ) ; addEventListener ( request , 'onmerchantvalidation' , params . onMerchantValidation ) ; request . show ( ) . then ( ( paymentResponse ) => { return new Promise ( ( resolve , reject ) => params . onShowSuccess ( paymentResponse , resolve , reject ) ) . then ( ( ) => paymentResponse . complete ( 'success' ) ) . catch ( ( ) => paymentResponse . complete ( 'fail' ) ) ; } ) . catch ( ( err ) => params . onShowFail ( err ) ) ; } ; const paymentRequest = < TProps extends object > ( ) => ( WrappedComponent ) : React . ClassType < TProps & PaymentRequestParamsConfig , any , any > => ( class ExtendedComponent extends React . Component < TProps & PaymentRequestParamsConfig , void > { render ( ) { const { config , ... passedProps } = this . props as any ; const isSupported = hasSupport ( ) ; const supportedProps = isSupported && config ? { isSupported , abort , show : show ( config ) } : { isSupported } ; return < WrappedComponent { ... passedProps } { ... supportedProps } />; } } ) ; export default paymentRequest ;