수색…


매개 변수

매개 변수 세부
card_data 트랜잭션에 대한 지불 정보가 포함 된 JSON 객체
credit_card_details 아뜰로 치기 위해 PayPal로 전송되는 신용 카드 데이터가 포함 된 JSON 객체
client_id PayPal 애플리케이션 클라이언트 ID (OAuth 2 자격증 명)
페이팔 PayPal 노드 SDK 참조
비밀 PayPal 애플리케이션 비밀번호 (OAuth 2 자격증 명)
uuid node-uuid 패키지에 대한 참조

비고

이 샘플에서는 PayPal SDK를 사용하여 간단한 신용 카드 거래에 대한 크레딧을 제시합니다.

노드 샘플

NPM에서 PayPal 노드 모듈을 설치하여 시작하십시오.

npm install paypal-rest-sdk

응용 프로그램 파일에서 SDK의 구성 정보를 추가하십시오

var paypal = require('paypal-rest-sdk');

var client_id = 'YOUR CLIENT ID';
var secret = 'YOUR SECRET';

paypal.configure({
    'mode': 'sandbox', //sandbox or live
    'client_id': client_id,
    'client_secret': secret
});

SDK에 대한 요구 사항을 추가 한 다음 응용 프로그램을 만들 때 얻은 클라이언트 ID 및 암호에 대한 변수를 설정합니다. 그런 다음 이러한 세부 정보를 사용하여 응용 프로그램을 구성하고 우리가 작업하고있는 환경 (라이브 또는 샌드 박스)을 지정합니다.

다음으로, 지불 인에 대한 지불 정보가 들어있는 JSON 오브젝트를 설정한다.

var card_data = {
  "intent": "sale",
  "payer": {
    "payment_method": "credit_card",
    "funding_instruments": [{
      "credit_card": {
        "type": "visa",
        "number": "4417119669820331",
        "expire_month": "11",
        "expire_year": "2018",
        "cvv2": "874",
        "first_name": "Joe",
        "last_name": "Shopper",
        "billing_address": {
          "line1": "52 N Main ST",
          "city": "Johnstown",
          "state": "OH",
          "postal_code": "43210",
          "country_code": "US" }}}]},
  "transactions": [{
    "amount": {
      "total": "7.47",
      "currency": "USD",
      "details": {
        "subtotal": "7.41",
        "tax": "0.03",
        "shipping": "0.03"}},
    "description": "This is the payment transaction description." 
}]};

sale intent 를 추가하고 credit_cardpayment_method 를 추가하십시오. 다음으로, funding_instruments 에서 신용 카드에 대한 카드 및 주소 세부 정보를 추가하고 transactions 청구 할 금액을 입력하십시오. 여기에 여러 트랜잭션 객체를 배치 할 수 있습니다.

마지막으로 payment를 처리하기 위해 card_data 객체를 전달하여 payment.create(...) 요청합니다.

paypal.payment.create(card_data, function(error, payment){
  if(error){
    console.error(error);
  } else {
    console.log(payment);
  }
});

트랜잭션이 성공하면 다음과 비슷한 응답 객체가 표시됩니다.

{
  "id": "PAY-9BS08892W3794812YK4HKFQY",
  "create_time": "2016-04-13T19:49:23Z",
  "update_time": "2016-04-13T19:50:07Z",
  "state": "approved",
  "intent": "sale",
  "payer": {
    "payment_method": "credit_card",
    "funding_instruments": [
      {
        "credit_card": {
          "type": "visa",
          "number": "xxxxxxxxxxxx0331",
          "expire_month": "11",
          "expire_year": "2018",
          "first_name": "Joe",
          "last_name": "Shopper",
          "billing_address": {
            "line1": "52 N Main ST",
            "city": "Johnstown",
            "state": "OH",
            "postal_code": "43210",
            "country_code": "US"
          }
        }
      }
    ]
  },
  "transactions": [
    {
      "amount": {
        "total": "7.47",
        "currency": "USD",
        "details": {
          "subtotal": "7.41",
          "tax": "0.03",
          "shipping": "0.03"
        }
      },
      "description": "This is the payment transaction description.",
      "related_resources": [
        {
          "sale": {
            "id": "0LB81696PP288253D",
            "create_time": "2016-04-13T19:49:23Z",
            "update_time": "2016-04-13T19:50:07Z",
            "amount": {
              "total": "7.47",
              "currency": "USD"
            },
            "state": "completed",
            "parent_payment": "PAY-9BS08892W3794812YK4HKFQY",
            "links": [
              {
                "href": "https:\/\/api.sandbox.paypal.com\/v1\/payments\/sale\/0LB81696PP288253D",
                "rel": "self",
                "method": "GET"
              },
              {
                "href": "https:\/\/api.sandbox.paypal.com\/v1\/payments\/sale\/0LB81696PP288253D\/refund",
                "rel": "refund",
                "method": "POST"
              },
              {
                "href": "https:\/\/api.sandbox.paypal.com\/v1\/payments\/payment\/PAY-9BS08892W3794812YK4HKFQY",
                "rel": "parent_payment",
                "method": "GET"
              }
            ],
            "fmf_details": {
              
            },
            "processor_response": {
              "avs_code": "X",
              "cvv_code": "M"
            }
          }
        }
      ]
    }
  ],
  "links": [
    {
      "href": "https:\/\/api.sandbox.paypal.com\/v1\/payments\/payment\/PAY-9BS08892W3794812YK4HKFQY",
      "rel": "self",
      "method": "GET"
    }
  ],
  "httpStatusCode": 201
}

