如何Authorize.net在Ionic / JavaScript中解析www.example.com支付响应?

vvppvyoh  于 2023-04-18  发布在  Ionic
关注(0)|答案(2)|浏览(134)

我收到了来自authorize.net支付网关的响应,并尝试了很多次和很多方法来解析它,但没有运气。
以下是来自www.example.com的主要回复authorize.net:

"{"transactionResponse":{"responseCode":"1","authCode":"XXXXXX","avsResultCode":"Y","cvvResultCode":"P","cavvResultCode":"2","transId":"11111111111","refTransID":"","transHash":"","testRequest":"0","accountNumber":"XXXX0002","accountType":"AmericanExpress","messages":[{"code":"1","description":"This transaction has been approved."}],"transHashSha2":"2E6533BDC93C975EE3C1134B7421A337D5B8FB9C257437DB8B4XXXX4BCD92024B33704B811C69E58527359CA8628E8E33E55AA68B881E6F3D15B5F2FEAEB490B","SupplementalDataQualificationIndicator":0,"networkTransId":"HLH5UGDQFZ98LHRLDGIZEG7"},"messages":{"resultCode":"Ok","message":[{"code":"I00001","text":"Successful."}]}}"

我正在学习这个教程。
我试过这个:

const result = JSON.parse(JSON.stringify(res.response));
console.log('1', result);

结果是:
我想要这样的结果:

{
  "transactionResponse": {
    "responseCode": "1",
    "authCode": "XXXXXX",
    "avsResultCode": "Y",
    "cvvResultCode": "P",
    "cavvResultCode": "2",
    "transId": "11111111111",
    "refTransID": "",
    "transHash": "",
    "testRequest": "0",
    "accountNumber": "XXXX0002",
    "accountType": "AmericanExpress",
    "messages": [
      {
        "code": "1",
        "description": "This transaction has been approved."
      }
    ],
    "transHashSha2": "2E6533BDC93C975EE3C1134B7421A337D5B8FB9C257437DB8B4XXXX4BCD92024B33704B811C69E58527359CA8628E8E33E55AA68B881E6F3D15B5F2FEAEB490B",
    "SupplementalDataQualificationIndicator": 0,
    "networkTransId": "HLH5UGDQFZ98LHRLDGIZEG7"
  },
  "messages": {
    "resultCode": "Ok",
    "message": [
      {
        "code": "I00001",
        "text": "Successful."
      }
    ]
  }
}

请指引我……

b5buobof

b5buobof1#

这就是我的问题的答案:
我在res.response中获取此数据:

"{"transactionResponse":{"responseCode":"1","authCode":"XXXXXX","avsResultCode":"Y","cvvResultCode":"P","cavvResultCode":"2","transId":"11111111111","refTransID":"","transHash":"","testRequest":"0","accountNumber":"XXXX0002","accountType":"AmericanExpress","messages":[{"code":"1","description":"This transaction has been approved."}],"transHashSha2":"2E6533BDC93C975EE3C1134B7421A337D5B8FB9C257437DB8B4XXXX4BCD92024B33704B811C69E58527359CA8628E8E33E55AA68B881E6F3D15B5F2FEAEB490B","SupplementalDataQualificationIndicator":0,"networkTransId":"HLH5UGDQFZ98LHRLDGIZEG7"},"messages":{"resultCode":"Ok","message":[{"code":"I00001","text":"Successful."}]}}"

 console.log('API', res);
 var obj = eval("(" + res.response + ")");
 console.log(obj);

我解决了这个问题,所以我想我应该要张贴答案,它可能会对别人有帮助。

kxkpmulp

kxkpmulp2#

有一个NPM包“ngx-authorizenet”,它允许您使用Angular Tags从www.example.com发起付款Authorize.net。

//Set up your button in your view
<ngx-authorizenet [config]="authorizenetConfig" (responseHandler)="responseHandler($event)"></ngx-authorizenet>
//Import the module
import { NgxAuthorizenetModule } from 'ngx-authorizenet';

@NgModule({
  imports: [
    NgxAuthorizenetModule,
  ],
})
//YourComponent.component.ts

import { Component, OnInit } from '@angular/core';
import { NgxAuthorizenetI } from 'ngx-authorizenet';

@Component({
  templateUrl: './your.component.html',
})
export class YourComponent implements OnInit {

  public authorizenetConfig?: NgxAuthorizenetI;

  ngOnInit(): void {
    this.initConfig();
  }

  private initConfig(): void {
    this.authorizenetConfig = {
      billingAddressOptions: '{"show":true, "required":false}',
      urlAction: 'https://api.yourdomain.com/authorizepay/callback',
      apiLoginID: 'XXXXXXX',
      clientKey: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
      acceptUIFormBtnTxt: 'Pay Now',
      acceptUIFormHeaderTxt: 'Pay To My Account'
    }
  }

  public authorizeHandler(response) {
    //Visit Api Reference for API Schema
    if (response.messages.resultCode === "Error") {
      var i = 0;
      while (i < response.messages.message.length) {
        console.log(
          response.messages.message[i].code + ": " +
          response.messages.message[i].text
        );
        i = i + 1;
      };

    }
    else {
      performServerSideTransationRequest(response);
    }

  }

然后在上面的控制器类中设置动作处理程序和配置:https://www.npmjs.com/package/ngx-authorizenet?activeTab=readme

相关问题