Skip to content

Commit e6cb57b

Browse files
committed
support recurring payment type=RECURRING
1 parent 7f1f619 commit e6cb57b

File tree

4 files changed

+87
-4
lines changed

4 files changed

+87
-4
lines changed

config/hyperpay.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,6 @@
2828
* }
2929
*/
3030
'transaction_model' => 'Devinweb\LaravelHyperpay\Models\Transaction',
31-
'notificationUrl' => '/hyperpay/webhook',
31+
32+
'notificationUrl' => '/hyperpay/webhook'
3233
];

src/LaravelHyperpay.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ class LaravelHyperpay implements Hyperpay
4747
*/
4848
protected $gateway_url = 'https://test.oppwa.com';
4949

50+
51+
/**
52+
* @var bool demand to register the user card
53+
*/
54+
protected $register_user_card = false;
55+
5056
/**
5157
* Create a new manager instance.
5258
*
@@ -140,7 +146,7 @@ protected function prepareCheckout(Model $user, array $trackable_data, $request)
140146
$this->config['merchantTransactionId'] = $this->token;
141147
$this->config['userAgent'] = $request->server('HTTP_USER_AGENT');
142148
$result = (new HttpClient($this->client, $this->gateway_url.'/v1/checkouts', $this->config))->post(
143-
$parameters = (new HttpParameters())->postParams(Arr::get($trackable_data, 'amount'), $user, $this->config, $this->billing)
149+
$parameters = (new HttpParameters())->postParams(Arr::get($trackable_data, 'amount'), $user, $this->config, $this->billing, $this->register_user_card)
144150
);
145151

146152
$response = (new HttpResponse($result, null, $parameters))
@@ -174,6 +180,20 @@ public function paymentStatus(string $resourcePath, string $checkout_id)
174180
return $response;
175181
}
176182

183+
/**
184+
*
185+
*/
186+
public function recurringPayment(string $registration_id, $amount, $checkout_id)
187+
{
188+
$result = (new HttpClient($this->client, $this->gateway_url.'/v1/registrations/'.$registration_id.'/payments', $this->config))->post(
189+
(new HttpParameters())->postRecurringPayment($amount, $this->redirect_url, $checkout_id),
190+
);
191+
192+
$response = (new HttpResponse($result, null, []))->recurringPayment();
193+
194+
return $response;
195+
}
196+
177197
/**
178198
* Add merchantTransactionId.
179199
*
@@ -200,6 +220,18 @@ public function addRedirectUrl($url)
200220
return $this;
201221
}
202222

223+
/**
224+
* Set the register user card information, to use it when we prepare the checkout
225+
*
226+
* @return $this
227+
*/
228+
public function registerUserCard()
229+
{
230+
$this->register_user_card = true;
231+
232+
return $this;
233+
}
234+
203235
/**
204236
* Generate the token that used as merchantTransactionId to generate the payment form.
205237
*

src/Support/HttpParameters.php

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ class HttpParameters
1717
* @param \Devinweb\LaravelHyperpay\Contracts\BillingInterface $billing
1818
* @return array
1919
*/
20-
public function postParams($amount, $user, $heyPerPayConfig, $billing): array
20+
public function postParams($amount, $user, $heyPerPayConfig, $billing, $register_user): array
2121
{
2222
$body = $this->getBodyParameters($amount, $user, $heyPerPayConfig);
23-
23+
if ($register_user) {
24+
$body = array_merge($body, $this->registerPaymentData());
25+
}
2426
$billing_parameters = $this->getBillingParameters($billing);
2527

2628
$parameters = array_merge($body, $billing_parameters);
@@ -41,6 +43,28 @@ public function getParams($checkout_id): array
4143
return ['entityId' => $entityId];
4244
}
4345

46+
/**
47+
* Generate the params that used in the recurring payment
48+
* @param string $amount
49+
* @param string $shopperResultUrl
50+
* @param string $checkout_id That define the entity_id related to the registration id.
51+
* @return array
52+
*/
53+
public function postRecurringPayment($amount, $shopperResultUrl, $checkout_id)
54+
{
55+
$currency = config('hyperpay.currency');
56+
57+
return array_merge([
58+
"standingInstruction.mode"=>"REPEATED",
59+
"standingInstruction.type" => "RECURRING",
60+
"standingInstruction.source"=>"MIT",
61+
"amount"=> $amount,
62+
"currency" => $currency,
63+
"paymentType"=>"PA",
64+
"shopperResultUrl" => $shopperResultUrl
65+
], $this->getParams($checkout_id));
66+
}
67+
4468
/**
4569
* Generate the basic user parameters.
4670
*
@@ -70,6 +94,20 @@ protected function getBodyParameters($amount, Model $user, $heyPerPayConfig): ar
7094
return $body_parameters;
7195
}
7296

97+
/**
98+
* The init recurring payment params
99+
* @return array
100+
*/
101+
protected function registerPaymentData()
102+
{
103+
return [
104+
"standingInstruction.mode"=>"INITIAL",
105+
"standingInstruction.type" => "RECURRING",
106+
"standingInstruction.source"=>"CIT",
107+
"createRegistration"=>true,
108+
];
109+
}
110+
73111
/**
74112
* Get the billing data from the Billing class if a user generate one.
75113
*

src/Support/HttpResponse.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,18 @@ public function paymentStatus()
115115
return response()->json($response, $response['status']);
116116
}
117117

118+
/**
119+
* Get the recurring payment response.
120+
*
121+
* @return \Illuminate\Support\Facades\Response
122+
*/
123+
public function recurringPayment()
124+
{
125+
$response = $this->response();
126+
127+
return response()->json($response, $response['status']);
128+
}
129+
118130
/**
119131
* Get the body of the response.
120132
*

0 commit comments

Comments
 (0)