28
36

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PayPal API決済の実装サンプル (PHP)

Last updated at Posted at 2017-08-02

PayPal API決済をPHPで実装する方法について、簡潔に纏めます。

リファレンス

PayPal API 決済

PayPal API決済 実装のお約束

決済実行の流れ

下記順番でPayPal API決済処理を実行していきます。

    1. PayPal決済開始API(SetExpressCheckout)実行
    1. PayPal決済開始APIから返却されたTokenを取得
  • 3. 取得したTokenをキーにPayPalへリダイレクト
  • 4. ユーザ側の決済処理完了後、PayPalからリダイレクトされたタイミングでPayPal決済API(DoExpressCheckoutPayment)を実行
  • 5. PayPal決済APIのレスポンス(トランザクションID、ステータスなど)を取得

PayPal API決済 サンプルコード(PHP)

PayPal API決済のサンプルコードを下記に記載する。
PayPalの接続先はSandBoxを利用しています。

PayPal決済開始

$POST_DATA = array(
    'USER' => 'ユーザ名',
    'PWD' => 'パスワード',
    'SIGNATURE' => 'APIトークン',
    'METHOD' => 'SetExpressCheckout',
    'VERSION' => 124,
    'PAYMENTREQUEST_0_AMT' => 10000,
    'PAYMENTREQUEST_0_CURRENCYCODE' => 'JPY',
    'PAYMENTREQUEST_0_PAYMENTACTION' => 'Sale',
    'cancelUrl' => 'キャンセル時の遷移先を指定',
    'returnUrl' => '成功時の遷移先を指定',
    'PAYMENTREQUEST_0_SHIPTONAME' => '田中太郎',
    'PAYMENTREQUEST_0_SHIPTOCOUNTRYCODE' => '日本'
);

$curl = curl_init("https://api-3t.sandbox.paypal.com/nvp");
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($POST_DATA));
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);

$output = curl_exec($curl);
$token = substr($output, 6, 22); // Token取得

$redirect_url = "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&useraction=commit&token=" . $token;
header("Location: " . $redirect_url); // 取得したTokenをセットし、PayPalへリダイレクト

return;

PayPal決済実行

$url = $_SERVER['HTTP_REFERER'];
$url = parse_url($url);
if (strpos($url['host'], 'paypal.com') === false) {
    return; // paypal以外のリダイレクト
}

// paypalからのリダイレクト時のクエリパラメータより、Tokenなどを取得
$token = $_GET[token];
$payer_id = $_GET[PayerID];

$POST_DATA = array(
    'USER' => 'ユーザ名',
    'PWD' => 'パスワード',
    'SIGNATURE' => 'APIトークン',
    'METHOD' => 'DoExpressCheckoutPayment',
    'VERSION' => 124,
    'PAYMENTREQUEST_0_PAYMENTACTION' => 'Sale',
    'PAYMENTREQUEST_0_AMT' => 10000,
    'PAYMENTREQUEST_0_CURRENCYCODE' => 'JPY',
    'TOKEN' => $token,
    'PAYERID' => $payer_id
);
$curl = curl_init("https://api-3t.sandbox.paypal.com/nvp");
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($POST_DATA));
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);

$output = curl_exec($curl);
$arr = explode('&', $output);

$txn_id;
$payment_status;
$error_code;

foreach ($arr as $line) {
    if ($txn_id !== "" && $payment_status !== "" && $error_code !== "") {
        break;
    }
    if (strpos($line, 'PAYMENTINFO_0_TRANSACTIONID=') !== false) {
        $txn_id = str_replace('PAYMENTINFO_0_TRANSACTIONID=', "", $line);
        continue;
    }
    if (strpos($line, 'PAYMENTINFO_0_PAYMENTSTATUS=') !== false) {
        $payment_status = str_replace('PAYMENTINFO_0_PAYMENTSTATUS=', "", $line);
        continue;
    }
    if (strpos($line, 'PAYMENTINFO_0_ERRORCODE=') !== false) {
        $error_code = str_replace('PAYMENTINFO_0_ERRORCODE=', "", $line);
    }
}

// 以下、後続処理続行

28
36
5

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
28
36

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?