import { Injectable } from "@angular/core" ; import { StripeAddress , StripeBackendAPI , StripeConfig , StripeCustomerSession , StripePaymentListener , StripePaymentSession , StripeShippingAddressField , StripeShippingMethod } from "nativescript-stripe/standard" ; import * as httpModule from "tns-core-modules/http" ; import { Page } from "tns-core-modules/ui/page" ; export const publishableKey = "pk_test_yours" ; const backendBaseURL = "https://yours.herokuapp.com/" ; const appleMerchantID = "" ; @ Injectable ( ) export class StripeService implements StripeBackendAPI { private customerSession : StripeCustomerSession ; constructor ( ) { if ( - 1 !== publishableKey . indexOf ( "pk_test_yours" ) ) { throw new Error ( "publishableKey must be changed from placeholder" ) ; } if ( - 1 !== backendBaseURL . indexOf ( "https://yours.herokuapp.com/" ) ) { throw new Error ( "backendBaseURL must be changed from placeholder" ) ; } StripeConfig . shared ( ) . backendAPI = this ; StripeConfig . shared ( ) . publishableKey = publishableKey ; StripeConfig . shared ( ) . appleMerchantID = appleMerchantID ; StripeConfig . shared ( ) . companyName = "Demo Company" ; this . customerSession = new StripeCustomerSession ( ) ; } createSetupIntent ( ) { return this . _postRequest ( "create_setup_intent" ) . then ( response => response . content . toJSON ( ) ) ; } createPaymentIntent ( amount , currency = 'usd' ) { const content = `amount=${ amount }&currency=${ currency }` ; return this . _postRequest ( "create_payment_intent" , content ) . then ( response => response . content . toJSON ( ) ) ; } createCustomerKey ( apiVersion ) { const content = `api_version=${ apiVersion }` ; return this . _postRequest ( "ephemeral_keys" , content ) . then ( response => response . content . toJSON ( ) ) ; } capturePayment ( stripeID , amount , shippingMethod ? , shippingAddress ? ) { let content = `payment_method_id=${ stripeID }&amount=${ amount }` ; if ( shippingMethod && shippingAddress ) content += `&${ this . _encodeShipping ( shippingMethod , shippingAddress ) }` ; return this . _postRequest ( "confirm_payment_intent" , content ) . then ( response => response . content . toJSON ( ) ) ; } confirmPaymentIntent ( paymentIntentID ) { const content = `payment_intent_id=${ paymentIntentID }` ; return this . _postRequest ( "confirm_payment_intent" , content ) . then ( response => response . content . toJSON ( ) ) ; } createPaymentSession ( page , price , requireShipping , listener ? ) { StripeConfig . shared ( ) . requiredShippingAddressFields = requireShipping ? [ StripeShippingAddressField . PostalAddress ] : [ ] ; return new StripePaymentSession ( page , this . customerSession , price , "usd" , listener ) ; } showPaymentMethods ( paymentSession ) { paymentSession . presentPaymentMethods ( ) ; } showShipping ( paymentSession ) { paymentSession . presentShipping ( ) ; } requestPayment ( paymentSession ) { paymentSession . requestPayment ( ) ; } private _postRequest ( endpoint , content = '' ) { let url = this . _backendURL ( endpoint ) ; return httpModule . request ( { url : url , method : "POST" , headers : { "Content-Type" : "application/x-www-form-urlencoded; charset=utf-8" } , content } ) . then ( response => { if ( response . statusCode < 200 || response . statusCode >= 300 ) { throw new Error ( `Status: ${ response . statusCode }; ${ response . content . toString ( ) }.` ) ; } return response ; } ) ; } private _backendURL ( pathComponent ) { if ( ! backendBaseURL ) throw new Error ( "backendBaseURL must be set" ) ; if ( ! backendBaseURL . endsWith ( "/" ) ) { return backendBaseURL + "/" + pathComponent ; } else { return backendBaseURL + pathComponent ; } } private _encodeShipping ( method , address ) { function entry ( label , value ) { return value ? encodeURI ( label ) + "=" + encodeURI ( value ) : "" ; } return entry ( "shipping[carrier]" , method . label ) + entry ( "&shipping[name]" , address . name ) + entry ( "&shipping[address][line1]" , address . line1 ) + entry ( "&shipping[address][line2]" , address . line2 ) + entry ( "&shipping[address][city]" , address . city ) + entry ( "&shipping[address][state]" , address . state ) + entry ( "&shipping[address][country]" , address . country ) + entry ( "&shipping[address][postal_code]" , address . postalCode ) + entry ( "&phone" , address . phone ) + entry ( "&email" , address . email ) ; } }