import { Injectable } from "@angular/core"; import { PaymentService } from "../services/paymentService"; import { PaymentRequest, CardRequest } from '../../public-api' @Injectable({ providedIn: 'root' }) export class PaymentState { constructor(private service:PaymentService) { } // postSquare(request: PaymentRequest,done:any,error:any) { // this.service.squarePayment(request).subscribe({ // next(rslt) { // done(rslt) // }, error(e) { // error(e); // }, complete() { // } // }) // } checkBank(request: {xml:string},done:any,error:any) { this.service.checkBank(request).subscribe({ next(rslt) { done(rslt) }, error(e) { error(e); }, complete() { if(this){ } } }) } saveCard(request:CardRequest, done:any,error:any){ this.service.storeCard(request).subscribe({ next(rslt) { done(rslt) }, error(e) { error(e); }, complete() { } }) } // chargeCard(request:PaymentRequest, done:any,error:any){ // this.service.squarePayment(request).subscribe({ // next(rslt) { // done(rslt.results) // }, error(e) { // error(e); // }, complete() { // } // }) // } // checkBank(args: {xml:string},done:any,error:any){ // return this.service.checkBank(args) // .subscribe({ // next(rslt) { // done(rslt) // }, error(e) { // error(e); // }, complete() { // } // }) // // .pipe( // // map((xmlString: IRslt)=>{ // // const asJson = this.xmlStringToJson(xmlString.results); // // return asJson; // // }), // // catchError((err)=> { // // console.warn('INT ERR:', err); // // return err; // // }) // // ); // // .subscribe( // // (response) => { // // done(this.xmlStringToJson(response)) // // }, // // (error) => { // // err(error) // // }) // } xmlStringToJson(xml: string) { // Convert the XML string to an XML Document. const oParser = new DOMParser(); const oDOM = oParser.parseFromString(xml, "application/xml"); // Convert the XML Document to a JSON Object. return this.xmlToJson(oDOM); } xmlToJson(xml:any) { // Create the return object var obj = {} as any; if (xml.nodeType == 1) { // element // do attributes if (xml.attributes.length > 0) { obj[`@attributes`] = {}; for (var j = 0; j < xml.attributes.length; j++) { var attribute = xml.attributes.item(j); obj["@attributes"][attribute.nodeName] = attribute.nodeValue; } } } else if (xml.nodeType == 3) { // text obj = xml.nodeValue; } // do children if (xml.hasChildNodes()) { for(var i = 0; i < xml.childNodes.length; i++) { var item = xml.childNodes.item(i); var nodeName = item.nodeName; if (typeof(obj[nodeName]) == "undefined") { obj[nodeName] = this.xmlToJson(item); } else { if (typeof(obj[nodeName].push) == "undefined") { var old = obj[nodeName]; obj[nodeName] = []; obj[nodeName].push(old); } obj[nodeName].push(this.xmlToJson(item)); } } } return obj; } }