api.so-manager-dev.com/app/Services/Wellnet/WellnetSoapService.php
Your Name f139a3f608
All checks were successful
Deploy api / deploy (push) Successful in 22s
支払いAPI実装
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-23 20:02:25 +09:00

219 lines
7.0 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Services\Wellnet;
use App\Exceptions\WellnetSoapException;
use SoapClient;
use SoapHeader;
use RuntimeException;
/**
* Wellnet SOAP通信サービスYoyakuSyunoBarCode
*/
class WellnetSoapService
{
private string $wsdlPath;
private string $endpoint;
private string $namespace;
private string $encoding;
private string $userId;
private string $password;
public function __construct()
{
$this->wsdlPath = config('wellnet.soap.wsdl_path');
$this->endpoint = config('wellnet.soap.endpoint');
$this->namespace = config('wellnet.soap.namespace');
$this->encoding = config('wellnet.soap.encoding');
$this->userId = config('wellnet.soap.user_id');
$this->password = config('wellnet.soap.password');
}
/**
* 受付データ登録SyunoOpCode='I'
*
* @param array $inData 送信データ
* @return array レスポンスデータ
* @throws RuntimeException Wellnetエラー時
*/
public function register(array $inData): array
{
$inData['SyunoOpCode'] = 'I';
return $this->execute($inData);
}
/**
* 受付データ更新SyunoOpCode='U'
*
* @param array $inData 送信データ
* @return array レスポンスデータ
* @throws RuntimeException Wellnetエラー時
*/
public function update(array $inData): array
{
$inData['SyunoOpCode'] = 'U';
return $this->execute($inData);
}
/**
* 受付データ取消SyunoOpCode='D'
*
* @param string $syunoRecvNum 受付番号
* @return array レスポンスデータ
* @throws RuntimeException Wellnetエラー時
*/
public function cancel(string $syunoRecvNum): array
{
$inData = [
'SyunoRecvNum' => $syunoRecvNum,
'SyunoOpCode' => 'D',
];
return $this->execute($inData);
}
/**
* SOAP通信実行
*
* @param array $inData 送信データ
* @return array レスポンスデータ
* @throws RuntimeException 通信エラー・Wellnetエラー時
*/
private function execute(array $inData): array
{
if (!file_exists($this->wsdlPath)) {
throw new RuntimeException('WSDLファイルが見つかりません: ' . $this->wsdlPath);
}
// 固定値の設定
$inData['DataSyubetsu'] = config('wellnet.payment.data_syubetsu');
if (empty($inData['SyunoPayCode'])) {
$inData['SyunoPayCode'] = config('wellnet.payment.pay_code');
}
if (empty($inData['SyunoCorpCode'])) {
$inData['SyunoCorpCode'] = config('wellnet.payment.corp_code');
}
if (empty($inData['BcJigyosyaNo'])) {
$inData['BcJigyosyaNo'] = config('wellnet.payment.jigyosya_no');
}
if (empty($inData['BcAnkenNo'])) {
$inData['BcAnkenNo'] = config('wellnet.payment.anken_no');
}
if (empty($inData['BcNinsyoKey'])) {
$inData['BcNinsyoKey'] = config('wellnet.payment.ninsyo_key');
}
if (empty($inData['SyunoServiceKey'])) {
$inData['SyunoServiceKey'] = config('wellnet.payment.service_key');
}
// 日本語文字列をShift_JISに変換
$inData = $this->convertToSjis($inData);
// SyunoFreeArray の構築
if (isset($inData['SyunoFreeArray']) && is_array($inData['SyunoFreeArray'])) {
$freeArray = [];
foreach ($inData['SyunoFreeArray'] as $index => $value) {
$freeArray[] = ['Index' => $index, 'SyunoFreeStr' => $value];
}
$inData['SyunoFreeArray'] = $freeArray;
}
try {
$client = new SoapClient($this->wsdlPath, [
'encoding' => $this->encoding,
'trace' => true,
'exceptions' => true,
'cache_wsdl' => WSDL_CACHE_NONE,
'connection_timeout' => 30,
'location' => $this->endpoint,
'stream_context' => stream_context_create([
'ssl' => [
'verify_peer' => true,
'verify_peer_name' => true,
]
]),
]);
// SOAP認証ヘッダ設定
$header = new SoapHeader($this->namespace, 'WellnetSoapHeader', [
'UserId' => $this->userId,
'Password' => $this->password,
], true);
$client->__setSoapHeaders($header);
// SOAP呼出
$params = ['inData' => $inData];
$response = $client->YoyakuSyunoBarCode($params);
// レスポンス解析
$result = $this->parseResponse($response);
// 結果チェック
if ($result['Result'] !== '0000') {
throw new WellnetSoapException(
$result['Result'],
'Wellnet SOAPエラー: Result=' . $result['Result']
);
}
return $result;
} catch (\SoapFault $e) {
throw new RuntimeException('SOAP通信エラー: ' . $e->getMessage(), 0, $e);
}
}
/**
* SOAPレスポンス解析
*
* @param object $response SOAPレスポンスオブジェクト
* @return array 解析済みデータ
*/
private function parseResponse(object $response): array
{
$result = $response->YoyakuSyunoBarCodeResult;
return [
'Result' => $result->Result ?? '',
'DataSyubetsu' => $result->DataSyubetsu ?? '',
'KKessaiNo' => $result->KKessaiNo ?? '',
'FreeArea' => $result->FreeArea ?? '',
'SyunoPayCode' => $result->SyunoPayCode ?? '',
'SyunoRecvNum' => $result->SyunoRecvNum ?? '',
'BcJigyosyaNo' => $result->BcJigyosyaNo ?? '',
'BcAnkenNo' => $result->BcAnkenNo ?? '',
'BcNinsyoKey' => $result->BcNinsyoKey ?? '',
'SyunoMMSNo' => $result->SyunoMMSNo ?? '',
];
}
/**
* 文字列値をShift_JISに変換
*
* @param array $data 変換対象データ
* @return array 変換済みデータ
*/
private function convertToSjis(array $data): array
{
// 変換対象フィールド(日本語が含まれる可能性のあるフィールド)
$targetFields = ['SyunoNameKanji', 'SyunoNameKana'];
foreach ($targetFields as $field) {
if (isset($data[$field]) && $data[$field] !== '') {
$data[$field] = mb_convert_encoding($data[$field], 'SJIS', 'UTF-8');
}
}
// SyunoFreeArray内の値も変換
if (isset($data['SyunoFreeArray']) && is_array($data['SyunoFreeArray'])) {
foreach ($data['SyunoFreeArray'] as $index => $value) {
if (is_string($value) && $value !== '' && $value !== '*') {
$data['SyunoFreeArray'][$index] = mb_convert_encoding($value, 'SJIS', 'UTF-8');
}
}
}
return $data;
}
}