Xamarin Intent.PutExtra 에 byte array 붙이기 오류.

안녕하세요. 평생을 문자열만 하다가 byte 를 하려니 도통 해결이 안되어 다시 질문을 올려봅니다.
결제를 붙이고 있습니다.

KSNET AppToApp 결제요청을 추가하고 있습니다.
Java byte 와 C# byte가 서로 달라서 그런가요?

[ 전문 규칙 ]
image

[ AppToApp 규칙 ]
image

public Task<string> RequestPayToKSNET()
{
  var result = "";

  try
  {
    var activity = Platform.CurrentActivity as MainActivity;

    var componentName = new ComponentName("com.ksnet.kscat_a", "com.ksnet.kscat_a.PaymentIntentActivity");

    var intent = new Intent(Intent.ActionMain);
    intent.SetFlags(ActivityFlags.SingleTop);
    intent.AddCategory(Intent.CategoryLauncher);
    intent.SetComponent(componentName);

    if ( intent != null )
    {

      using var bb = ByteBuffer.Allocate(4096);

      //Encoding df = Encoding.Default;
      //Encoding u7 = Encoding.UTF7;
      Encoding u8 = Encoding.UTF8;
      //Encoding ac = Encoding.ASCII;
      //Encoding u16LE = Encoding.Unicode;
      //Encoding u16BE = Encoding.BigEndianUnicode;
      //Encoding u32 = Encoding.UTF32;

      Encoding encoding = u8;

      var stx = Convert.ToSByte(0x02);
      bb.Put(stx); // STX (start of text) 0x02 / 2

      bb.Put(encoding.GetBytes("IC"));  // 거래구분 - 신용 IC : IC
      bb.Put(encoding.GetBytes("01"));  // 업무구분 - 승인/취소 : 01
      bb.Put(encoding.GetBytes("0200"));  // 전문구분 - 승인/조회 : 0200 / 0210(응답)
      bb.Put(encoding.GetBytes("N")); // 거래형태 N: 일반

      bb.Put(encoding.GetBytes("DPT0TEST03".PadLeft(10, ' ')));  // 단말기번호 : DPT0TEST03 (테스트)
      bb.Put(encoding.GetBytes("".PadRight(4, ' ')));  // 업체정보(가맹점에서 임의 사용)
      bb.Put(encoding.GetBytes("".PadRight(12, ' '))); // 전문일련번호 (가맹점에서 임의 사용 ), (망취소시 원거래 키 - 망취소사용시 필수 입력 사항임 )
      bb.Put(encoding.GetBytes("".PadLeft(1, ' '))); // POS Entry Mode   // IC ( 문서는 공백, 샘플은 S )
      bb.Put(encoding.GetBytes("".PadRight(20, ' '))); // 거래고유번호
      bb.Put(encoding.GetBytes("".PadRight(20, ' '))); // 암호화하지 않은 카드 번호
      bb.Put(encoding.GetBytes("".PadRight(1, ' '))); // 암호화 여부 ( 문서는 공백, 샘플릉 9 )
      bb.Put(encoding.GetBytes("".PadRight(16, ' ')));  // SW 모델번호 ( 문서는 공백, 샘플은 # )
      bb.Put(encoding.GetBytes("".PadRight(16, ' ')));  // CAT or Reader 모델번호 ( 문서는 공백, 샘플은 # )
      bb.Put(encoding.GetBytes("".PadRight(40, ' ')));  // 암호화 정보
      bb.Put(encoding.GetBytes("".PadRight(37, ' ')));  // Track II  

      var FS = Convert.ToSByte(0x1C);
      bb.Put(FS); // FS (필드 구분자) : 0x1C / 28
      //bb.Put(0x1C);

      bb.Put(encoding.GetBytes("00")); // 할부
      bb.Put(encoding.GetBytes("1004".PadLeft(12, '0'))); // 총금액
      bb.Put(encoding.GetBytes("0".PadLeft(12, '0')));  // 봉사료
      bb.Put(encoding.GetBytes("91".PadLeft(12, '0')));  // 세금(부가세)
      bb.Put(encoding.GetBytes("913".PadLeft(12, '0')));  // 공급금액
      bb.Put(encoding.GetBytes("0".PadLeft(12, '0'))); // 면세금액

      bb.Put(encoding.GetBytes("".PadLeft(2, ' ')));  // Working Key Index 
      bb.Put(encoding.GetBytes("".PadLeft(16, ' ')));  // 비밀번호 
      bb.Put(encoding.GetBytes("".PadLeft(12, ' ')));  // 원거래승인번호
      bb.Put(encoding.GetBytes("".PadLeft(6, ' '))); // 원거래승인일자
      bb.Put(encoding.GetBytes("".PadLeft(163, ' '))); // 사용자정보 ~ DCC 환율조회 Data

      bb.Put(encoding.GetBytes("N")); // 전자서명 유무

      var etx = Convert.ToSByte(0x03);
      bb.Put(etx); // ETX : 0x03 / 3
      //bb.Put(0x03);

      var cr = Convert.ToSByte(0x0D);
      bb.Put(cr); // CR : 0x0D / 13
      //bb.Put(0x0D);

      // CR 적용안되 bb.Position() + 1 해보았으나 여전히 전문 오류.
      var telegram = new byte[bb.Position()];
      bb.Rewind();
      bb.Get(telegram);

      System.Diagnostics.Debug.WriteLine($"========== telegramLength: {telegram.Length}");
      
      byte[] payRequestTelegram = new byte[telegram.Length + 4];

      var telegramLength = encoding.GetBytes(telegram.Length.ToString().PadLeft(4, '0'));

      System.Buffer.BlockCopy(telegramLength, 0, payRequestTelegram, 0, 4);
      System.Buffer.BlockCopy(telegram, 0, payRequestTelegram, 4, telegram.Length);

      System.Diagnostics.Debug.WriteLine($"payRequestTelegram: {encoding.GetString(payRequestTelegram)}");
      System.Diagnostics.Debug.WriteLine($"telegramLength: {encoding.GetString(telegramLength)}");
      System.Diagnostics.Debug.WriteLine($"============ Total length: {telegramLength.Length + telegram.Length}");

      System.Diagnostics.Debug.WriteLine($"HexString: {BitConverter.ToString(payRequestTelegram).Replace("-", " ")}");
      System.Diagnostics.Debug.WriteLine($"payRequestTelegram: {encoding.GetString(payRequestTelegram)}");

      // PayRequest -> KSNET 전문 대체
      // Telegram, TelegamLength, SignData, SigLen, TradeCode
      intent.PutExtra("Telegram", payRequestTelegram);
      intent.PutExtra("TelegamLength", payRequestTelegram.Length);

      activity.StartActivityForResult(intent, 100);

      result = encoding.GetString(payRequestTelegram);
    }
  }
  catch ( Exception ex )
  {
    System.Diagnostics.Debug.WriteLine($"RequestPayToKSNET: {ex.Message}");
  }

  return Task.FromResult(result);
}

payRequestTelegram: 0452IC010200NDPT0TEST03 00000000001004000000000000000000000091000000000913000000000000 N

응답이 “전문 오류” 로 옵니다;;;;

var responseTelegram = data.GetByteArrayExtra("responseTelegram");
System.Diagnostics.Debug.WriteLine($"responseTelegram: {Encoding.UTF8.GetString(responseTelegram)}");

message1 ; 오류
message2 : 전문오류

// 인코딩이 안맞나요??
responseTelegram: 0454 F1001 230502185201 ���� ���� ���� 1001 AA0000000000000000000000000000000000000000000000000000 

3개의 좋아요

이럴수가…TYPO …자괴감 들다가 어린이날 선물받은 기분이라고 생각하려고요.

intent.PutExtra(“TelegamLength”, payRequestTelegram.Length);

TelegarmLength

3개의 좋아요

OnActivityResult 에서 한글깨지는건

  var responseTelegram = bundle.GetByteArray("responseTelegram");
  var result = bundle.GetInt("result", 0);
  var message1 = bundle.GetString("message1");
  var message2 = bundle.GetString("message2");

  var encoding = Encoding.GetEncoding("euc-kr");

euc-kr 이라고 상상도 못했습니다;;;;

image

3개의 좋아요