我努力工作,Omnipay没有多少文档。我已成功将其用于其他支付网关,但未与Sagepay一起使用。我试图将它集成到CodeIgniter中,但可以从其他框架中的示例中工作 - 我越来越绝望了!有没有人有Omnipay和Sagepay Server或Sagepay Direct(使用3D Secure)的工作示例?
回答
感谢github上的一些很好的帮助(请参阅我的原始帖子中的主题链接评论),我现在有一些可行的代码,我将在此分享以防将来帮助其他人。
<?php
use Omnipay\Omnipay;
class PaymentGateway {
//live details
private $live_vendor = 'xxx';
//test details
private $test_vendor= 'xxx';
//payment settings
private $testMode = true;
private $api_vendor = '';
private $gateway = null;
public function __construct()
{
parent::__construct();
//setup api details for test or live
if ($this->testMode) :
$this->api_vendor = $this->test_vendor;
else :
$this->api_vendor = $this->live_vendor;
endif;
//initialise the payment gateway
$this->gateway = Omnipay::create('SagePay_Server');
$this->gateway->setVendor($this->api_vendor);
$this->gateway->setTestMode($this->testMode);
}
public function initiate()
{
//get order details
$orderNo = customFunctionToGetOrderNo(); //get the order number from your system however you store and retrieve it
$params = array(
'description'=> 'Online order',
'currency'=> 'GBP',
'transactionId'=> $orderNo,
'amount'=> customFunctionToGetOrderTotal($orderNo)
);
$customer = customFunctionToGetCustomerDetails($orderNo);
$params['returnUrl'] = '/payment-gateway-process/' . $orderNo . '/'; //this is the Sagepay NotificationURL
$params['card'] = array(
'firstName' => $customer['billing_firstname'],
'lastName' => $customer['billing_lastname'],
'email' => $customer['billing_email'],
'billingAddress1' => $customer['billing_address1'],
'billingAddress2' => $customer['billing_address2'],
'billingCity' => $customer['billing_town'],
'billingPostcode' => $customer['billing_postcode'],
'billingCountry' => $customer['billing_country'],
'billingPhone' => $customer['billing_telephone'],
'shippingAddress1' => $customer['delivery_address1'],
'shippingAddress2' => $customer['delivery_address2'],
'shippingCity' => $customer['delivery_town'],
'shippingPostcode' => $customer['delivery_postcode'],
'shippingCountry' => $customer['delivery_country']
);
try {
$response = $this->gateway->purchase($params)->send();
if ($response->isSuccessful()) :
//not using this part
elseif ($response->isRedirect()) :
$reference = $response->getTransactionReference();
customFunctionToSaveTransactionReference($orderNo, $reference);
$response->redirect();
else :
//do something with an error
echo $response->getMessage();
endif;
} catch (\Exception $e) {
//do something with this if an error has occurred
echo 'Sorry, there was an error processing your payment. Please try again later.';
}
}
public function processPayment($orderNo)
{
$params = array(
'description'=> 'Online order',
'currency'=> 'GBP',
'transactionId'=> $orderNo,
'amount'=> customFunctionToGetOrderTotal($orderNo)
);
$customer = customFunctionToGetCustomerDetails($orderNo);
$transactionReference = customFunctionToGetTransactionReference($orderNo);
try {
$response = $this->gateway->completePurchase(array(
'transactionId' => $orderNo,
'transactionReference' => $transactionReference,
))->send();
customFunctionToSaveStatus($orderNo, array('payment_status' => $response->getStatus()));
customFunctionToSaveMessage($orderNo, array('gateway_response' => $response->getMessage()));
//encrypt it to stop anyone being able to view other orders
$encodeOrderNo = customFunctionToEncodeOrderNo($orderNo);
$response->confirm('/payment-gateway-response/' . $encodeOrderNo);
} catch(InvalidResponseException $e) {
// Send "INVALID" response back to SagePay.
$request = $this->gateway->completePurchase(array());
$response = new \Omnipay\SagePay\Message\ServerCompleteAuthorizeResponse($request, array());
customFunctionToSaveStatus($orderNo, array('payment_status' => $response->getStatus()));
customFunctionToSaveMessage($orderNo, array('gateway_response' => $response->getMessage()));
redirect('/payment-error-response/');
}
}
public function paymentResponse($encodedOrderNo)
{
$orderNo = customFunctionToDecode($encodedOrderNo);
$sessionOrderNo = customFunctionToGetOrderNo();
if ($orderNo != $sessionOrderNo) :
//do something here as someone is trying to fake a successful order
endif;
$status = customFunctionToGetOrderStatus($orderNo);
switch(strtolower($status)) :
case 'ok' :
customFunctionToHandleSuccess($orderNo);
break;
case 'rejected' :
case 'notauthed' :
//do something to handle failed payments
break;
case 'error' :
//do something to handle errors
break;
default:
//do something if it ever reaches here
endswitch;
}
}
我看到订单号在返回URL中被加密。另一种方法是不在该URL上提供任何参数,而是在调用通知处理程序之前依靠订单号保存到会话中。关于错误的“重定向”也是错误的 - 在这里没有重定向。您必须调用'$ response-> error('your url','您的可选信息');'而不是,然后将URL返回给SagePay,SagePay会将用户发送给您。 – Jason 2015-07-07 18:37:39
我昨晚做了谈这一点,已经把工作演示脚本在GitHub上的位置:
https://github.com/academe/OmniPay-SagePay-Demo
SagePay Direct是一次性的行动 - OmniPay发送交易细节并立即得到回应。
SagePay服务器涉及用户重定向到SagePay网站,以使用他们的卡详细信息授权交易。此API使用通知消息,其中SagePay将直接使用授权结果调用您的应用程序。这发生在用户会话之外,因此需要将事务存储在数据库中,以便可以在两个事务之间共享。
所有这些都在上面链接的脚本中。 authorize.php
将执行授权。编辑该文件以使用SagePay\Direct
或SagePay\Server
来查看它是如何工作的。 SagePay\Server
的通知处理程序是sagepay-confirm.php
,最终将用户发送到final.php
,其中可以从存储在数据库中的事务中读取结果。
脚本都是注释,应该是有道理的,但随时在这里或在该github存储库的问题跟踪器中提出更多问题。
虽然我没有尝试使用3D-Secure的SagePay\Direct
。这些脚本可能需要一些修改才能支持,假设组合是的东西。
- 1. Sagepay Direct和Cloudflare
- 2. Sagepay Direct 3D安全空白页问题
- 3. Omnipay sagepay使用什么协议?
- 4. 如何使用sagepay服务器(内框)完成与Omnipay Sagepay的购买
- 5. SagePay多语言 - 文本没有模板
- 6. Omnipay sagepay 2.0软件包找不到
- 7. 有没有glxinfo的Direct 3D等价物?
- 8. chosen.js ::有没有人有一个实际的工作示例?
- 9. 有没有人有Cocos2d中的ParticleExplosion的工作示例?
- 10. 使用Windows Azure配置SagePay Direct的IP地址
- 11. 有没有人有gevent-socketio的工作示例?
- 12. 有没有人有使用API在Squarespace上工作的谷歌街景示例?
- 13. 有没有人有Android的cropImage示例?
- 14. iFrame sagepay低调?
- 15. Cartthrob Sagepay错误
- 16. 一把umbraco&SagePay
- 17. 有没有人使用(或考虑使用)Sql Server精简版?
- 18. 有没有人有工作的Django AJAX表单的例子?
- 19. 有没有人有FullCalendar事件的示例(作为函数)
- 20. AWS IP系列和Sagepay
- 21. 有没有人有使用encodeBytes的示例代码:length:forKey:?
- 22. 有没有人有一个ActionCells在CompositeCell内工作的例子?
- 23. SagePay响应问题
- 24. 有没有人有WPF Datagrid在MVVM模式下工作的代码示例?
- 25. SagePay XML购物篮
- 26. 有没有人创建一个3D网站,在3D监视器上工作?
- 27. 有没有人有一个在OrientDB中JSON插入/选择的工作示例?
- 28. 有没有人有一个json分页mvc3 webgrid的工作示例?
- 29. 为sagepay设置ReturnURL
- 30. SagePay集成窗体
刚刚发现模拟器不再支持这是我用于测试。我不知道是否这是我的问题的原因,因为现在必须建立一个合作伙伴帐户进行测试,但认为我会更新这个以防其他人在此期间遇到此问题。 – JoJo 2015-04-01 13:53:20
还有一个关于Sagepay协议3和Omnipay的线索https://github.com/thephpleague/omnipay-sagepay/issues/19 – JoJo 2015-04-01 14:48:42
此主题让我走上了正轨:https://github.com/thephpleague/omnipay/issues/255 – JoJo 2015-04-07 14:52:02