[PHP] Payoneer 결제 API

Payoneer의 예치금을 이용해 결제를 처리할 수 있는 API이다. Payoneer 파트너사에 가입하고 지불처리하는 것이 주된 기능이다.

<?php
/**
 * @Author: Seongho Jang
 * @Date:   2017-07-21
 * @Last Modified by:   Seongho Jang
 * @Last Modified time: 2017-07-21 17:33:27
*/

class Payoneer
{
    const SANDBOX_API_URL    = 'https://api.sandbox.payoneer.com/Payouts/HttpApi/API.aspx';
    const PRODUCTION_API_URL = 'https://api.payoneer.com/payouts/HttpAPI/API.aspx';

    public $apiEndpoint;    
    public $apiUser;    
    public $apiPassword;
    public $partnerId;
    public $sandbox = true;
    
    private $response;
    private $params;
    
    function __construct($apiUser, $apiPassword, $partnerId, $sandbox = true) {
        $this->sandbox = $sandbox;
        if($sandbox == true){
            $this->apiEndpoint = static::SANDBOX_API_URL;    
        }
        else{
            $this->apiEndpoint = static::PRODUCTION_API_URL;
        }
        
        $this->apiUser = $apiUser;
        $this->apiPassword = $apiPassword;
        $this->partnerId = $partnerId;
    }

    public function getBasicParameters() {
        return ['p1' => $this->apiUser, 'p2' => $this->apiPassword, 'p3' => $this->partnerId];
    }

    public function getToken($request)
    {
        $this->call('GetToken', $request);
        $response = $this->response;
        return $this->getData();
    }

    public function getTokenXML($request)
    {
        $this->call('GetTokenXML', $request);
        $response = $this->response;
        return $this->getData('Token');
    }

    public function getApiStatus($item='')
    {
        $this->call('Echo');
        
        return $this->getData($item);
    }

    public function getVersion()
    {
        $this->call('GetVersion');
        $response = $this->response;
        return $this->getData('Version');
    }

    public function chargeAccount($request)
    {
        $this->call('ChargeAccount', $request);
        $response = $this->response;
        return $this->getData();
    }

    public function cancelChargeAccount($request)
    {
        $this->call('CancelChargeAccount', $request);
        $response = $this->response;
        return $this->getData();
    }

    public function getPaymentStatus($request)
    {
        $this->call('GetPaymentStatus', $request);
        $response = $this->response;
        return $this->getData();
    }

    public function convertArray($object)
    {
        return json_decode( json_encode( $object ), 1 );
    }

    public function getData($key='')
    {
        $xml = simplexml_load_string($this->response);

        $data = $this->convertArray($xml);

        if($key)
            return $data[$key];
        else
            return $data;
    }

    public function call($methodName, $request=null)
    {
        $this->params = $this->getBasicParameters();

        if($request && is_array($request))
            $this->params = array_merge($this->params, $request);

        $url = $this->apiEndpoint.'?mname='.$methodName;

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
        curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $this->params);
        
        $this->response = curl_exec($ch);
        $err = curl_error($ch);

        curl_close($ch);
    }
}

위 API를 이용해 Sign-Up 기능을 구현한 코드는 아래오 같다.

<?php
require_once('./Payoneer.Api.php');

$payee_id = trim($_POST['payee_id']);

$payoneer = new Payoneer(PAYONEER_USERNAME, PAYONEER_API_PASSWORD, PAYONEER_PARTNER_ID, PAYONEER_SANDBOX);

$request = array(
    'p4'  => $payee_id,
    'p6'  => 'http://example.com/payoneer/payoneer.php',
    'p8'  => 5,
    'p10' => 'True'
);

$result = $payoneer->getToken($request);

$error = '';
$token = '';

if(isset($result['Token'])) {
    $token = $result['Token'];
} else {
    $error = '['.$result['Code'].'] '.$result['Description'];
}

die(json_encode(array('error' => $error, 'token' => $token)));

 

편리

PHP와 MariaDB, jQuery 등을 사용해 게시판, 쇼핑몰 솔루션을 개발합니다. 그누보드5와 영카트5 개발에 참여 했습니다. Linux와 Nginx는 물론 WordPress, Git 등에도 관심이 많습니다. 자전거 타기 및 사진 촬영을 취미로 하고 있습니다.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.