Skip to content
This repository has been archived by the owner on Feb 10, 2023. It is now read-only.

Commit

Permalink
Fixed a bug preventing payment sources from populating payments
Browse files Browse the repository at this point in the history
  • Loading branch information
jmauzyk committed Apr 29, 2021
1 parent 9b2464f commit 5344169
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/models/CreditCardPaymentForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace jmauzyk\commerce\cardconnect\models;

use jmauzyk\commerce\cardconnect\errors\PaymentSourceException;

use craft\commerce\models\PaymentSource;
use craft\commerce\models\payments\CreditCardPaymentForm as BaseCreditCardPaymentForm;

Expand All @@ -33,7 +35,7 @@ class CreditCardPaymentForm extends BaseCreditCardPaymentForm
*/
public function populateFromPaymentSource(PaymentSource $paymentSource)
{
$response = json_decode($paymentSource->response);
$response = $this->_getResponseObject($paymentSource->response);
$name = explode(' ', $response->name, 2);
$this->profile = $paymentSource->token;
$this->firstName = $name[0];
Expand All @@ -54,4 +56,26 @@ public function defineRules(): array

return $rules;
}

/**
* Converts response to object depending on variable type
*
* @param mixed $response
* @return object
* @throws PaymentSourceException
*/
private function _getResponseObject($response): object
{
$type = gettype($response);

if ($type === 'string') {
return json_decode($response);
}

if ($type === 'array') {
return (object)$response;
}

throw new PaymentSourceException('Invalid payment source response.');
}
}

0 comments on commit 5344169

Please sign in to comment.