hieudmg/ payway-api-sdk
Payway API SDK
dev-master
2020-10-19 04:33 UTC
This package is auto-updated.
Last update: 2024-09-19 12:57:14 UTC
README
免责声明
这是我为开发Payway支付插件所使用的基准包。其中可能有些笨拙,但它完成了工作。如果您有任何建议,请提交问题。
用法
您不能立即使用此包。您必须实现一些功能才能使其工作。这提供了灵活性和框架兼容性。
实现抽象函数
您需要继承AbstractPaywayApiWrapper类来使用它。目前需要实现三个函数
protected abstract function getStoreUid(); protected abstract function getMerchantInformation(); protected abstract function makeRequest($url, $method, $idempotencyKey = '', $data = null, $additionalHeaders = null);
getStoreUid
:此函数用于获取您框架中保存的商店唯一标识符。客户相关功能需要此标识符以确保在Payway中保存的每个商店的客户都是唯一的。想象一下在两个使用相同商户凭据的商店中使用插件,如果没有此标识符,具有id 11的客户可能被其他商店误用并造成混乱。getMerchantInformation
:此函数返回框架数据库中保存的商户信息。它需要返回PaywaySDK\Data\MerchantInformation
类型。makeRequest
:此函数使用框架的实现执行请求。每个框架都需要不同的方式来执行外部请求。例如:Magento 2使用Magento\Framework\HTTP\Client\Curl
,WordPress使用wp_remote_request
。函数必须返回一个数组 - 来自Payway网关的解码JSON响应。别忘了添加必要的API头并在响应中添加幂等键。WordPress实现示例,失败时进行重试
protected function makeRequest( $url, $method, $idempotencyKey = '', $data = null, $additionalHeaders = null ) { if ( ! is_array( $additionalHeaders ) ) { $additionalHeaders = []; } $headers = array_merge( $additionalHeaders, array( 'Idempotency-Key' => $idempotencyKey, 'Authorization' => 'Basic ' . base64_encode( $this->getMerchantInformation()->getPrivateKey() ), 'Content-Type' => 'application/x-www-form-urlencoded' ) ); if ( $method == METHOD_GET ) { $data = null; } elseif ( is_array( $data ) ) { $data = http_build_query( $data ); } elseif ( ! is_string( $data ) ) { $data = null; } $result = array(); for ( $retries = 0; $retries < 4; $retries ++ ) { if ( $retries > 0 ) { sleep( 20 ); } $result = wp_remote_request( $url, array( 'method' => $method, 'headers' => $headers, 'body' => $data, 'timeout' => 5 ) ); if ( $result instanceof WP_Error ) { continue; } elseif ( $result['response']['code'] == 429 || $result['response']['code'] == 503 ) { continue; } elseif ( $result['response']['code'] > 200 && $result['response']['code'] < 299 || $result['response']['code'] == 422 ) { $result = json_decode( $result['body'], 1 ); $result['idempotencyKey'] = $idempotencyKey; break; } else { unset( $result['cookies'] ); unset( $result['http_response'] ); break; } } return $result; }
使用包装器
实现抽象函数后,您可以开始使用它。示例用法
$api = new PaywayApiWrapper(); /** @var \PaywaySDK\Response\TransactionResponse $paymentResult */ $paymentResult = $api->takePayment( $paywayToken, $customerId, $orderTotal, $customerIpAddress ); if ($paymentResult && $paymentResult->isSuccess()) { // Process order here } else { // Something went wrong, handle error here }
贡献
如果您发现任何问题或/或有改进的想法,请提交问题。