Skip to content

Commit 5a638fd

Browse files
committed
added sharing feature to Parsian Adapter
1 parent 0444d0d commit 5a638fd

File tree

4 files changed

+82
-4
lines changed

4 files changed

+82
-4
lines changed

database/migrations/create_larapay_transaction_table.php.stub

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class CreateLarapayTransactionTable extends Migration
4141
$table->bigInteger('amount')->default(0);
4242
$table->jsonb('extra_params')->nullable()->default('{}');
4343
$table->jsonb('additional_data')->nullable()->default('{}');
44+
$table->jsonb('sharing')->nullable()->default('{}');
4445

4546
$table->dateTime('paid_at')->nullable();
4647
$table->timestamps();

src/Adapter/Parsian.php

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,40 @@ protected function requestToken()
5555
throw new Exception('larapay::larapay.could_not_request_payment');
5656
}
5757

58+
5859
$this->checkRequiredParameters([
5960
'pin',
6061
'order_id',
6162
'amount',
6263
'redirect_url',
6364
]);
6465

66+
6567
$sendParams = [
6668
'LoginAccount' => $this->pin,
6769
'Amount' => intval($this->amount),
6870
'OrderId' => intval($this->order_id),
6971
'CallBackUrl' => $this->redirect_url,
70-
'AdditionalData' => $this->additional_data ? $this->additional_data : '',
72+
'AdditionalData' => $this->additional_data ?? '',
73+
'Originator' => $this->originator ?? '',
7174
];
7275

76+
77+
if (is_array($this->sharing) && !empty($this->sharing)) {
78+
return $this->requestTokenWithoutSharing($sendParams);
79+
} else {
80+
return $this->requestTokenWithSharing($sendParams);
81+
}
82+
}
83+
84+
/**
85+
* @param array $sendParams
86+
*
87+
* @return mixed
88+
* @throws Exception
89+
*/
90+
private function requestTokenWithoutSharing($sendParams)
91+
{
7392
try {
7493
$this->requestType = 'request';
7594
$soapClient = $this->getSoapClient();
@@ -96,6 +115,60 @@ protected function requestToken()
96115
}
97116
}
98117

118+
/**
119+
* @param array $sendParams
120+
*
121+
* @return mixed
122+
* @throws Exception
123+
*/
124+
private function requestTokenWithSharing($sendParams)
125+
{
126+
foreach ($this->sharing as $item) {
127+
if (isset($item->IBAN)) {
128+
// dynamic sharing
129+
$method = 'MultiplexedSaleWithIBANPaymentRequest';
130+
$respo = 'MultiplexedSaleWithIBANPaymentResult';
131+
$sendParams['MultiplexedAccounts']['Account'][] = [
132+
'Amount' => $item->share,
133+
'PayId' => $item->pay_id ?? '',
134+
'IBAN' => $item->iban
135+
];
136+
} else {
137+
// fix sharing
138+
$method = 'MultiplexedSalePaymentRequest';
139+
$respo = 'MultiplexedSalePaymentResult';
140+
$sendParams['MultiplexedAccounts']['Account'][] = [
141+
'Amount' => $item->share,
142+
'PayId' => $item->pay_id ?? '',
143+
];
144+
}
145+
}
146+
147+
try {
148+
$this->requestType = 'request';
149+
$soapClient = $this->getSoapClient();
150+
151+
XLog::debug("{$method} call", $sendParams);
152+
153+
$response = $soapClient->$method(array("requestData" => $sendParams));
154+
155+
XLog::debug("{$method} response", $this->obj2array($response));
156+
157+
if (isset($response->$respo->Status, $response->$respo->Token)) {
158+
if ($response->$respo->Status == 0) {
159+
$this->getTransaction()->setGatewayToken(strval($response->$respo->Token)); // update transaction reference id
160+
161+
return $response->$respo->Token;
162+
} else {
163+
throw new Exception($this->$respo->Status);
164+
}
165+
} else {
166+
throw new Exception('larapay::parsian.errors.invalid_response');
167+
}
168+
} catch (SoapFault $e) {
169+
throw new Exception('SoapFault: ' . $e->getMessage() . ' #' . $e->getCode(), $e->getCode());
170+
}
171+
}
99172
/**
100173
* @return mixed
101174
* @throws Exception

src/Models/LarapayTransaction.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class LarapayTransaction extends Model implements TransactionInterface
3737
'extra_params',
3838
'model',
3939
'additional_data',
40+
'sharing',
4041
];
4142

4243
protected $dates = [
@@ -92,7 +93,6 @@ public function reverseTransaction()
9293

9394
public function generateForm($autoSubmit = false, $callback = null, $adapterConfig = [])
9495
{
95-
9696
$paymentGatewayHandler = $this->gatewayHandler($adapterConfig);
9797

9898
$callbackRoute = route(config("larapay.payment_callback"), [
@@ -111,12 +111,13 @@ public function generateForm($autoSubmit = false, $callback = null, $adapterConf
111111
'order_id' => $this->getBankOrderId(),
112112
'redirect_url' => $callbackRoute,
113113
'amount' => $this->amount,
114+
'sharing' => json_decode($this->sharing, true),
114115
'submit_label' => trans('larapay::larapay.goto_gate'),
115116
];
116117

117118
try {
118119
if ($autoSubmit) {
119-
$paymentGatewayHandler->setParameters(['auto_submit' => true]);
120+
$paymentParams['auto_submit'] = true;
120121
}
121122

122123
$form = $paymentGatewayHandler->form($paymentParams);
@@ -150,6 +151,7 @@ public function formParams($callback = null, $adapterConfig = []): array
150151
'order_id' => $this->getBankOrderId(),
151152
'redirect_url' => $callbackRoute,
152153
'amount' => $this->amount,
154+
'sharing' => json_decode($this->sharing, true),
153155
'submit_label' => trans('larapay::larapay.goto_gate'),
154156
];
155157

src/Payable.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ public function createTransaction(
4545
$paymentGateway,
4646
$amount = null,
4747
$description = null,
48-
array $additionalData = []
48+
array $additionalData = [],
49+
array $sharing = []
4950
) {
5051

5152
$transactionData = [];
@@ -67,6 +68,7 @@ public function createTransaction(
6768
$transactionData['bank_order_id'] = $this->generateBankOrderId($paymentGateway);
6869
$transactionData['payment_method'] = 'ONLINE';
6970
$transactionData['additional_data'] = json_encode($additionalData, JSON_UNESCAPED_UNICODE);
71+
$transactionData['sharing'] = json_encode($sharing, JSON_UNESCAPED_UNICODE);
7072

7173
return $this->transactions()->create($transactionData);
7274
}

0 commit comments

Comments
 (0)