/** * @param array $parameter * @return Charge|ArrayAccess */ public function createCharge(array $parameter) { try { return Charge::create($parameter); } catch (Authentication $e) { return $this->returnError($e); } catch (InvalidRequest $e) { return $this->returnError($e); } }
* 下記 $aurhorize_url へのアクセスがAuthorization Requestとなります。 * URLパラメータの詳細については https://pay.jp/docs/payjp-oauth-api の「OAuth API利用の流れ」の章をご参照ください。 */ $_SESSION['state'] = md5(uniqid()); $authorization_request_params = array('response_type' => 'code', 'client_id' => CLIENT_ID, 'scope' => 'accounts cards addresses', 'state' => $_SESSION['state']); $aurhorize_url = AUTHORIZATON_ENDPOINT . '?' . http_build_query($authorization_request_params); } /* * フォームがPOSTされたらサーバーサイドで決済処理を行う */ if ($_SERVER['REQUEST_METHOD'] === 'POST') { $token = $_POST['payjp-token']; $amount = $_POST['amount']; \Payjp\Payjp::setApiKey(APIKEY_SECRET); try { $charge = \Payjp\Charge::create(['card' => $token, 'amount' => $amount, 'currency' => 'jpy']); echo "決済が完了しました。<br>Charge ID = {$charge->id}\n"; error_log(var_export($charge, true)); } catch (\Payjp\Error\InvalidRequest $e) { echo "トークンは既に使用済みです。\n"; } exit; } ?> <!doctype html> <html> <head> <meta charset="utf-8"> <meta content="width=device-width,initial-scale=1.0" name="viewport"> <link rel="stylesheet" href="//oss.maxcdn.com/semantic-ui/2.0.7/semantic.min.css" type="text/css" media="screen" charset="utf-8">
function before_process() { global $messageStack, $order, $currencies; try { if (is_readable(DIR_WS_MODULES . 'payment/vendor/autoload.php')) { require_once DIR_WS_MODULES . 'payment/vendor/autoload.php'; } else { if (is_readable(DIR_WS_MODULES . 'payment/payjp-php/init.php')) { require_once DIR_WS_MODULES . 'payment/payjp-php/init.php'; } else { // PayJPのPHPライブラリがない $messageStack->add_session('checkout_payment', MODULE_PAYMENT_WEBPAYJP_ERROR_LIBRARY_NOT_FOUND, 'error'); zen_redirect(zen_href_link(FILENAME_CHECKOUT_PAYMENT, '', 'SSL', true, false)); } } \Payjp\Payjp::setApiKey(MODULE_PAYMENT_PAYJP_API_SECRETKEY); // トークンを使って顧客オブジェクトを作っておく $customer = \Payjp\Customer::create(array('card' => $_SESSION['payjp_token'], "email" => $order->customer['email_address'])); // トークン決済 $charge_opt = array('amount' => $currencies->value($order->info['total']), 'currency' => 'jpy', 'customer' => $customer->id, "description" => $order->products[0]['model'] . (count($order->products) >= 2 ? MODULE_PAYMENT_PAYJP_TEXT_CHARGE_MULTI_PRODUCT_ETC : '')); if (defined('MODULE_PAYMENT_PAYJP_CAPTURE') and MODULE_PAYMENT_PAYJP_CAPTURE == 'False') { $charge_opt['capture'] = MODULE_PAYMENT_PAYJP_CAPTURE; } if (defined('MODULE_PAYMENT_PAYJP_CAPTURE') and MODULE_PAYMENT_PAYJP_CAPTURE == 'False' and defined('MODULE_PAYMENT_PAYJP_EXPIRY_DAYS') and MODULE_PAYMENT_PAYJP_EXPIRY_DAYS != '') { $charge_opt['expiry_days'] = MODULE_PAYMENT_PAYJP_EXPIRY_DAYS; } $charge = \Payjp\Charge::create($charge_opt); $_SESSION['payment_payjp'][] = array('type' => 'CHARGE', 'customer_id' => $customer->id, 'id' => $charge->id, 'fingerprint' => $charge->card->fingerprint); } catch (\Payjp\ErrorResponse\ErrorResponseException $e) { $error = $e->data->error; switch ($error->type) { case 'card_error': // カードに関するエラー $messageStack->add_session('checkout_payment', MODULE_PAYMENT_PAYJP_API_ERROR_CARD, 'error'); break; case 'client_error': // リクエストエラー $messageStack->add_session('checkout_payment', MODULE_PAYMENT_PAYJP_API_ERROR_CLIENT, 'error'); break; case 'sever_error': // PAY.JPや決済ネットワーク側のエラー $messageStack->add_session('checkout_payment', MODULE_PAYMENT_PAYJP_API_ERROR_SERVER, 'error'); break; default: // 未知のエラー $messageStack->add_session('checkout_payment', MODULE_PAYMENT_PAYJP_API_ERROR_UNKNOWN, 'error'); break; } zen_redirect(zen_href_link(FILENAME_CHECKOUT_PAYMENT, '', 'SSL', true, false)); } catch (\Payjp\ApiException $e) { // APIからのレスポンスが受け取れない場合。接続エラーなど $messageStack->add_session('checkout_payment', MODULE_PAYMENT_PAYJP_API_ERROR_CONNECT_FAILD, 'error'); zen_redirect(zen_href_link(FILENAME_CHECKOUT_PAYMENT, '', 'SSL', true, false)); } catch (\Exception $e) { // PAY.JP とは関係ない例外の場合 $messageStack->add_session('checkout_payment', MODULE_PAYMENT_PAYJP_API_ERROR_INVALID, 'error'); zen_redirect(zen_href_link(FILENAME_CHECKOUT_PAYMENT, '', 'SSL', true, false)); } return false; }