app/Plugin/PayPalCheckout42/Service/PayPalService.php line 233

Open in your IDE?
  1. <?php
  2. namespace Plugin\PayPalCheckout42\Service;
  3. use Eccube\Common\EccubeConfig;
  4. use Eccube\Entity\Cart;
  5. use Eccube\Entity\CartItem;
  6. use Eccube\Entity\Customer;
  7. use Eccube\Entity\Order;
  8. use Eccube\Entity\OrderItem;
  9. use Eccube\Entity\Shipping;
  10. use Exception;
  11. use Plugin\PayPalCheckout42\Contracts\CaptureTransactionResponse;
  12. use Plugin\PayPalCheckout42\Contracts\EccubeAddressAccessible;
  13. use Plugin\PayPalCheckout42\Contracts\OrderResultResponse;
  14. use Plugin\PayPalCheckout42\Contracts\ShowOrderDetailsResponse;
  15. use Plugin\PayPalCheckout42\Entity\Config;
  16. use Plugin\PayPalCheckout42\Entity\SubscribingCustomer;
  17. use Plugin\PayPalCheckout42\Entity\Transaction;
  18. use Plugin\PayPalCheckout42\Exception\NotFoundBillingTokenException;
  19. use Plugin\PayPalCheckout42\Exception\NotFoundOrderingIdException;
  20. use Plugin\PayPalCheckout42\Exception\NotFoundPurchaseProcessingOrderException;
  21. use Plugin\PayPalCheckout42\Exception\PayPalCaptureException;
  22. use Plugin\PayPalCheckout42\Exception\PayPalCheckoutException;
  23. use Plugin\PayPalCheckout42\Exception\PayPalRequestException;
  24. use Plugin\PayPalCheckout42\Exception\ReFoundPaymentException;
  25. use Plugin\PayPalCheckout42\Exception\ReFoundSubscriptionPaymentException;
  26. use Plugin\PayPalCheckout42\Repository\ConfigRepository;
  27. use Plugin\PayPalCheckout42\Repository\SubscribingCustomerRepository;
  28. use Plugin\PayPalCheckout42\Repository\TransactionRepository;
  29. use Plugin\PayPalCheckout42\Service\Method\BankTransfer;
  30. use stdClass;
  31. use Symfony\Component\HttpFoundation\Response;
  32. /**
  33.  * Class PayPalService
  34.  * @package Plugin\PayPalCheckout42\Service
  35.  */
  36. class PayPalService
  37. {
  38.     /**
  39.      * @var EccubeConfig
  40.      */
  41.     private $eccubeConfig;
  42.     /**
  43.      * @var PayPalOrderService
  44.      */
  45.     private $order;
  46.     /**
  47.      * @var PayPalRequestService
  48.      */
  49.     private $client;
  50.     /**
  51.      * @var PayPalAcdcService
  52.      */
  53.     private $payPalAcdcService;
  54.     /**
  55.      * @var Logger
  56.      */
  57.     private $logger;
  58.     /**
  59.      * @var TransactionRepository
  60.      */
  61.     private $transactionRepository;
  62.     /**
  63.      * @var SubscribingCustomerRepository
  64.      */
  65.     private $subscribingCustomerRepository;
  66.     /**
  67.      * @var Config
  68.      */
  69.     private $config;
  70.     /**
  71.      * PayPalService constructor.
  72.      * @param EccubeConfig $eccubeConfig
  73.      * @param PayPalOrderService $orderService
  74.      * @param PayPalRequestService $requestService
  75.      * @param PayPalAcdcService $payPalAcdcService
  76.      * @param LoggerService $loggerService
  77.      * @param TransactionRepository $transactionRepository
  78.      * @param SubscribingCustomerRepository $subscribingCustomerRepository
  79.      * @param ConfigRepository $configRepository
  80.      */
  81.     public function __construct(
  82.         EccubeConfig $eccubeConfig,
  83.         PayPalOrderService $orderService,
  84.         PayPalRequestService $requestService,
  85.         PayPalAcdcService $payPalAcdcService,
  86.         LoggerService $loggerService,
  87.         TransactionRepository $transactionRepository,
  88.         SubscribingCustomerRepository $subscribingCustomerRepository,
  89.         ConfigRepository $configRepository
  90.     ) {
  91.         $this->config $configRepository->get();
  92.         $this->eccubeConfig $eccubeConfig;
  93.         $this->order $orderService;
  94.         $this->client $requestService;
  95.         $clientId $this->config->getClientId();
  96.         $clientSecret $this->config->getClientSecret();
  97.         $this->client->setEnv($clientId$clientSecret$this->config->getUseSandbox());
  98.         $this->payPalAcdcService $payPalAcdcService;
  99.         $this->logger $loggerService;
  100.         $this->transactionRepository $transactionRepository;
  101.         $this->subscribingCustomerRepository $subscribingCustomerRepository;
  102.     }
  103.     /**
  104.      * @param Cart $cart
  105.      * @return bool
  106.      */
  107.     public static function existsSubscriptionProductInCart(Cart $cart): bool
  108.     {
  109.         /** @var CartItem $cartItem */
  110.         foreach ($cart->getCartItems() as $cartItem) {
  111.             if ($cartItem->getProductClass()->getUseSubscription()) {
  112.                 return true;
  113.             }
  114.         }
  115.         return false;
  116.     }
  117.     /**
  118.      * @param Order $order
  119.      * @return bool
  120.      */
  121.     public static function existsSubscriptionProductInOrderItems(Order $order): bool
  122.     {
  123.         /** @var OrderItem[] $items */
  124.         $items $order->getProductOrderItems();
  125.         /** @var OrderItem $item */
  126.         foreach ($items as $item) {
  127.             if ($item->getProductClass()->getUseSubscription()) {
  128.                 return true;
  129.             }
  130.         }
  131.         return false;
  132.     }
  133.     /**
  134.      * @param Cart $cart
  135.      * @return string
  136.      */
  137.     public static function getCartAmount(Cart $cart): string
  138.     {
  139.         $amount $cart->getTotalPrice();
  140.         return $amount;
  141.     }
  142.     /**
  143.      * @return bool
  144.      */
  145.     public function useExpressBtn(): bool
  146.     {
  147.         return $this->config->getUseExpressBtn() === true;
  148.     }
  149.     /**
  150.      * @return bool
  151.      */
  152.     public function isDebug(): bool
  153.     {
  154.         return $this->eccubeConfig->get('paypal.debug') ?? false;
  155.     }
  156.     /**
  157.      * @param string $id
  158.      */
  159.     public function saveShortcutPayPalCheckoutToken(string $id): void
  160.     {
  161.         $this->order->setShortcutPaymentSession($id);
  162.     }
  163.     /**
  164.      * @return Order
  165.      * @throws NotFoundPurchaseProcessingOrderException
  166.      */
  167.     public function getShippingOrder(): Order
  168.     {
  169.         return $this->order->getPurchaseProcessingOrder();
  170.     }
  171.     /**
  172.      * @return Customer
  173.      * @throws NotFoundOrderingIdException
  174.      * @throws Exception
  175.      */
  176.     public function getShippingCustomer(): Customer
  177.     {
  178.         /** @var OrderResultResponse|EccubeAddressAccessible|null $response */
  179.         $response null;
  180.         $this->order->doProcessOrderingId(function ($orderingId) use (&$response) {
  181.             $transaction $this->client->prepareOrderDetailTransaction($orderingId);
  182.             $response $this->client->orderDetailTransaction($transaction);
  183.         });
  184.         $this->order->setOrderingId($response->getOrderingId());
  185.         /** @var Customer $customer */
  186.         $customer $this->order->generateCustomer($response);
  187.         return $customer;
  188.     }
  189.     /**
  190.      * @param Order $order
  191.      * @param callable $afterProcessing
  192.      * @param bool $authenticatedUser
  193.      * @param string $vaultId
  194.      * @throws PayPalRequestException
  195.      * @throws PayPalCheckoutException
  196.      */
  197.     public function createOrderRequest(Order $order, callable $afterProcessing$authenticatedUser true$vaultId ''): void
  198.     {
  199.         /** @var stdClass $options */
  200.         $options = new stdClass();
  201.         $this->setApplicationContext($options$order$authenticatedUser);
  202.         $this->setItemAndTotalPrice($options$order);
  203.         $this->setCustomer($options$order);
  204.         $this->setShipping($options$order$authenticatedUser);
  205.         $transaction $this->client->prepareOrderTransaction($options$order->getPayment()->getMethodClass());
  206.         $this->logger->debug("CreateOrderRequest contains the following parameters"$transaction->body);
  207.         /** @var OrderResultResponse $response */
  208.         $response $this->client->orderTransaction($transaction);
  209.         $this->order->setOrderingId($response->getOrderingId());
  210.         // vault ID はここでは利用しないが、決済時に利用するのでセッションに保存しておく
  211.         if (!empty($vaultId)) {
  212.             $this->payPalAcdcService->setVaultIdToSession($vaultId);
  213.         }
  214.         try {
  215.             call_user_func($afterProcessing$response);
  216.         } catch (Exception $e) {
  217.             throw new PayPalCheckoutException($e->getMessage(), $e->getCode(), $e);
  218.         }
  219.     }
  220.     /**
  221.      * @param Order $order
  222.      * @param callable $afterProcessing
  223.      * @throws NotFoundOrderingIdException
  224.      * @throws PayPalCheckoutException
  225.      */
  226.     public function updateOrderRequest(Order $order, callable $afterProcessing): void
  227.     {
  228.         /** @var OrderResultResponse|null $response */
  229.         $response null;
  230.         /** @var stdClass $options */
  231.         $options = new stdClass();
  232.         $this->setItemAndTotalPrice($options$order);
  233.         unset($options->items);
  234.         $this->order->doProcessOrderingId(function ($orderingId) use (&$response$options) {
  235.             $transaction $this->client->prepareOrderPatchTransaction($orderingId$options);
  236.             $response  $this->client->orderPatchTransaction($transaction);
  237.         });
  238.         $this->order->setOrderingId($response->getOrderingId());
  239.         try {
  240.             call_user_func($afterProcessing$response);
  241.         } catch (Exception $e) {
  242.             throw new PayPalCheckoutException($e->getMessage(), $e->getCode(), $e);
  243.         }
  244.     }
  245.     /**
  246.      * @param Order $order
  247.      * @param callable $afterProcessing
  248.      * @throws PayPalRequestException
  249.      * @throws PayPalCheckoutException
  250.      */
  251.     public function createBillingAgreementTokenRequest(Order $order, callable $afterProcessing): void
  252.     {
  253.         /** @var Shipping $shipping */
  254.         $shipping $order->getShippings()->first();
  255.         /** @var stdClass $options */
  256.         $options = new stdClass();
  257.         $this->setShippingAddressSubscription($options$shipping);
  258.         $transaction $this->client->prepareBillingAgreementToken($options);
  259.         $this->logger->debug("CreateBillingAgreementToken contains the following parameters"$transaction->body);
  260.         $response $this->client->billingAgreementToken($transaction);
  261.         $this->order->setBillingToken($response->result->token_id);
  262.         try {
  263.             call_user_func($afterProcessing$response);
  264.         } catch (Exception $e) {
  265.             throw new PayPalCheckoutException($e->getMessage(), $e->getCode(), $e);
  266.         }
  267.     }
  268.     /**
  269.      * @param Order $order
  270.      * @param callable $afterProcessing
  271.      * @throws NotFoundOrderingIdException
  272.      * @throws ReFoundPaymentException
  273.      * @throws PayPalCaptureException
  274.      */
  275.     private function makeOneTimePayment(Order $order, callable $afterProcessing)
  276.     {
  277.         $response null;
  278.         $vaultId $this->payPalAcdcService->extractVaultIdFromSession();
  279.         $fraudNetSessionId $this->payPalAcdcService->extractFraudNetSessionIdentifierFromSession();
  280.         $this->order->doProcessOrderingId(function ($orderingId) use (&$response$vaultId$fraudNetSessionId) {
  281.             $transaction $this->client->prepareCaptureTransaction($orderingId$vaultId$fraudNetSessionId);
  282.             $this->logger->debug("CaptureTransaction contains the following parameters"$transaction->body ?? []);
  283.             /** @var CaptureTransactionResponse $response */
  284.             $response $this->client->captureTransaction($transaction);
  285.         });
  286.         /**
  287.          * 何らかの理由で、トランザクションが失敗した場合、EC-CUBEの決済処理を無効にするため、例外を送出する。
  288.          */
  289.         if (!isset($response) || $response->isNg()) {
  290.             throw new PayPalCaptureException($response->getDebugId());
  291.         }
  292.         try {
  293.             call_user_func($afterProcessing$response);
  294.         } catch (Exception $e) {
  295.             throw new ReFoundPaymentException($e->getMessage(), $e->getCode(), $e);
  296.         }
  297.     }
  298.     /**
  299.      * 3Dセキュア認証が有効な場合、認証成功したかを確認する
  300.      *
  301.      * @throws NotFoundOrderingIdException
  302.      */
  303.     public function verify3dsecure(): bool
  304.     {
  305.         $orderingId $this->order->getOrderingId();
  306.         $request $this->client->prepareShowOrderDetails($orderingId);
  307.         $this->logger->debug("ShowOrderDetails contains the following parameters"$request->body ?? []);
  308.         /** @var ShowOrderDetailsResponse $response */
  309.         $response $this->client->showOrderDetails($request);
  310.         return $response->isOk();
  311.     }
  312.     /**
  313.      * @param Order $order
  314.      * @throws NotFoundBillingTokenException
  315.      * @throws NotFoundOrderingIdException
  316.      * @throws PayPalRequestException
  317.      * @throws ReFoundPaymentException
  318.      * @throws ReFoundSubscriptionPaymentException
  319.      */
  320.     public function checkout(Order $order): void
  321.     {
  322.         if (self::existsSubscriptionProductInOrderItems($order)) {
  323.             $this->subscriptionPayment($order);
  324.         } else {
  325.             $this->payment($order);
  326.         }
  327.     }
  328.     /**
  329.      * @param Order $Order
  330.      * @param SubscribingCustomer $subscribingCustomer
  331.      * @param callable $afterProcessing
  332.      * @throws PayPalRequestException
  333.      * @throws ReFoundSubscriptionPaymentException
  334.      */
  335.     public function subscription(Order $OrderSubscribingCustomer $subscribingCustomer, callable $afterProcessing): void
  336.     {
  337.         /** @var Shipping $Shipping */
  338.         $Shipping $Order->getShippings()->first();
  339.         /** @var stdClass $options */
  340.         $options = new stdClass();
  341.         $options->invoice_number $Order->getId();
  342.         $this->setItemAndTotalPriceSubscription($options$Order);
  343.         $this->setShippingAddressSubscription($options$Shipping);
  344.         /** @var string $billingAgreementId */
  345.         $billingAgreementId $subscribingCustomer->getReferenceTransaction()->getBillingAgreementId();
  346.         $transaction $this->client->prepareReferenceTransaction($billingAgreementId$options);
  347.         $this->logger->debug("CreateBillingAgreementPayment contains the following parameters"$transaction->body);
  348.         $response $this->client->referenceTransactionPayment($transaction);
  349.         try {
  350.             /** @var Transaction $transaction */
  351.             $transaction $this->transactionRepository->saveSuccessfulTransaction($Order$response);
  352.             call_user_func($afterProcessing$response$transaction);
  353.         } catch (Exception $e) {
  354.             throw new ReFoundSubscriptionPaymentException($e->getMessage(), $e->getCode(), $e);
  355.         }
  356.     }
  357.     /**
  358.      * @param Transaction $transaction
  359.      * @param callable $afterProcessing
  360.      * @throws PayPalRequestException
  361.      * @throws PayPalCheckoutException
  362.      */
  363.     public function refound(Transaction $transaction, callable $afterProcessing): void
  364.     {
  365.         $transaction $this->client->prepareRefoundTransaction($transaction->getCaptureId());
  366.         $response $this->client->refoundTransaction($transaction);
  367.         try {
  368.             $transaction $this->transactionRepository->saveSuccessfulTransaction($this->order$response);
  369.             call_user_func($afterProcessing$response);
  370.         } catch (Exception $e) {
  371.             if ($e->getCode() === Response::HTTP_UNPROCESSABLE_ENTITY) {
  372.                 throw new PayPalCheckoutException($e->getMessage(), $e->getCode(), $e);
  373.             } else {
  374.                 throw new PayPalCheckoutException($e->getMessage(), $e->getCode(), $e);
  375.             }
  376.         }
  377.     }
  378.     /**
  379.      * @param Order $order
  380.      * @param callable $afterProcessing
  381.      * @throws NotFoundBillingTokenException
  382.      * @throws PayPalRequestException
  383.      * @throws ReFoundSubscriptionPaymentException
  384.      */
  385.     private function makeFirstSubscriptionPayment(Order $order, callable $afterProcessing): void
  386.     {
  387.         /** @var string|null $billingAgreementId */
  388.         $billingAgreementId null;
  389.         $this->order->doProcessBillingToken(function ($billingToken) use (&$billingAgreementId) {
  390.             $transaction $this->client->prepareBillingAgreement($billingToken);
  391.             $response $this->client->billingAgreement($transaction);
  392.             $billingAgreementId $response->result->id;
  393.         });
  394.         /** @var Shipping $Shipping */
  395.         $Shipping $order->getShippings()->first();
  396.         /** @var stdClass $options */
  397.         $options = new stdClass();
  398.         $options->invoice_number $order->getPreOrderId();
  399.         $this->setItemAndTotalPriceSubscription($options$order);
  400.         $this->setShippingAddressSubscription($options$Shipping);
  401.         $transaction $this->client->prepareReferenceTransaction($billingAgreementId$options);
  402.         $this->logger->debug("CreateBillingAgreementPayment contains the following parameters"$transaction->body);
  403.         $response $this->client->referenceTransactionPayment($transaction);
  404.         try {
  405.             call_user_func($afterProcessing$response);
  406.         } catch (Exception $e) {
  407.             throw new ReFoundSubscriptionPaymentException($e->getMessage(), $e->getCode(), $e);
  408.         }
  409.     }
  410.     /**
  411.      * @param Order $order
  412.      * @throws NotFoundOrderingIdException
  413.      * @throws ReFoundPaymentException
  414.      */
  415.     private function payment(Order $order): void
  416.     {
  417.         try {
  418.             $this->makeOneTimePayment($order, function ($response) use ($order) {
  419.                 $this->transactionRepository->saveSuccessfulTransaction($order$response);
  420.             });
  421.         } catch (ReFoundPaymentException $e) {
  422.             throw $e;
  423.         }
  424.     }
  425.     /**
  426.      * @param Order $order
  427.      * @throws NotFoundBillingTokenException
  428.      * @throws PayPalRequestException
  429.      * @throws ReFoundSubscriptionPaymentException
  430.      */
  431.     private function subscriptionPayment(Order $order): void
  432.     {
  433.         try {
  434.             $this->makeFirstSubscriptionPayment($order, function ($response) use ($order) {
  435.                 /** @var Transaction $referenceTransaction */
  436.                 $referenceTransaction $this->transactionRepository->saveSuccessfulTransaction($order$response);
  437.                 $this->subscribingCustomerRepository->agreement($referenceTransaction);
  438.             });
  439.         } catch (ReFoundSubscriptionPaymentException $e) {
  440.             throw $e;
  441.         }
  442.     }
  443.     /**
  444.      * 商品、価格情報を付与する
  445.      * PayPal管理ツールの税表示がわかりにくいため、現在は税込価格をセットするようにしている。
  446.      *
  447.      * @param stdClass $options
  448.      * @param Order $order
  449.      */
  450.     private function setItemAndTotalPrice(stdClass &$optionsOrder $order): void
  451.     {
  452.         /** @var array $products */
  453.         $products array_map(function (OrderItem $item): array {
  454.             return [
  455.                 'name' => $item->getProductName(),
  456.                 'description' => $item->getProductName(),
  457.                 'sku' => $item->getProductCode(),
  458.                 'unit_amount' => [
  459.                     'currency_code' => $item->getCurrencyCode(),
  460.                     // 税込価格を送る
  461.                     'value' => $item->getPriceIncTax(),
  462.                     //'value' => $this->client->roundedCurrencyFormat($item->getPrice()),
  463.                 ],
  464.                 //'tax' => [
  465.                 //    'currency_code' => $item->getCurrencyCode(),
  466.                 //    'value' => $this->client->roundedCurrencyFormat($item->getTax())
  467.                 //],
  468.                 'quantity' => $item->getQuantity(),
  469.             ];
  470.         }, $order->getProductOrderItems());
  471.         $options->amount = [
  472.             'currency_code' => 'JPY',
  473.             'value' => $this->client->roundedCurrencyFormat($order->getTotal()),
  474.             'breakdown' => [
  475.                 'item_total' => [
  476.                     'currency_code' => 'JPY',
  477.                     'value' => $this->client->roundedCurrencyFormat(array_reduce($products, function ($carry, array $product): int {
  478.                         // unit_amountだが現在は税込価格が入っているため、税込価格での計算がされる
  479.                         $carry += $product['unit_amount']['value'] * $product['quantity'];
  480.                         return $carry;
  481.                     })),
  482.                 ],
  483.                 'shipping' => [
  484.                     'currency_code' => 'JPY',
  485.                     'value' => $this->client->roundedCurrencyFormat($order->getDeliveryFeeTotal()),
  486.                 ],
  487.                 'discount' => [
  488.                     'currency_code' => 'JPY',
  489.                     'value' => $this->client->roundedCurrencyFormat($order->getDiscount()),
  490.                 ],
  491.                 // 税を送らない
  492.                 //'tax_total' => [
  493.                 //    'currency_code' => 'JPY',
  494.                 //    'value' => $this->client->roundedCurrencyFormat(array_reduce($products, function ($carry, array $product): int {
  495.                 //        $carry += $product['tax']['value'] * $product['quantity'];
  496.                 //        return $carry;
  497.                 //    }))
  498.                 //]
  499.             ],
  500.         ];
  501.         $options->items $products;
  502.     }
  503.     /**
  504.      * 会員情報を付与する
  505.      *
  506.      * @param stdClass $options
  507.      * @param Order $Order
  508.      */
  509.     private function setCustomer(stdClass &$optionsOrder $Order): void
  510.     {
  511.         $options->customer = [
  512.             'name01' => $Order->getName01(),
  513.             'name02' => $Order->getName02(),
  514.             'email' => $Order->getEmail(),
  515.             'phone_number' => $Order->getPhoneNumber(),
  516.             'address' => [
  517.                 'address_line_1' => $Order->getAddr02(),
  518.                 'address_line_2' => null,
  519.                 'admin_area_2' => $Order->getAddr01(),
  520.                 'admin_area_1' => $Order->getPref()->getName(),
  521.                 'postal_code' => $Order->getPostalCode(),
  522.                 'country_code' => 'JP'
  523.             ]
  524.         ];
  525.     }
  526.     /**
  527.      * 商品、価格情報を付与する
  528.      * PayPal管理ツールの税表示がわかりにくいため、現在は税込価格をセットするようにしている。
  529.      *
  530.      * @param stdClass $options
  531.      * @param Order $Order
  532.      */
  533.     private function setItemAndTotalPriceSubscription(stdClass &$optionsOrder $Order): void
  534.     {
  535.         /** @var array $products */
  536.         $products array_map(function (OrderItem $item): array {
  537.             return [
  538.                 'sku' => $item->getProductCode(),
  539.                 'name' => $item->getProductName(),
  540.                 'description' => $item->getProductName(),
  541.                 'quantity' => $item->getQuantity(),
  542.                 // 税込価格を送る
  543.                 'price' => $item->getPriceIncTax(),
  544.                 // 'price' => $this->client->roundedCurrencyFormat($item->getPrice()),
  545.                 'currency' => "JPY",
  546.                 'tax' => $this->client->roundedCurrencyFormat($item->getTax()),
  547.             ];
  548.         }, $Order->getProductOrderItems());
  549.         $options->amount = [
  550.             "total" => $this->client->roundedCurrencyFormat($Order->getTotal()),
  551.             "currency" => "JPY",
  552.             "details" => [
  553.                 "subtotal" => $this->client->roundedCurrencyFormat(array_reduce($products, function ($carry, array $product): int {
  554.                     $carry += $product['price'] * $product['quantity'];
  555.                     return $carry;
  556.                 })),
  557.                 // 税を送らない
  558.                 //"tax" => $this->client->roundedCurrencyFormat(array_reduce($products, function ($carry, array $product): int {
  559.                 //    $carry += $product['tax'] * $product['quantity'];
  560.                 //    return $carry;
  561.                 //})),
  562.                 "shipping" => $this->client->roundedCurrencyFormat($Order->getDeliveryFeeTotal())
  563.             ]
  564.         ];
  565.         $options->items $products;
  566.     }
  567.     /**
  568.      * @param stdClass $options
  569.      * @param Order $order
  570.      * @param $authenticatedUser
  571.      */
  572.     private function setApplicationContext(stdClass &$optionsOrder $order$authenticatedUser): void
  573.     {
  574.         /** @var string $paymentMethod */
  575.         $paymentMethod $order->getPayment()->getMethodClass();
  576.         $options->application_context = [
  577.             'landing_page' => $paymentMethod === BankTransfer::class ? 'BILLING' 'LOGIN'
  578.         ];
  579.         if ($authenticatedUser) {
  580.             $options->application_context['shipping_preference'] = 'SET_PROVIDED_ADDRESS';
  581.         }
  582.     }
  583.     /**
  584.      * @param stdClass $options
  585.      * @param $authenticatedUser
  586.      * @param Order $Order
  587.      */
  588.     private function setShipping(stdClass &$optionsOrder $Order$authenticatedUser): void
  589.     {
  590.         if ($authenticatedUser) {
  591.             /** @var Shipping $Shipping */
  592.             $Shipping $Order->getShippings()->first();
  593.             $options->shipping = [
  594.                 'name' => [
  595.                     'full_name' => "{$Shipping->getName01()} {$Shipping->getName02()}",
  596.                 ],
  597. //                "phone_number" => $Shipping->getPhoneNumber(),
  598. //                "email" => $Order->getEmail(),
  599.                 'address' => [
  600.                     'address_line_1' => $Shipping->getAddr02(),
  601.                     'address_line_2' => null,
  602.                     'admin_area_2' => $Shipping->getAddr01(),
  603.                     'admin_area_1' => $Shipping->getPref()->getName(),
  604.                     'postal_code' => $Shipping->getPostalCode(),
  605.                     'country_code' => 'JP'
  606.                 ]
  607.             ];
  608.         }
  609.     }
  610.     /**
  611.      * @param Shipping $Shipping
  612.      * @param stdClass $options
  613.      */
  614.     private function setShippingAddressSubscription(stdClass &$optionsShipping $Shipping): void
  615.     {
  616.         $options->shipping_address = [
  617.             'recipient_name' => $Shipping->getFullName(),
  618.             'line1' => $Shipping->getAddr02(),
  619.             "line2" => null,
  620.             'city' => $Shipping->getAddr01(),
  621.             'state' => 'JP',
  622.             "phone" => $Shipping->getPhoneNumber(),
  623.             'postal_code' => $Shipping->getPostalCode(),
  624.             'country_code' => 'JP'
  625.         ];
  626.     }
  627. }