フージー(@fuujii__)です。
PHPで組んでるサイトでPayPal REST APIで月額課金を組んだとき、情報とっちらかってるので、まとめ。
PayPal登録〜実装まで
1 ペイパルwebサイトから登録
2 デベロッパーサイトでREST APIを登録
3 テストアカウントを登録
販売者、購買者をそれぞれ登録。各100万円程度を登録。
4 WEBHOOKを登録
今回は解約時だけを登録すれば事足りた。
5 プロジェクトにSDKを組み込み
❌ついてるけど大丈夫なんで気にしない。。
6 Making First Call
↓このページを読んでその通りにやる。GREAT!
7 サンプルコード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
<?php use PayPal\Api\ChargeModel; use PayPal\Api\Currency; use PayPal\Api\MerchantPreferences; use PayPal\Api\PaymentDefinition; use PayPal\Api\Plan; use PayPal\Api\Patch; use PayPal\Api\PatchRequest; use PayPal\Common\PayPalModel; use PayPal\Api\Agreement; use PayPal\Api\Payer; use PayPal\Api\ShippingAddress; 〜〜〜 // 1. Autoload the SDK Package. This will include all the files and classes to your autoloader // require ('../../PayPal-PHP-SDK/autoload.php'); // 2. Provide your Secret Key. Replace the given one with your app clientId, and Secret // https://developer.paypal.com/webapps/developer/applications/myapps $apiContext = new \PayPal\Rest\ApiContext( new \PayPal\Auth\OAuthTokenCredential( PAYPAY_CLIENT_ID, // ClientID PAYPAY_CLIENT_SECRET // ClientSecret ) ); // Step 2.1 : Between Step 2 and Step 3 $apiContext->setConfig( array( 'mode' => 'sandbox', // 'mode' => 'live', 'log.LogEnabled' => true, 'log.FileName' => 'PayPal.log', 'log.LogLevel' => 'DEBUG', 'http.CURLOPT_CONNECTTIMEOUT' => 30, 'validation.level'=>"disable", 'cache.enabled'=>true, 'cache.FileName'=>"auth.cache" ) ); //create plan // Create a new instance of Plan object $plan = new Plan(); // # Basic Information // Fill up the basic information that is required for the plan $plan->setName('月額基本プラン') ->setDescription('テスト月額支払い基本コース。月額¥300-(税別)初月無料') ->setType('fixed'); // # Payment definitions for this billing plan. $paymentDefinition = new PaymentDefinition(); $paymentDefinition->setName('Regular Payments') ->setType('REGULAR') ->setFrequency('Month') ->setFrequencyInterval("1") ->setCycles("12") ->setAmount(new Currency(array('value' => 300, 'currency' => 'JPY'))); // Charge Models $chargeModel = new ChargeModel(); $chargeModel->setType('SHIPPING') ->setAmount(new Currency(array('value' => 30, 'currency' => 'JPY'))); $paymentDefinition->setChargeModels(array($chargeModel)); $merchantPreferences = new MerchantPreferences(); $merchantPreferences->setReturnUrl($okurl) ->setCancelUrl($cancelurl) ->setAutoBillAmount("yes") ->setInitialFailAmountAction("CONTINUE") ->setMaxFailAttempts("0") ->setSetupFee(new Currency(array('value' => 0, 'currency' => 'JPY'))); $plan->setPaymentDefinitions(array($paymentDefinition)); $plan->setMerchantPreferences($merchantPreferences); try { $output = $plan->create($apiContext); } catch (Exception $ex) { exit(1); } //3 plan to ACTIVE $createdPlan=$output; try { $patch = new Patch(); $value = new PayPalModel('{ "state":"ACTIVE" }'); $patch->setOp('replace') ->setPath('/') ->setValue($value); $patchRequest = new PatchRequest(); $patchRequest->addPatch($patch); $createdPlan->update($patchRequest, $apiContext); $plan = Plan::get($createdPlan->getId(), $apiContext); } catch (Exception $ex) { exit(1); } $createdPlan=$plan; //4 create agreement $agreement = new Agreement(); $agreement->setName('月額基本プラン') ->setDescription('テスト月額支払い基本コース。月額¥300-(税別)初月無料') ->setStartDate($startdate); // Add Plan ID $plan = new Plan(); $plan->setId($createdPlan->getId()); $agreement->setPlan($plan); // Add Payer $payer = new Payer(); $payer->setPaymentMethod('paypal'); $agreement->setPayer($payer); try { $agreement = $agreement->create($apiContext); $approvalUrl = $agreement->getApprovalLink(); } catch (Exception $ex) { exit(1); } // リダイレクト先のURLへ転送する header('Location: ' . $approvalUrl, true, 301); // すべての出力を終了 exit; |
確認
サンドボックスサイトで、購入者アカウントでは購入履歴が見られる。販売者アカウントでは販売履歴が見られる。
デベロッパーサイトでは下記のようにnotificationsがきていることを確認できる。
またwebhookの状態も確認できる。
webhookは保留などになっている場合は正常にスクリプトが叩かれていない。webレスポンスが200 OKであれば正常に叩かれたと表示される。
関連サイト
デベロッパーサイト
Sandbox

Send Money, Pay Online or Set Up a Merchant Account - PayPal
PayPal is the faster, safer way to send money, make an online payment, receive money or set up a merchant account.
githubサンプル。このまま使える。
paypal/PayPal-PHP-SDK
PHP SDK for PayPal RESTful APIs. Contribute to paypal/PayPal-PHP-SDK development by creating an account on GitHub.
PayPal 定期支払いの実装方法
おしまいっ
コメント