From c7a0ca56ad6362fc6cb33406224f65356451c1f3 Mon Sep 17 00:00:00 2001 From: Guillaume Malleret Date: Wed, 15 Aug 2018 12:34:37 +0200 Subject: [PATCH] Add charge with local (alternative) payment methods --- .../ApiServices/Charges/ChargeService.php | 24 ++++++++ .../ApiServices/Charges/ChargesMapper.php | 11 ++++ .../RequestModels/LocalPaymentCharge.php | 47 ++++++++++++++++ .../RequestModels/BaseLocalPayment.php | 45 +++++++++++++++ com/checkout/helpers/ApiUrls.php | 25 +++++++++ .../ChargeServiceLocalPaymentTest.php | 55 +++++++++++++++++++ 6 files changed, 207 insertions(+) create mode 100644 com/checkout/ApiServices/Charges/RequestModels/LocalPaymentCharge.php create mode 100644 com/checkout/ApiServices/LocalPayment/RequestModels/BaseLocalPayment.php create mode 100644 test/ChargeService/ChargeServiceLocalPaymentTest.php diff --git a/com/checkout/ApiServices/Charges/ChargeService.php b/com/checkout/ApiServices/Charges/ChargeService.php index c59b421..fb4a95c 100644 --- a/com/checkout/ApiServices/Charges/ChargeService.php +++ b/com/checkout/ApiServices/Charges/ChargeService.php @@ -306,6 +306,30 @@ public function getChargeHistory($chargeId) $responseModel = new ResponseModels\ChargeHistory($processCharge); return $responseModel; } + + /** + * Creates a charge with a local payment + * @param RequestModels\LocalPaymentCharge $requestModel + * @return ResponseModels\Charge + */ + public function chargeWithLocalPayment(RequestModels\LocalPaymentCharge $requestModel) + { + + $chargeMapper = new ChargesMapper($requestModel); + $requestPayload = array ( + 'authorization' => $this->_apiSetting->getSecretKey(), + 'mode' => $this->_apiSetting->getMode(), + 'postedParam' => $chargeMapper->requestPayloadConverter(), + + ); + + $processCharge = \com\checkout\helpers\ApiHttpClient::postRequest($this->_apiUrl->getLocalPaymentChargesApiUrl(), + $this->_apiSetting->getSecretKey(),$requestPayload); + + $responseModel = new ResponseModels\Charge($processCharge); + + return $responseModel; + } } diff --git a/com/checkout/ApiServices/Charges/ChargesMapper.php b/com/checkout/ApiServices/Charges/ChargesMapper.php index 7cf874c..c6f9ce6 100644 --- a/com/checkout/ApiServices/Charges/ChargesMapper.php +++ b/com/checkout/ApiServices/Charges/ChargesMapper.php @@ -335,6 +335,17 @@ public function requestPayloadConverter($requestModel = null ) $requestPayload['paymentPlans'][] = $requestSinglePaymentPlan; } } + + if(method_exists($requestModel,'getBaseLocalPayment') ) { + $baseLocalPayment = $requestModel->getBaseLocalPayment(); + + $requestPayload['localPayment']['lppId'] = $baseLocalPayment->getLppId(); + + if ( !empty($baseLocalPayment->getIssuerId()) ) { + $requestPayload['localPayment']['userData']['issuerId'] = $baseLocalPayment->getIssuerId(); + } + + } } diff --git a/com/checkout/ApiServices/Charges/RequestModels/LocalPaymentCharge.php b/com/checkout/ApiServices/Charges/RequestModels/LocalPaymentCharge.php new file mode 100644 index 0000000..5647cb8 --- /dev/null +++ b/com/checkout/ApiServices/Charges/RequestModels/LocalPaymentCharge.php @@ -0,0 +1,47 @@ +_baseLocalPayment; + } + + /** + * @param \com\checkout\ApiServices\LocalPayment\RequestModels\BaseLocalPayment $baseLocalPayment + */ + public function setBaseLocalPayment (\com\checkout\ApiServices\LocalPayment\RequestModels\BaseLocalPayment $baseLocalPayment ) + { + $this->_baseLocalPayment = $baseLocalPayment; + } + + /** + * @return mixed + */ + public function getPaymentToken () + { + return $this->_paymentToken; + } + + /** + * @param mixed $paymentToken + */ + public function setPaymentToken ( $paymentToken ) + { + $this->_paymentToken = $paymentToken; + } + +} \ No newline at end of file diff --git a/com/checkout/ApiServices/LocalPayment/RequestModels/BaseLocalPayment.php b/com/checkout/ApiServices/LocalPayment/RequestModels/BaseLocalPayment.php new file mode 100644 index 0000000..5b5aeeb --- /dev/null +++ b/com/checkout/ApiServices/LocalPayment/RequestModels/BaseLocalPayment.php @@ -0,0 +1,45 @@ +_lppId; + } + + /** + * @return string Issuer ID + */ + public function getIssuerId() + { + return $this->_issuerId; + } + + /** + * @param string $lppId Payment provider ID + * @return $this + */ + public function setLppId($lppId) + { + $this->_lppId = $lppId; + return $this; + } + + /** + * @param string $issuerId Issuer ID + * @return $this + */ + public function setIssuerId($issuerId) + { + $this->_issuerId = $issuerId; + return $this; + } + +} diff --git a/com/checkout/helpers/ApiUrls.php b/com/checkout/helpers/ApiUrls.php index c32c41c..d30f91a 100644 --- a/com/checkout/helpers/ApiUrls.php +++ b/com/checkout/helpers/ApiUrls.php @@ -31,6 +31,7 @@ class ApiUrls private $_recurringPaymentsCustomersApiUri = null; private $_recurringPaymentsCustomersQueryApiUri = null; private $_visaCheckoutCardTokenApiUri = null; + private $_localPayementChargesApiUrl = null; public function __construct() { @@ -592,4 +593,28 @@ public function setVisaCheckoutCardTokenApiUri($visaCheckoutCardTokenApiUri) { $this->_visaCheckoutCardTokenApiUri = $visaCheckoutCardTokenApiUri; } + + /** + * get local payment charge url + * @return string + */ + public function getLocalPaymentChargesApiUrl() + { + if (empty($this->_localPayementChargesApiUrl)) { + $this->setLocalPayementChargesApiUrl($this->getBaseApiUri() . "/charges/localpayment"); + } + return $this->_localPayementChargesApiUrl; + } + + /** + * + * @param string $localPayementChargesApiUrl Local payment charge API url + * @return $this + */ + public function setLocalPayementChargesApiUrl($localPayementChargesApiUrl) + { + $this->_localPayementChargesApiUrl = $localPayementChargesApiUrl; + return $this; + } + } diff --git a/test/ChargeService/ChargeServiceLocalPaymentTest.php b/test/ChargeService/ChargeServiceLocalPaymentTest.php new file mode 100644 index 0000000..4cb03e7 --- /dev/null +++ b/test/ChargeService/ChargeServiceLocalPaymentTest.php @@ -0,0 +1,55 @@ +_apiClient = new \com\checkout\ApiClient('sk_test_a2dba067-bfe8-425c-88e9-6685820aa16e'); + + } + + public function testChargeWithLocalPayment() + { + + $paymentTokenCreate = new \com\checkout\ApiServices\Tokens\RequestModels\PaymentTokenCreate(); + $paymentTokenCreate->setValue(100); + $paymentTokenCreate->setCurrency('EUR'); + $paymentTokenCreate->setChargeMode('3'); + $paymentTokenCreate->setSuccessUrl('http://mycustomerurl.com/order?result=pass'); + $paymentTokenCreate->setFailUrl('http://mycustomerurl.com/order?result=fail'); + + $tokenService = $this->_apiClient->tokenService(); + $paymentToken = $tokenService->createPaymentToken($paymentTokenCreate); + + $this->baseLocalPayment = new \com\checkout\ApiServices\LocalPayment\RequestModels\BaseLocalPayment(); + // iDEAL + $this->baseLocalPayment->setLppId('lpp_9'); + $this->baseLocalPayment->setIssuerId('RABONL2U'); + + $charge = new \com\checkout\ApiServices\Charges\RequestModels\LocalPaymentCharge(); + $charge->setBaseLocalPayment($this->baseLocalPayment); + $charge->setPaymentToken($paymentToken->getId()); + $charge->setEmail('test@example.org'); + + $chargeService = $this->_apiClient->chargeService(); + $chargeResponse = $chargeService->chargeWithLocalPayment($charge); + + $this->assertFalse( $chargeResponse->hasError()); + $this->assertEquals(200, $chargeResponse->getHttpStatus()); + $this->assertNotNull($chargeResponse->getId()); + $this->assertStringStartsWith('https://sandbox.checkout.com', $chargeResponse->getLocalPayment()->getPaymentUrl()); + + } + + +} + \ No newline at end of file