이 반환 객체에서 approved state 는 거래가 성공적임을 나타냅니다. links 개체 아래에는 방금 수행 한 작업에서 취할 수있는 잠재적 인 다음 단계를 제공하는 수많은 HATEOAS 링크가 있습니다. 이 경우, 우리는에 GET 요청함으로써 지불에 대한 정보를 검색 할 수 있습니다 self 제공하는 엔드 포인트를.

둥근 신용 카드 (노드)로 지불하기

이 예에서는 PayPal 저장고를 사용하여 신용 카드를 저장 한 다음 저장 한 신용 카드를 참조하여 사용자의 신용 카드 거래를 처리하는 방법을 살펴 보겠습니다.

금고를 사용하려는 이유는 민감한 신용 카드 정보를 자체 서버에 저장할 필요가 없기 때문입니다. 제공된 Vault ID를 통해 지불 방법을 간단하게 참조 할 수 있습니다. 즉, 우리는 신용 카드를 직접 보관할 때 PCI 준수 규정을 다룰 필요가 없습니다.

이전 샘플과 마찬가지로 환경 설정부터 시작합니다.

var paypal = require('paypal-rest-sdk'),
    uuid = require('node-uuid');

var client_id = 'YOUR CLIENT ID';
var secret = 'YOUR SECRET';

paypal.configure({
  'mode': 'sandbox', //sandbox or live
  'client_id': client_id,
  'client_secret': secret
});

앞의 샘플과의 한 가지 다른 점은 카드를 저장할 때 지불자가 고유 한 UUID를 생성하는 데 사용되는 새로운 패키지 node-uuid 가 필요하다는 것입니다. 다음을 통해 해당 패키지를 설치할 수 있습니다.

npm install node-uuid

다음으로, 우리는 저장을 위해 PayPal 보관소로 보낼 신용 카드 JSON 개체를 정의합니다. 여기에는 카드의 정보와 node-uuid 사용하여 생성 한 고유 한 지불 자 ID가 포함되어 있습니다. 이 고유 한 payer_id 는 아 payer_id 카드로 결제 할 때 사용되므로 데이터베이스에 저장해야합니다.

var create_card_details = {
    "type": "visa",
    "number": "4417119669820331",
    "expire_month": "11",
    "expire_year": "2018",
    "first_name": "John",
    "last_name": "Doe",
    "payer_id": uuid.v4()
};

마지막으로, 우리는 신용 카드를 저장하고 그 카드를 사용하여 지불을 처리해야합니다. 신용 카드를 보관하기 위해 방금 생성 한 credit_card_details 객체를 전달하여 credit_card.create(...) 호출합니다. 모든 것이 잘되면, 우리는 아치형 카드에 관한 세부 사항을 가진 물건을 우리에게 돌려 주어야합니다. 해당 카드로 지불하기 위해 두 개의 정보 즉, 이미 저장된 payer_id와 저장소 ID가 필요합니다.이 ID는 자체 데이터베이스에 참조로 저장해야합니다.

paypal.credit_card.create(create_card_details, function(error, credit_card){
    if(error){
        console.error(error);
    } else {
        var card_data = {
            "intent": "sale",
            "payer": {
                "payment_method": "credit_card",
                "funding_instruments": [{
                    "credit_card_token": {
                        "credit_card_id": credit_card.id,
                        "payer_id": credit_card.payer_id
                    }
                }]
            },
            "transactions": [{
                "amount": {
                    "total": "7.47",
                    "currency": "USD",
                    "details": {
                        "subtotal": "7.41",
                        "tax": "0.03",
                        "shipping": "0.03"
                    }
                },
                "description": "This is the payment transaction description." 
            }]
        };
        
        paypal.payment.create(card_data, function(error, payment){
            if(error){
                console.error(error);
            } else {
                console.log(JSON.stringify(payment));
            }
        });
    }
});

신용 카드를 성공적으로 보관 한 다음 섹션에서는 이전의 신용 카드 처리 예제에서와 마찬가지로 단순히 카드 세부 정보를 정의하고 지불 처리 만하고 있습니다. 의 구조에서 가장 큰 차이점 card_data 목적은 인 funding_instruments 우리는 아래에서 정의하는 것이, 부 payer . 신용 카드 정보를 정의하는 대신 볼트 ID 참조 및 지불 자 ID가 포함 된 다음 객체를 사용합니다.

"credit_card_token": {
    "credit_card_id": credit_card.id,
    "payer_id": credit_card.payer_id
}

이것이 우리가 아뜰리에 카드를 사용하여 지불을 처리하는 방법입니다.



Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow