Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to update a charge #119

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,20 @@ public function purchase(array $parameters = array())
return $this->createRequest('\Omnipay\Stripe\Message\PurchaseRequest', $parameters);
}

/**
* Update charge request.
*
* Update the data for a charge after the charge has been created.
*
* @param array $parameters
*
* @return \Omnipay\Stripe\Message\Response

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Return type should be
* @return \Omnipay\Stripe\Message\UpdateChargeRequest

*/
public function updateCharge(array $parameters = array())
{
return $this->createRequest('\Omnipay\Stripe\Message\UpdateChargeRequest', $parameters);
}

/**
* Refund Request.
*
Expand Down
50 changes: 50 additions & 0 deletions src/Message/UpdateChargeRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/**
* Stripe Update Charge Request.
*/
namespace Omnipay\Stripe\Message;

/**
* Stripe Update Charge Request.
*
* @see \Omnipay\Stripe\Gateway
* @link https://stripe.com/docs/api/charges/update
*/
class UpdateChargeRequest extends AuthorizeRequest
{
public function getData()
{
$this->validate('transactionReference');

$data = array();

if ($this->getDescription()) {
$data['description'] = $this->getDescription();
}

if ($this->getCustomerReference()) {
$data['customer'] = $this->getCustomerReference();
}

if ($this->getMetadata()) {
$data['metadata'] = $this->getMetadata();
}

if ($this->getReceiptEmail()) {
$data['receipt_email'] = $this->getReceiptEmail();
}

if ($this->getTransferGroup()) {
$data['transfer_group'] = $this->getTransferGroup();
}

// Filter out the empty/non-updated values
return $data;
}

public function getEndpoint()
{
return $this->endpoint.'/charges/'.$this->getTransactionReference();
}
}
61 changes: 61 additions & 0 deletions tests/Message/UpdateChargeRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Omnipay\Stripe\Message;

use Omnipay\Tests\TestCase;

class UpdateChargeRequestTest extends TestCase
{
public function setUp()
{
$this->request = new UpdateChargeRequest($this->getHttpClient(), $this->getHttpRequest());
$this->request->setTransactionReference('foo');
}

public function testEndpoint()
{
$this->assertSame('https://api.stripe.com/v1/charges/foo', $this->request->getEndpoint());
}

public function testData()
{
$this->request->setDescription('New customer');
$this->request->setCustomerReference('cus_1MZeNih5LdKxDq');
$this->request->setReceiptEmail('[email protected]');
$this->request->setTransferGroup('test');
$this->request->setMetadata(array('field' => 'value'));

$data = $this->request->getData();

$this->assertSame('cus_1MZeNih5LdKxDq', $data['customer']);
$this->assertSame('[email protected]', $data['receipt_email']);
$this->assertSame('New customer', $data['description']);
$this->assertArrayHasKey('field', $data['metadata']);
$this->assertSame('value', $data['metadata']['field']);
$this->assertSame('test', $data['transfer_group']);
}

public function testSendSuccess()
{
$this->setMockHttpResponse('UpdateChargeSuccess.txt');
$response = $this->request->send();

$this->assertTrue($response->isSuccessful());
$this->assertFalse($response->isRedirect());
$this->assertSame('ch_1E0Pt92eZvKYlo2C05QSmQvw', $response->getTransactionReference());
$this->assertSame('cus_1MZeNih5LdKxDq', $response->getCustomerReference());
$this->assertNull($response->getMessage());
}

public function testSendFailure()
{
$this->setMockHttpResponse('UpdateChargeFailure.txt');
$response = $this->request->send();

$this->assertFalse($response->isSuccessful());
$this->assertFalse($response->isRedirect());
$this->assertNull($response->getTransactionReference());
$this->assertNull($response->getCustomerReference());
$this->assertSame('No such charge: ch_1E0Pt92eZvKYlo2C0QSmQvw', $response->getMessage());
}
}
19 changes: 19 additions & 0 deletions tests/Mock/UpdateChargeFailure.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
HTTP/1.1 400 Bad Request
Server: nginx
Date: Sun, 05 May 2013 08:52:09 GMT
Content-Type: application/json;charset=utf-8
Content-Length: 127
Connection: keep-alive
Cache-Control: no-cache, no-store
Access-Control-Max-Age: 300
Access-Control-Allow-Credentials: true

{
"error": {
"code": "resource_missing",
"doc_url": "https://stripe.com/docs/error-codes/resource-missing",
"message": "No such charge: ch_1E0Pt92eZvKYlo2C0QSmQvw",
"param": "id",
"type": "invalid_request_error"
}
}
104 changes: 104 additions & 0 deletions tests/Mock/UpdateChargeSuccess.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
HTTP/1.1 200 OK
Server: nginx
Date: Sun, 05 May 2013 08:51:15 GMT
Content-Type: application/json;charset=utf-8
Content-Length: 997
Connection: keep-alive
Cache-Control: no-cache, no-store
Access-Control-Max-Age: 300
Access-Control-Allow-Credentials: true

{
"id": "ch_1E0Pt92eZvKYlo2C05QSmQvw",
"object": "charge",
"amount": 1000,
"amount_refunded": 0,
"application": null,
"application_fee": null,
"application_fee_amount": null,
"balance_transaction": "txn_19XJJ02eZvKYlo2ClwuJ1rbA",
"captured": false,
"created": 1549357603,
"currency": "usd",
"customer": "cus_1MZeNih5LdKxDq",
"description": "Test",
"destination": null,
"dispute": null,
"failure_code": "card_declined",
"failure_message": "Your card was declined.",
"fraud_details": {},
"invoice": null,
"livemode": false,
"metadata": {},
"on_behalf_of": null,
"order": null,
"outcome": {
"network_status": "not_sent_to_network",
"reason": "highest_risk_level",
"risk_level": "highest",
"risk_score": 96,
"rule": "block_if_high_risk",
"seller_message": "Stripe blocked this payment as too risky.",
"type": "blocked"
},
"paid": false,
"payment_intent": null,
"payment_method": null,
"payment_method_details": {
"card": {
"brand": "Visa",
"country": "US",
"dynamic_last4": null,
"exp_month": 12,
"exp_year": 2031,
"fingerprint": "yTFMYiPXiPa5AoSn",
"funding": "credit",
"last4": "4954",
"three_d_secure": null
},
"type": "card"
},
"receipt_email": null,
"receipt_number": "1431-2893",
"receipt_url": "https://pay.stripe.com/receipts/acct_1032D82eZvKYlo2C/ch_1E0Pt92eZvKYlo2C05QSmQvw/rcpt_ETMcXQkHm68RMJztwqc6DDKAdCm8QR7",
"refunded": false,
"refunds": {
"object": "list",
"data": [],
"has_more": false,
"total_count": 0,
"url": "/v1/charges/ch_1E0Pt92eZvKYlo2C05QSmQvw/refunds"
},
"review": null,
"shipping": null,
"source": {
"id": "card_1E0Pt52eZvKYlo2Czz19lZi3",
"object": "card",
"address_city": null,
"address_country": null,
"address_line1": null,
"address_line1_check": null,
"address_line2": null,
"address_state": null,
"address_zip": "12345",
"address_zip_check": "unavailable",
"brand": "Visa",
"country": "US",
"customer": null,
"cvc_check": "unavailable",
"dynamic_last4": null,
"exp_month": 12,
"exp_year": 2031,
"fingerprint": "yTFMYiPXiPa5AoSn",
"funding": "credit",
"last4": "4954",
"metadata": {},
"name": "[email protected]",
"tokenization_method": null
},
"source_transfer": null,
"statement_descriptor": null,
"status": "failed",
"transfer_data": null,
"transfer_group": null
}