Use ApplePay
Last updated: 11-Jul-2024
Rate this article:
Overview
Place an order using catalog products defined in your Control Panel, and collect the payment using ApplePay.
Currency support
Check with 2Checkout support for a list of currencies available for ApplePay.
Activation
For activation, you need to have Apple Pay enabled on your 2Checkout account. Contact the Merchant Support team in order to enable this. After 2Checkout sets up our domain, we will provide you with a domain verification file.
Host your domain verification file at the following path on our server: https://[DOMAIN_NAME]/.well-known/apple-developer-merchantid-domain-association.
Workflow
- Initialize an ApplePay session using our API.
- Collect the "ApplePayDataToken" provided by Apple Pay client side and pass it server side to make the placeOrder API call.
- Create the order object. Use APPLE_PAY as the type in the PaymentDetails object and pass the "ApplePayDataToken" through the "ApplePayToken" property in the PaymentMethod object. Place the order.
Parameters
Parameters | Type/Description |
---|---|
sessionID | Required (string) |
Session identifier, the output of the Login method. Include sessionID into all your requests. 2Checkout throws an exception if the values are incorrect. The sessionID expires in 10 minutes. | |
Order | Required (Object) |
Object designed to collect all data necessary for an order, including billing, product/subscription plan and payment details. |
Response parameters
Parameters | Type/Description |
---|---|
Order information | Object |
Object containing order information. |
Request
<?php
declare(strict_types=1);
class Configuration
{
public const MERCHANT_CODE = 'MERCHANT_CODE';
public const MERCHANT_KEY = 'SECRET_KEY';
public const URL = 'http://api.avangate.local/soap/6.0';
public const ACTION = 'placeOrder';
public const PAYLOAD = <<<JSON
{
"Currency": "usd",
"Language": "en",
"Country": "us",
"Source": "API",
"Affiliate": {
"AffiliateCode": "ABCDE1234"
},
"Items": [
{
"Code": "K6DHJXGULK",
"Quantity": 2
}
],
"BillingDetails": {
"Address1": "Test Address",
"City": "LA",
"State": "California",
"CountryCode": "US",
"Email": "testcustomer@2Checkout.com",
"FirstName": "Customer",
"LastName": "2Checkout",
"Zip": "12345"
},
"PaymentDetails":{
"Type":"APPLE_PAY",
"Currency":"USD",
"CustomerIP":"10.10.10.10",
"PaymentMethod":{
"ApplePayToken":"ApplePayDataToken"
}
}
}
JSON;
}
class Client
{
public function call(
string $url = Configuration::URL,
$payload = Configuration::PAYLOAD,
string $action = Configuration::ACTION
): ?object {
if (is_array($payload)) {
$payload = json_encode($payload);
}
if (!empty($payload)) {
// SoapClient works with objects(StdClass)
$payload = json_decode($payload);
}
$soapClient = $this->getClient($url);
$sessionId = $this->getSession($soapClient);
$args = array_filter([$sessionId, $payload]);
return $soapClient->$action(...$args);
}
public function getClient(string $url): SoapClient
{
return new SoapClient(
$url.'?wsdl',
[
'location' => $url,
'cache_wsdl' => WSDL_CACHE_NONE,
]
);
}
public function getSession(SoapClient $client)
{
$date = gmdate('Y-m-d H:i:s');
$merchantCode = Configuration::MERCHANT_CODE;
$key = Configuration::MERCHANT_KEY;
$string = strlen($merchantCode).$merchantCode.strlen($date).$date;
$algo = 'sha256';
$hash = hash_hmac($algo, $string, $key);
$client->__setCookie('XDEBUG_SESSION', 'PHPSTORM');
return $client->login($merchantCode, $date, $hash, $algo);
}
}
try {
$client = new Client();
var_dump($client->call());
} catch (Exception $ex) {
var_dump($ex);
}
Rate this article: